gpt4 book ai didi

java.sql.SQLException : Illegal operation on empty result set. Else 语句不起作用

转载 作者:行者123 更新时间:2023-11-30 02:28:50 37 4
gpt4 key购买 nike

try {
//Class.forName("org.h2.Driver");
//Connection conn = DriverManager.getConnection("jdbc:h2:~/test","admin","admin");
String m=textField_1.getText();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/logindetails","root","root");
//Connection conn1 = DriverManager.getConnection("jdbc:mysql://localhost/logindetails","root","root");


String query1="Select * from Details where Username='"+m+"'";
PreparedStatement statement1 = conn.prepareStatement(query1);
ResultSet res=statement1.executeQuery(query1);
res.next();

String user;
user=res.getString("Username");

{

if(m.compareTo(user)==0)
{

JOptionPane.showMessageDialog(null, "CONNECTION SUCCESSFULL & DATA SAVED");

DataEntry.this.dispose();
login ing= new login();
ing.setVisible(true);



}

else
{

String query = "insert into Details values(?,?,?,?,?)";
PreparedStatement statement=conn.prepareStatement(query);
statement.setString(1, textField.getText());


statement.setString(2, textField_2.getText());
statement.setString(3, textField_3.getText());
statement.setString(4, textField_1.getText());
statement.setString(5,String.valueOf(passwordField.getPassword()));
statement.execute();

}


}



}

在这个 else 部分不起作用。它抛出错误“对空结果集进行非法操作”。我还交换了两个 if-else 语句的主体,但这对我没有帮助。If 语句主体工作正常。我认为错误是 ResultSet 语句。提前致谢。

最佳答案

您没有检查 ResultSet 是否包含结果。因此,在空 ResultSet 上调用 res.getString("Username") 时会出现异常。

更改:

res.next();

String user;
user=res.getString("Username");

至:

String user = null;
if (res.next())
user=res.getString("Username");

然后,代替:

if(m.compareTo(user)==0) {
...
}

写:

if (user != null) {
...
}

关于java.sql.SQLException : Illegal operation on empty result set. Else 语句不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44778067/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com