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.
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
posted
0
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
posted
0
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
posted
0
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
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
posted
0
if i use this, it says isBeforeFirst() && isAfterLast() for java.sql.resultset with arguments() is not defined
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
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
posted
0
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
posted
0
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
posted
0
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.