Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Navigate through ResultSet

 
Indika Hewage
Ranch Hand
Posts: 35
  • 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
Sheriff
Posts: 15752
74
Android IntelliJ IDE Java Scala Spring
  • 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
  • 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
  • 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
Sheriff
Posts: 35976
422
Eclipse IDE Java VI Editor
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all. I use ArrayList.
 
Annie Smith
Ranch Hand
Posts: 172
  • 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
Posts: 65657
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Be aware that CachedRowset is only available with Java 1.5.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!