Make sure ResultSet is empty in Java

I use HSQLDB in my program. I want to check if my result set is empty or not.

//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 correctly. According to this post, I need to call .first() or .beforeFirst() to place the cursor on the first line, but .first() and .beforFirst() not supported in HSQL. I also tried adding connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); but I still get the same result (I get the message empty and the data from the database !!!) What am I doing wrong here?

+6
source share
1 answer

If I understand your purpose, you can use the do while

 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 just save count so

 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 } 
+11
source

Source: https://habr.com/ru/post/971315/


All Articles