This week's giveaways are in the MongoDB and Jobs Discussion forums.
We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line!
See this thread and this one for details.
The moose likes JDBC and the fly likes How do you check is Result set is null Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "How do you check is Result set is null" Watch "How do you check is Result set is null" New topic
Author

How do you check is Result set is null

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
if (rs != null)
{
out.println("result set has got something");
while (rs.next())
{ //I am processing result set now
}
}
else
{
out.println("result set is empty");
}

IS THIS CORRECT WAY TO CHECK IF RESULT SET IS EMPTY,
FOR ME EVEN THOUGH RESULT SET IS EMPTY IT IS GOING
INTO FIRST BLOCK AND PRINTING
RESULT SET HAS GOT SOMETHING.
CAN SOMEONE ADVISE ME ON THIS

Bhasker Reddy
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
I think there is a difference between resultset being empty & resultset being null.
If the resultset is empty it means that no rows were returned for the query & thats the reason the following line is executed.
out.println("result set has got something");
But I am sure "while (rs.next())" statement will print ("result set is empty");

Originally posted by Bhasker Reddy:
if (rs != null)
{
out.println("result set has got something");
while (rs.next())
{ //I am processing result set now
}
}
else
{
out.println("result set is empty");
}

IS THIS CORRECT WAY TO CHECK IF RESULT SET IS EMPTY,
FOR ME EVEN THOUGH RESULT SET IS EMPTY IT IS GOING
INTO FIRST BLOCK AND PRINTING
RESULT SET HAS GOT SOMETHING.
CAN SOMEONE ADVISE ME ON THIS

Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1241
Agree with Roopa.
you can try the method
boolean isEmpty(); in the Set interface (actually it is from the collection interface).
boolean isEmpty() Returns true if this set contains no elements.

Hope this help.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
JDBC is not part of the Programmer Certification objectives.
Sorry guys I'm moving this to JDBC forum..

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 564
if you used the CODE tags and indented, you would see that it's not doing anything if rs.next() is false.


there are 2 solutions i use:

or this:


I have seen things you people would not believe, attack ships on fire off the shoulder of Orion, c-beams sparkling in the dark near the Tennhauser Gate. All these moments will be lost in time, like tears in the rain.
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
if i use this, it says
isBeforeFirst() && isAfterLast() for java.sql.resultset with arguments() is not defined
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

You can have a counter that will count the number of records processed. If the counter remains at 0, no records were returned otherwise the counter will have the number of records returned:

There is more examples back about 7 posts with pretty much the same topic
Jamie
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

Just a note:
"if (rs != null)" --> This does not indicate whether or not a resultSet is empty or not. No matter how many rows are returned(starting from 0), the resultSet will never be null. The only case in which a resultset will remain null is when an exception is thrown... but the program flow will jump to the Exception handling block anyways. So what is the purpose of "if (rs != null)"?
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 564
if (rs != null)

means that the
stmt.executeQuery("SELECT a, b FROM TABLE2");
failed - but you're probably right - it would fail with an exception and never get there.
the reason why i do the check is because my resultSet is returned from a method, and if it's null, it really means the method failed. kinda stupid, but what the heck.
Adam Hardy
Ranch Hand

Joined: Oct 09, 2001
Posts: 564


I posted this code saying it was a way of seeing if a resultSet has no rows.
I just tested it and found out that i was talking cr*p - sorry for misleading anyone. If however you are using ADO in ASP or VB, that is what you do to check a recordset for zero rows.
It seems that these java methods on the resultSet return false if the resultSet has no rows. as is probably logical since you can't be beforeFirst when there is no first.
David Peterson
author
Ranch Hand

Joined: Oct 14, 2001
Posts: 154
If you use a do..while loop instead of a normal while loop, you can use rs.next() to check to see if there are any rows.

David
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: How do you check is Result set is null
 
Similar Threads
How do you check is Result set is null
tomcat with jdbc
Action form: data getting lost between form submit and setters on ActionForm
Null vs empty string
Action problem