• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to test if resultset is empty

 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having problems finding the right syntax. How would you test to see if a resultset is empty?
if(result= what?)
I've tried null and "" but it's not the right type. What is the correct way to do this?
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can do something like this:
ResultSet ResSet = con.executeQuery(sql);
boolean more = ResSet.next();
if(more){ // or while(more)
.....
.....
}
.....
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in previous, if(more) returns false then your ResultSet is empty.
 
Annette L'Heureux
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks Alex!
That did the trick!
 
Ben Roy
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just thought I'd toss in my 2 cents and add that you can skip one of the steps.
if (result.next())
{
}
In particular, I don't think you'd want to do this:
boolean more=result.next()
while(more)
{
}
since you could very well end up in a loop. Usually you see:
while(result.next())
{
}
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
Why would you end up in the loop? When you say while(result.next()), you check wheather the value is true or false anyway. If you do:
boolean more = result.next();
you save your self space and time so you don't have to type result.next().
Don't you agree?
------------------
Alex
"Java is the answer"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic