2011-08-09 41 views
0

我知道如何从数据库中的数据的Java应用程序,但我的问题是增加了数据的JComboBox,我这样做: main.java:设置项目的JComboBox

`Interface itself = new Interface(); 
    itself.setComboBoxItems();` 

//调用该方法setComboBoxItems

这里是法setComboBoxItems():

public void setComboBoxItems() { 
      ResultSet rset = null; 
      Statement stmt = null; 
      Connection connect = null; 
      try { 
       stmt = connect.createStatement(); 
       rset = stmt.executeQuery("SELECT * FROM DB_Library.dbo.categories"); 
       while(rset.next()) { 
        String comboItem = rset.getString("categoryName"); 
        System.out.print("now combobox items will run!"); 
        categoriesComboBox.addItem(comboItem); 
       } 
      } catch (SQLException ex) { 
       System.out.print("error from set ComboBox: "); 
       Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } 

但它返回这个错误对我来说,我解决不了,

+2

你会得到什么错误? – u449355

+0

告诉我们'ex.printStackTrace()' – oliholz

+0

'接口本身=新接口();'使小猫哭。 – Qwerky

回答

1

因此,它看起来像你没有得到一个连接:

Connection connect = null; 
    try { 
     stmt = connect.createStatement(); 

这里是连接仍然是零。不是吗?

+0

谢谢安德烈!你是对的,但它仍然没有出现在JComboBox上! – user743473

+0

@ user743473,你检查,结果集是否包含某些内容或它是空的? –