File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Navigate through ResultSet

 
Indika Hewage
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I use the following code to navigate through ResultSet. Navigate to previous record is not working when I move to last record (using 'rs.next()').
I think I missed out something here.
Basically I need to navigate through ResultSet first,previous,next,last. I would appreciate if someone can provide an example.

Thanks.


======================================
con = getConnection();
stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
if (rs != null)
rs.close();
rs = stmt.executeQuery("SELECT * FROM Department");
=================================================
For the next record
if (rs.next()){
idField.setText(rs.getString(1));
depField.setText(rs.getString(2));
}

For the previous record

if (rs.previous()){
idField.setText(rs.getString(1));
depField.setText(rs.getString(2));
}
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "... is not working"?
Does previous() return false?
Do you get an exception?
 
Annie Smith
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your case, rs.previous() would work if there is a previous record... I mean if you have just obtained a ResultSet from query execution, rs.previous() would not have any effect. You might try using
 
Indika Hewage
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have First,Previous,Next and Last buttons to navigate through the ResultSet. I would appreciate if anyone can provide the code for the each move (ie. First,Previous,Next and Last).


Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33699
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indika,
Do you have a resultset open while the page is rendered to the user? Unless it is a disconnected resultset, it is likely timing out and becoming stale.

I recommend copying the data into a list (like an ArrayList) and using that for paging.
 
Indika Hewage
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. I use ArrayList.
 
Annie Smith
Ranch Hand
Posts: 172
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might even take a look at CachedRowset; it is a disconnected rowset.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that CachedRowset is only available with Java 1.5.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic