File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes ResultSet check if there are any values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ResultSet check if there are any values" Watch "ResultSet check if there are any values" New topic
Author

ResultSet check if there are any values

Reggie McDougal
Ranch Hand

Joined: Sep 27, 2004
Posts: 69
I need to check a ResultSet to see if it contain any rows, I tried rs.next() method but this is unreliable first() thows an exception on a forward coursor

I just need to check if the rs contains any rows at all if any one can help.

Reg


You can never drink too much
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

I tried rs.next() method but this is unreliable


How so?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bob Rocks
Greenhorn

Joined: Oct 21, 2004
Posts: 23
if you use rs.next() and the result set is null, you will probably need to use a do while instead of just a while for the loop... never had a problem myself though, maybe if we had some more info on your specific problem.
Rudy Simon Yeung
Greenhorn

Joined: Jun 06, 2003
Posts: 15
See the code snippets and it works for me.

Statement st1 = db2Conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
System.out.println("Search Begin: " + new java.util.GregorianCalendar().getTime());
ResultSet resultSet1 = null;
try {
resultSet1 = st1.executeQuery(formatSQLQuery(aContext, REMITCCYIND_DONT_CARE, null));
} catch (java.sql.SQLException e)
{
releaseResources(resultSet1, st1, db2Conn);
throw new java.sql.SQLException();
}

if (isToBank)
{
if (resultSet1.first()) // CBID code found in branch file
{
.....

and you can use beforeFirst() to reset the pointer of the result set.

Rudy
[ November 10, 2004: Message edited by: Rudy Simon Yeung ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30146
    
150

Reggie,
rs.next() is fine and the recommended way of doing this:

Note that a database will never return null for the resultset after doing a query. It will return an empty resultset.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Reggie McDougal
Ranch Hand

Joined: Sep 27, 2004
Posts: 69
Thats what I was doing but the weirdest thing, the recordSet would not return all the records that met the criteria,

I mean I execute the query then check it with rs.next() and oracle would just return some of the records not all, not to mention the fact that if there is data it skips the first record, which I don't want.

So I reckon Rudy has the right Idea beforeFirst().

I'm using a while(rs.next()) and writing the data to an ArrayList to get the number of values that way and it works perfectly.

Thats what I meant by unreliable.

Reg

[ November 10, 2004: Message edited by: Reggie McDougal ]
[ November 10, 2004: Message edited by: Reggie McDougal ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ResultSet check if there are any values
 
Similar Threads
Problem with rs.isAfterLast() and rs.isFirst()
what is fastest way to retrive rows from a resultSet?
Size of ResultSet
Record Count
ResultSet