Check whether the resultset in Java is empty
•
Java
See English answers > java resultset how to check if there are any results21
//check if empty first
if(results.next() == false){
System.out.println("empty");
}
//display results
while (results.next()) {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
}
The above method does not work properly According to this post, I have to call First () or Beforefirst() to stop the cursor at the first line, but hsql does not support it First () and beforFirst(). I also tried to add connection createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); But I still get the same result (I get that the message is empty and the data is from DB!) What did I do wrong here?
Solution
If I understand your goal, you can use the do while loop
if (!results.next()) {
System.out.println("empty");
} else {
//display results
do {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
} while (results.next());
}
Or you can keep this count,
int count = 0;
//display results
while (results.next()) {
String data = results.getString("first_name");
//name.setText(data);
System.out.println(data);
count++;
}
if (count < 1) {
// Didn't even read one row
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
