Solved

How to find out if there a Database ResultSet contains results

Posted on 1998-07-31
3
196 Views
Last Modified: 2010-03-30
I'm developing a servlet and I need to test whether there are any results in the ResultSet returned from my database query, if there aren't any I want to display an html page which indicates this.
What is the method to determine if there are results, I don't want to use 'next()' as this moves the resultset counter on one.

Please send me the code.

Thanks

Mel
0
Comment
Question by:help_me
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1229589
I am afraid you have to call next() and cache the row values if the set is not empty.
0
 
LVL 2

Accepted Solution

by:
shchuka earned 50 total points
ID: 1229590
There's no other way to find out if the ResultSet is empty as to call the next() method.  Here's what you can do:

ResultSet = db.executeQuery(...);
if(rs == null) {
    //Error in the query string
}
if(!rs.next()) {
    //ResultSet is empty - do whatever you need
} else {
    do {
        //Do whatever you need with each row
    } while(rs.next());
}

This way you don't loose the first record by using next() to figure out whether the recordset is empty.
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1229591
Statement.executeQuery() never returns null, a ResultSet object (possibly empty) is always returned.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question