我知道如何从数据库获取数据到Java应用程序,但是我的问题是将数据添加到JComboBox,我这样做是:
main.java:

`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);
            }
        }


但它为我返回了我无法解决的错误,

最佳答案

因此,您似乎没有连接:

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


这里的连接仍然为空。是不是

关于java - 设置项目到jComboBox,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6994222/

10-08 23:03