File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases 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
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "how do you fetch a limited # of rows" Watch "how do you fetch a limited # of rows" New topic

how do you fetch a limited # of rows

Qaiser Chaudhry

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.
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.

So much trouble in the world -- Bob Marley
Qaiser Chaudhry

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.
I agree. Here's the link:
subject: how do you fetch a limited # of rows
It's not a secret anymore!