| Author |
Navigate through ResultSet
|
Indika Hewage
Ranch Hand
Joined: May 18, 2004
Posts: 35
|
|
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
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
What do you mean by "... is not working"? Does previous() return false? Do you get an exception?
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Annie Smith
Ranch Hand
Joined: Mar 05, 2005
Posts: 172
|
|
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
|
Cheers!<br /><b>Annie</b>
|
 |
Indika Hewage
Ranch Hand
Joined: May 18, 2004
Posts: 35
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26173
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Indika Hewage
Ranch Hand
Joined: May 18, 2004
Posts: 35
|
|
|
Thanks all. I use ArrayList.
|
 |
Annie Smith
Ranch Hand
Joined: Mar 05, 2005
Posts: 172
|
|
|
You might even take a look at CachedRowset; it is a disconnected rowset.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56179
|
|
|
Be aware that CachedRowset is only available with Java 1.5.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: Navigate through ResultSet
|
|
|