aspose file tools*
The moose likes JDBC and the fly likes Navigate through ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Navigate through ResultSet" Watch "Navigate through ResultSet" New topic
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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 8 API documentation
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
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30762
    
156

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: 61426
    
  67

Be aware that CachedRowset is only available with Java 1.5.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Navigate through ResultSet