Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to check if a ResultSet contains no data

Posted on 2005-05-04
7
Medium Priority
?
183 Views
Last Modified: 2010-03-31
How can I check if a ResultSet contains data or not?
0
Comment
Question by:stephan_zehnder
  • 5
7 Comments
 
LVL 37

Accepted Solution

by:
zzynx earned 900 total points
ID: 13924579
if ( !rs.next() ) {
   // no Data
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13924586
The commo use is:

  ResultSet rs = preparedStatement.executeQuery();
  while ( rs.next() ) {
       // process the result set data
  }
0
 

Author Comment

by:stephan_zehnder
ID: 13924619
if ( !rs.next() ) {
   // no Data
}

isn't there the possibility, that the cursor is on the first one. so even if !rs.next() returns true there could be one row?
i use it for a unit test, so no need to process data, because there shouldn't be.

maybe this
((!rs.next())&&(!rs.isFirst())) ??
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 37

Expert Comment

by:zzynx
ID: 13924660
>> isn't there the possibility, that the cursor is on the first one. so even if !rs.next() returns true there could be one row?
No. I quote from the API docs:

A ResultSet object maintains a cursor pointing to its current row of data.
Initially the cursor is positioned before the first row.
The next method moves the cursor to the next row,
and because it returns false when there are no more rows in the ResultSet object,
it can be used in a while loop to iterate through the result set.


0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 600 total points
ID: 13924721
When you obtain a ResultSet just after executing a query, the cursor would always be just before the first row. Calling next () would try to advance it and return true of false, depending upon whether advancement is possible or not. If it is not possible (no more rows), it would return false. So zzynx's approach is correct.

>> so even if !rs.next() returns true there could be one row?

You mean that the result-set contained one row and has already been advanced to the first row previously. Then you are calling rs.next () again? Well, that would return false. But if you want to check if a ResultSet is empty just after getting it by shooting a query, the approach mentioned is the way to go.

Unfortunately, ResultSet does not give you a method to determine the size or count. However, if you go for a CachedRowSet (which is like a disconnected result-set), you have a size () method which you can use.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14034521
Thanks for accepting.

For my interest: Could you tell me what you missed in my comment for not awarding it with an A?
For your interest: You can always ask for more explanation if you're not completely satisfied with a comment.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 14034643
I think the answer I gave was quite correct, isn't it?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 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