运行java代码时无法从mysql数据库检索行
我有一个代码,它将从company表中获取行并插入JComboBox 当应用程序在调试模式下运行时,结果集将填充数据。但在正常执行时,结果集为空 我正在使用Netbeans IDE 7.0.1进行开发,并使用phpmyadmin mysql数据库版本5.1.37 下面是我的代码:运行java代码时无法从mysql数据库检索行,java,mysql,debugging,Java,Mysql,Debugging,我有一个代码,它将从company表中获取行并插入JComboBox 当应用程序在调试模式下运行时,结果集将填充数据。但在正常执行时,结果集为空 我正在使用Netbeans IDE 7.0.1进行开发,并使用phpmyadmin mysql数据库版本5.1.37 下面是我的代码: boolean isvalue = false; // variable to identify if the company name found or not. ResultSet r
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);
}
帮我摆脱困境。
提前感谢。安装使用isvalue变量您可以尝试:
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);
}
我发现了这个问题
由于连接未正确关闭而导致的问题。i、 e以前的连接未正确关闭,而且我使用了静态连接对象。comboCompanyName.addItemrs.getStringcomp_name之后的任何其他代码;对于comboCompanyName?您的null检查是在访问变量之后进行的,因此您不打算记录任何内容。消息“未找到数据”来自何处?我在你的代码中找不到。我刚刚编辑了一点这个问题。。。请看一看正常模式下是否有异常?@Rajshri当我运行此代码时,它总是显示系统找不到任何公司信息。在JOptionPane中。代码不会产生任何显式错误或异常。