wood burning stoves 2.0*
The moose likes JDBC and the fly likes Problem with rs.isAfterLast() and rs.isFirst() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem with rs.isAfterLast() and rs.isFirst()" Watch "Problem with rs.isAfterLast() and rs.isFirst()" New topic
Author

Problem with rs.isAfterLast() and rs.isFirst()

Kalai Selvi
Ranch Hand

Joined: Jan 08, 2007
Posts: 45
I am using jdbc to connect to oracle 10g. I can connect to the db and get the resultset. I have 2 rows in my table. I am able to use resultsetmetadata on the resultset.
But, when I try to retrieve the values from the resultset, I get the following exception

"java.sql.SQLException: Result set is in an invalid state. May be before the first row or after the last row."

When I try to use various methods in the resultset for checking, I find isAfterLast and isFirst both methods returning true. I am not able to understand, where the cursor points to now. I am using scrollable resultset. My resultset is not null.

rs.isAfterLast() true
rs.isFirst() true

Can somebody help me ?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
You at least need to call ResultSet#next() to set the cursor to the firstnext valid state.

If it in no way returns true, then the ResultSet simply doesn´t contain any rows. Which means that either the query is bogus or that the table in question doesn´t contain anything matching the query.
Kalai Selvi
Ranch Hand

Joined: Jan 08, 2007
Posts: 45
If the resultset doesnot contain any rows, then what would it return if we check

if (rs != null)

In my case, this returns true. Is it possible, this returns true even if there are no rows in my resultset?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You'll notice that the API documentation for the executeQuery() method of Statement says:

Returns:
a ResultSet object that contains the data produced by the given query; never null

So don't waste your time testing to see if it's null. But even so, if you have a ResultSet object (which is what the != null test tells you that you do) there's no reason why it shouldn't contain zero rows. Don't confuse "null" with "empty".
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Kalai Selvi wrote:If the resultset doesnot contain any rows, then what would it return if we check

if (rs != null)

In my case, this returns true. Is it possible, this returns true even if there are no rows in my resultset?


As said, this is totally wrong. The obtained ResultSet is never ever null.

Just check if ResultSet#next() returns true. You normally do that in an if or a while statement.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem with rs.isAfterLast() and rs.isFirst()