我有一个将从公司表中获取行并将其插入JComboBox的代码。
当APP在调试模式下运行时,结果集中将填充数据。但是在正常执行时,结果集为空!
我正在使用Netbeans IDE 7.0.1
进行开发,并使用phpmyadmin mysql数据库版本5.1.37
。
下面是我的代码:
boolean isvalue = false; // variable to identify if the company name found or not.
ResultSet rs = null;
try {
st = con.createStatement();
if(con == null) {
logger.error("Database Connection Not available.");
throw new NullPointerException();
}
//Set the company name to combo box
rs = st.executeQuery("Select comp_name from company");
while (rs.next()) {
comboCompanyName.addItem(rs.getString("comp_name"));
isvalue = true; //Set true if found
}
} catch (SQLException ex) {
System.out.println("SQLError found while updating information." + ex.getMessage());
} catch (Exception ex) {
System.out.println("Error found while updating information." + ex.getMessage());
}
if (!isvalue) //Check company information available
{
JOptionPane.showMessageDialog(null, "System could not found any company information.", "Error", JOptionPane.WARNING_MESSAGE);
}
帮助我。
提前致谢。
最佳答案
除了使用value变量,还可以尝试:
PreparedStatement statement = con.prepareStatement("SELECT comp_name FROM company");
ResultSet result = statement.executeQuery();
int i=0;
while (result.next()) {
jComboBox1.addItem(result.getString(1));
i++;
}
if(i==0){
JOptionPane.showMessageDialog(null, "System could not found any company information.", "Error",JOptionPane.WARNING_MESSAGE);
}