This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes how do you fetch a limited # of rows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how do you fetch a limited # of rows" Watch "how do you fetch a limited # of rows" New topic
Author

how do you fetch a limited # of rows

Qaiser Chaudhry
Greenhorn

Joined: Apr 25, 2001
Posts: 10
I am trying to do research on how to fetch a limited number of rows. The problem that's prompting me to do this is that I am working with Tomcat (with Sybase) and when multiple users fetch a page with a list on it, a large number of records are loaded for each user. What I am trying to achieve is fetching only the records that will fit on that page. Can this be achieved by using cursors? Please help.
raymond yadao
Ranch Hand

Joined: Jan 29, 2001
Posts: 88
The ResultSet has a method setFetchSize() which you can use to specify the number of rows you want to fetch.
hth...
raymond
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
This method may not be supported by all JDBC drivers. It depends on the database you may be using.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Qaiser Chaudhry
Greenhorn

Joined: Apr 25, 2001
Posts: 10
I have already tried setFetchSize(x), but that means that when you do a select statement, you will the first x number of records that show up in the resultset. How do I get to the next set of x number of records? I would like to be able to get to any point in the the resultset and pick out x number of records.
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
you need to ask yourself if you want to fetch all the records and display some at a time, or fetch some and display them , then fetch the next or previous set if needed.
if you want to fetch all then yes the cursor will achieve this but the other records may never be requested and resources will have been wasted.
if you want to get some only then you'll need to know where you last stopped.
i recently fixed a similar problem, the jsp was sent 20 megs to load at once, hehe, so i had the session hold an int (offset) for the last record he saw, and if he wanted the next or previous 20 i'd setMaxRows(20) and start fetching based on the offset.
theres probably a better way to do it but i couldn't think of it so thats what i did.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how do you fetch a limited # of rows
 
Similar Threads
lazily loaded scrollable table
pagination for forEach of JSTL List
Retreiving 20 rows per page
best practice for a sql script for pagination
Fetching size limitted rows from a large database table