• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How do you check is Result set is null

 
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i use this, it says
isBeforeFirst() && isAfterLast() for java.sql.resultset with arguments() is not defined
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 567
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
author
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic