How to check if a ResultSet contains no data

How can I check if a ResultSet contains data or not?
stephan_zehnderAsked:
Who is Participating?
 
zzynxConnect With a Mentor Software engineerCommented:
if ( !rs.next() ) {
   // no Data
}
0
 
zzynxSoftware engineerCommented:
The commo use is:

  ResultSet rs = preparedStatement.executeQuery();
  while ( rs.next() ) {
       // process the result set data
  }
0
 
stephan_zehnderAuthor Commented:
if ( !rs.next() ) {
   // no Data
}

isn't there the possibility, that the cursor is on the first one. so even if !rs.next() returns true there could be one row?
i use it for a unit test, so no need to process data, because there shouldn't be.

maybe this
((!rs.next())&&(!rs.isFirst())) ??
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
zzynxSoftware engineerCommented:
>> isn't there the possibility, that the cursor is on the first one. so even if !rs.next() returns true there could be one row?
No. I quote from the API docs:

A ResultSet object maintains a cursor pointing to its current row of data.
Initially the cursor is positioned before the first row.
The next method moves the cursor to the next row,
and because it returns false when there are no more rows in the ResultSet object,
it can be used in a while loop to iterate through the result set.


0
 
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
When you obtain a ResultSet just after executing a query, the cursor would always be just before the first row. Calling next () would try to advance it and return true of false, depending upon whether advancement is possible or not. If it is not possible (no more rows), it would return false. So zzynx's approach is correct.

>> so even if !rs.next() returns true there could be one row?

You mean that the result-set contained one row and has already been advanced to the first row previously. Then you are calling rs.next () again? Well, that would return false. But if you want to check if a ResultSet is empty just after getting it by shooting a query, the approach mentioned is the way to go.

Unfortunately, ResultSet does not give you a method to determine the size or count. However, if you go for a CachedRowSet (which is like a disconnected result-set), you have a size () method which you can use.
0
 
zzynxSoftware engineerCommented:
Thanks for accepting.

For my interest: Could you tell me what you missed in my comment for not awarding it with an A?
For your interest: You can always ask for more explanation if you're not completely satisfied with a comment.
0
 
zzynxSoftware engineerCommented:
I think the answer I gave was quite correct, isn't it?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.