java: testing empty resultset

I really don’t understand why the java.sql.ResultSet don’t provide an isEmpty() method or something similar and it’s true that I rarely need this method, however, sometimes this happen. Here is how I test for empty resultset in java.

ResultSet rs = ...
boolean  emptyRs = true;

while(rs.next()){
   emptyRs = false;
   ...
}

if(emptyRs){
   ...
}

or better (i prefer this one)

ResultSet rs = ...

if(rs.next()){
   do{
      ...
   }while(rs.next());
}else{
   //is empty
}
Advertisements

2 thoughts on “java: testing empty resultset

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s