• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why ResultSet call previous() will throw SQLException?

 
Zhixiong Pan
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
I want to check if my ResultSet instance is empty of not, so I code this: if(!rs.next()) throw new EmptyWallException(); but that step will moves the cursor 1 row. In order to pull the cursor back, so I write expression rs.previous() and this will throw SQLException. At last I will have to re-create the ResultSet. Can anyone tell my why? Thank you.
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Zhixiong Pan:
Hi ranchers,
I want to check if my ResultSet instance is empty of not, so I code this: if(!rs.next()) throw new EmptyWallException(); but that step will moves the cursor 1 row. In order to pull the cursor back, so I write expression rs.previous() and this will throw SQLException. At last I will have to re-create the ResultSet. Can anyone tell my why? Thank you.


Per default result sets are forward scroll only. If you want a scrollable result set, you must use a different createStatement method and specify that. Alternatively you can determine after the fact whether the set was empty or not by using a boolean set within the while(rs.next( ) ) loop, or, if you save information into arrays, or whatever, inside of the while loop, and checking, after the loop, whether the boolean was set, or data changed/added.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One question I have to wonder is, what are you trying to with the result set? If I understand it, you want to check if a resultset is empty but not actually read from the result set? In that case you want to pass around a result set?

Usually, when a query is executed, the data is read from the resultset right away since this not an object that can be easily passed around. What I recommend doing is reading all of the data of the result set into some simple array of POJOs then passing that array around instead of the resultset.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic