views:

55

answers:

1

What is the best way to skip ahead to a specific record number in a Java JDBC resultset?

+1  A: 

You can use ResultSet#absolute() for this. Whether it works depends on the JDBC driver used however.

But a new question would rise: why don't you just let the SQL query return only the particular row of interest? That would have been much more efficient than moving the cursor forth and back. Or if you're interested in any of the rows, but only wanted to get the particular row, better map the ResultSet to a List<Data> so that you can use List#get().

BalusC
I need to get rows X through Z from A through Z out of a large dataset, so a list or using absolute and working from there are great answers. thanks
Jonathan Kushner
Keep in mind that this may be memory hogging. There's no need to duplicate the entire database contents into Java's heap memory if you only wanted the last few rows. Better fire a more specific SQL query everytime. Especially if the total rowcount gets into thousands.
BalusC
The only improvement I can make on this database migration script is by performing a limit from X to Z since I need all of the data (a full index scan is required). Thanks for the helpful information though, I'm watching it slowly go from 1,000 records/min of insertion to about 10.
Jonathan Kushner