File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 14107
    
  16

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: 30294
    
150

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: 60997
    
  65

Be aware that CachedRowset is only available with Java 1.5.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Navigate through ResultSet