我知道如何从数据库获取数据到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);
}
}
但它为我返回此错误,我无法解决,
答案 0 :(得分:1)
所以,看起来你没有得到连接:
Connection connect = null;
try {
stmt = connect.createStatement();
这里的连接仍然是null。不是吗?