2014-09-04 117 views
0
我有我的項目的問題

我不能將程序與數據源Java和數據庫連接

所以如果有連接任何幫助plzz幫助我

這是錯誤信息和源代碼下面

我就麻煩了plzzz幫助meeeeee

//for creating the North Panel 
    private JPanel northPanel = new JPanel(); 
    //for creating the Center Panel 
    private JPanel centerPanel = new JPanel(); 
    //for creating the label 
    private JLabel northLabel = new JLabel("THE LIST FOR THE BOOKS"); 
    //for creating the button 
    private JButton printButton; 
    //for creating the table 
    private JTable table; 
    //for creating the TableColumn 
    private TableColumn column = null; 
    //for creating the JScrollPane 
    private JScrollPane scrollPane; 

    //for creating an object for the ResultSetTableModel class 
    private ResultSetTableModel tableModel; 

    /*************************************************************************** 
    * for setting the required information for the ResultSetTableModel class. * 
    ***************************************************************************/ 
    private static final String JDBC_DRIVER = "sun.jdbc.odbc.JdbcOdbcDriver"; 
    private static final String DATABASE_URL = "jdbc:odbc:Telecom"; 
    private static final String DEFAULT_QUERY = "SELECT EmployeeID,EmployeeName,ProjectName,JobTitle,MobileNumber,DateOfSim,SimNumber,MCG,Active FROM [Telecom].[dbo].[Employee];"; 


    //constructor of listBooks 
    public ListBooks() { 
     //for setting the title for the internal frame 
     super("Employee", false, true, false, true); 
     //for setting the icon 
     setFrameIcon(new ImageIcon(ClassLoader.getSystemResource("images/List16.gif"))); 
     setLocale(new java.util.Locale("ar", "SA", "")); 

     //for getting the graphical user interface components displaygvk area 
     Container cp = getContentPane(); 

     //for bassing the required information to the ResultSetTableModel object 
     try { 
      tableModel = new ResultSetTableModel(JDBC_DRIVER, DATABASE_URL, DEFAULT_QUERY); 
      //for setting the Query 
      try { 
       tableModel.setQuery(DEFAULT_QUERY); 
      } 
      catch (SQLException sqlException) { 
      } 
     } 
     catch (ClassNotFoundException classNotFound) { 
      System.out.println(classNotFound.toString()); 
     } 
     catch (SQLException sqlException) { 
      System.out.println(sqlException.toString()); 
     } 
     //for setting the table with the information 
     table = new JTable(tableModel); 
     //for setting the size for the table 
     table.setPreferredScrollableViewportSize(new Dimension(990, 200)); 
     //for setting the font 
     table.setFont(new Font("Tahoma", Font.PLAIN, 12)); 
     //for setting the scrollpane to the table 
     scrollPane = new JScrollPane(table); 

     //for setting the size for the table columns 
     for (int i = 0; i < 9; i++) { 

      column = table.getColumnModel().getColumn(i); 
      if (i == 0) //BookID 
      column.setPreferredWidth(20); 
      if (i == 1) //Subject 
       column.setPreferredWidth(100); 
      if (i == 2) //Title 
       column.setPreferredWidth(150); 
      if (i == 3) //Auther 
       column.setPreferredWidth(50); 
      if (i == 4) //Publisher 
       column.setPreferredWidth(70); 
      if (i == 5) //Copyright 
       column.setPreferredWidth(40); 
      if (i == 6) //Edition 
       column.setPreferredWidth(40); 
      if (i == 7) //Pages 
       column.setPreferredWidth(40); 
      if (i == 8) //NumberOfBooks 
       column.setPreferredWidth(80); 


       } 
     //for setting the font to the label 
     northLabel.setFont(new Font("Tahoma", Font.BOLD, 14)); 
     //for setting the layout to the panel 
     northPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 
     //for adding the label to the panel 
     northPanel.add(northLabel); 
     //for adding the panel to the container 
     cp.add("North", northPanel); 

     //for setting the layout to the panel 
     centerPanel.setLayout(new BorderLayout()); 
     //for creating an image for the button 
     ImageIcon printIcon = new ImageIcon(ClassLoader.getSystemResource("images/Print16.gif")); 
     //for adding the button to the panel 
     printButton = new JButton("print the books", printIcon); 
     //for setting the tip text 
     printButton.setToolTipText("Print"); 
     //for setting the font to the button 
     printButton.setFont(new Font("Tahoma", Font.PLAIN, 12)); 
     //for adding the button to the panel 
     centerPanel.add(printButton, BorderLayout.NORTH); 
     //for adding the scrollpane to the panel 
     centerPanel.add(scrollPane, BorderLayout.CENTER); 
     //for setting the border to the panel 
     centerPanel.setBorder(BorderFactory.createTitledBorder("Books:")); 
     //for adding the panel to the container 
     cp.add("Center", centerPanel); 

     //for adding the actionListener to the button 
     printButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent ae) { 
       Thread runner = new Thread() { 
        public void run() { 
         try { 
          PrinterJob prnJob = PrinterJob.getPrinterJob(); 
          prnJob.setPrintable(new PrintingBooks(DEFAULT_QUERY)); 
          if (!prnJob.printDialog()) 
           return; 
          setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); 
          prnJob.print(); 
          setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 
         } 
         catch (PrinterException ex) { 
          System.out.println("Printing error: " + ex.toString()); 
         } 
        } 
       }; 
       runner.start(); 
      } 
     }); 
     //for setting the visible to true 
     setVisible(true); 
     //to show the frame 
     pack(); 
    } 
} 

我有以下錯誤

值java.sql.SQLException:[微軟] [ODBC驅動程序管理器]無效的字符串 或緩衝區長度

+2

代碼中的哪個地方是生成的異常? – MadProgrammer 2014-09-04 06:24:33

+0

不知道你的意思是 – 2014-09-04 06:26:52

+0

發佈詳細的堆棧跟蹤。 – ferrerverck 2014-09-04 06:28:56

回答

0

你應該試試這個

for (int i = 0; i < 9; i++) 
    column = table.getColumnModel().getColumn(i+1); 

,而不是這個

for (int i = 0; i < 9; i++) 
    column = table.getColumnModel().getColumn(i); 

也許它的工作原理。

+0

不能正常工作.... 感謝您的幫助^ _ ^ – 2014-09-04 07:29:41