• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why ResultSet call previous() will throw SQLException?

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic