Win a copy of Java Challengers this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

How to check record exists

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
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
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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried rs.next() but still goes into code block and giving invalid cursor state.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic