| Author |
How to check record exists
|
Vasudevan Gopalan
Ranch Hand
Joined: Aug 29, 2003
Posts: 67
|
|
Hi all, How to check whether resultset is retrieving records or not?.I tried by giving if(rs!=null) or if(rs.next()) Still it goes into the loop and giving invalid cursor state when there are no records. Help me please
|
 |
Chris Brat
Ranch Hand
Joined: May 22, 2003
Posts: 108
|
|
Hi, I would think that if(rs != null){ // code block } would send you into the code block ... isnt there always a resultset returned even if there are no records. but i would also think that if (rs.next()){ // code block } SHOULD not let you into the code block if you have no results.
|
SCJP 1.2, SCJP 5, SCBCD
|
 |
Jamie Robertson
Ranch Hand
Joined: Jul 09, 2001
Posts: 1879
|
|
Usually, I want to track the number of records found, so I use: If I only want to check if a record exists, or expect only one row back: there are many other ways as well, but these two should get you started. Jamie
|
 |
Jamie Robertson
Ranch Hand
Joined: Jul 09, 2001
Posts: 1879
|
|
also, note that will never be true. The executeQuery() method always return a ResultSet object or throws a SQLException which will cause it to immediately jump to the exception block of code anyways. Jamie
|
 |
Vasudevan Gopalan
Ranch Hand
Joined: Aug 29, 2003
Posts: 67
|
|
|
I tried rs.next() but still goes into code block and giving invalid cursor state.
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15229
|
|
If you could please show us the entire code block and not just one line, then we might be able to help better. Keep in mind that if you are still doing this: Because of the (rs != null) part, it will drop into the block because, as Jamie already mentioned, executeQuery will always return a ResultSet object or throw an exception.
|
 |
Vasudevan Gopalan
Ranch Hand
Joined: Aug 29, 2003
Posts: 67
|
|
Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs =st.executeQuery("union query here"); //which retrieves more than one record if (rs.next()==true) { rs.beforeFirst(); while(rs.next()) { .... } }
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15229
|
|
Originally posted by Vasudevan Gopalan: Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); ResultSet rs =st.executeQuery("union query here"); //which retrieves more than one record if (rs.next()==true) { rs.beforeFirst(); while(rs.next()) { .... } }
What if you don't use the IF statement at all? What if you just use the while(rs.next()) { } Unless you have an ELSE clause with your IF, there is no need for the IF.
|
 |
Jamie Robertson
Ranch Hand
Joined: Jul 09, 2001
Posts: 1879
|
|
Also, in your while block, do not reuse the same statement that you created your resultset with or the resultset will also be closed automatically. eg. also, please use the code tags to keep your code readable to others! Jamie
|
 |
 |
|
|
subject: How to check record exists
|
|
|