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 Fetch only  'n' records from the ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Fetch only  Watch "Fetch only  New topic
Author

Fetch only 'n' records from the ResultSet

Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
I wanted my Resultset to get only the last 'n' records from the resultSet.

Say for Example, My Query Return 10 rows and i wanted to display only the last 4 records of the query.
Sachin Joshi
Ranch Hand

Joined: Mar 23, 2004
Posts: 43
Originally posted by Ameya Thakur:
I wanted my Resultset to get only the last 'n' records from the resultSet.

Say for Example, My Query Return 10 rows and i wanted to display only the last 4 records of the query.


Hi Ameya,

This can be done in java as well as on DB side. Which way you want to do it depends on your total number of records and some other factors.

1. In java approach, fetch all records from DB. Then using ScrollableResultSet, display only certain number of records each time.

2. In DB approach, write a query such that you will only fetch those records which are to be displyed. Here, for showing next set of records, you have to query the DB again. I have not used this approach much, but these queries are usually written using default "ROWNUM" field (I am talking about Oracle).

I Hope this will help.

Sachin
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Dear Sachin,

Thanks for your help, But this solution wont solve my problem.

As i am using Scrollable ResultSet the ResultSet is Fetching me 10 rows
and i have a field in the Table called as Concurrency which tells me how many time the record has been updated.

So Depending upon the Concurrency Count i want to get those Many Records

for which i am doing is

if (lConCurrencyCount >=1 && lResultSet!=null)
{
lResultSet.last();
while(ltempCurrencyCount!=0)
{
lResultSet.getString("AA");
}
}

Now the Data which i am getting is in the reverse order(10th record first the 9th & so on which i need to chnage).
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
To pull the data in the order you need can you not do an "ORDER BY" in the SQL statement?
Gagan Gulati
Greenhorn

Joined: Jul 27, 2004
Posts: 7
I feel this is a simple logical issue.

You can always subtract the concurrency count from 10 , get the remaining ( say 'n') and then do a
rs.absolute(n) and start moving ahead !

-G
Ameya Thakur
Ranch Hand

Joined: Feb 04, 2004
Posts: 43
Dear Gagan,

Thanks for your help , i had use the same rs.absoulte() and i am passing the parameter as -5 so that we can read from the 5th Record..

Regards

Ameya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fetch only 'n' records from the ResultSet
 
Similar Threads
how to obtain a resultset having fixed records
ResultSet Manipulation
Getting First n Records
resultset data stored in database or java
How to find the size of my resultSet