This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Servlets and the fly likes how to test if resultset is empty Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "how to test if resultset is empty" Watch "how to test if resultset is empty" New topic
Author

how to test if resultset is empty

Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
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

Joined: Jan 24, 2001
Posts: 476
I think you can do something like this:
ResultSet ResSet = con.executeQuery(sql);
boolean more = ResSet.next();
if(more){ // or while(more)
.....
.....
}
.....


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
in previous, if(more) returns false then your ResultSet is empty.
Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
Hey thanks Alex!
That did the trick!
Ben Roy
Ranch Hand

Joined: Nov 01, 2000
Posts: 70
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

Joined: Jan 24, 2001
Posts: 476
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"
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: how to test if resultset is empty
 
Similar Threads
Testing for empty ResultSet
slowness in the Arraylist adding
JSP what's wrong with following code?
How to find null (ResultSet + PreparedStatement))
Counting number of records in result set