Link to home
Start Free TrialLog in
Avatar of stephan_zehnder
stephan_zehnder

asked on

How to check if a ResultSet contains no data

How can I check if a ResultSet contains data or not?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The commo use is:

  ResultSet rs = preparedStatement.executeQuery();
  while ( rs.next() ) {
       // process the result set data
  }
Avatar of stephan_zehnder
stephan_zehnder

ASKER

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())) ??
>> 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.


SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
I think the answer I gave was quite correct, isn't it?