• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to check record exists

 
Vasudevan Gopalan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried rs.next() but still goes into code block and giving invalid cursor state.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic