Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Kalai Selvi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic