| Author |
Why ResultSet call previous() will throw SQLException?
|
Zhixiong Pan
Ranch Hand
Joined: Jan 25, 2006
Posts: 239
|
|
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.
|
SCJP 1.4 SCJD
|
 |
Martin Simons
Ranch Hand
Joined: Mar 02, 2006
Posts: 196
|
|
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
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
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.
|
My Blog: Down Home Country Coding with Scott Selikoff
|
 |
 |
|
|
subject: Why ResultSet call previous() will throw SQLException?
|
|
|