aspose file tools*
The moose likes JDBC and the fly likes select rows in batch of 5 from resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "select rows in batch of 5 from resultset" Watch "select rows in batch of 5 from resultset" New topic
Author

select rows in batch of 5 from resultset

nikki sinha
Greenhorn

Joined: Jul 04, 2012
Posts: 14
i have used a select command in my java program and stored its value in the result set. now while looping in the resultset i want to use a select command which will select the first 5 lines of the resultset and insert into other table. for the second time, it should select the next 5 lines and insert into the table. and for the third time, so on..

Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

It would have been better if you can mention what solution(s) you have tried.

Coming back to the question, having an inner loop with a count of 5 should do the trick. Actually, in such cases the ResultSet support is limited i believe. Using other collections should make it easier.


Warm Regards,
Rakesh
nikki sinha
Greenhorn

Joined: Jul 04, 2012
Posts: 14
Rakesh K. Cherukuri wrote:It would have been better if you can mention what solution(s) you have tried.

Coming back to the question, having an inner loop with a count of 5 should do the trick. Actually, in such cases the ResultSet support is limited i believe. Using other collections should make it easier.

Thank you for a quick reply,
i have tried it doing this way:



this code selects the rows in alternate manner.... i want to get frist 5 in one batch then secon 5 in second batch.
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

Problem with the posted code is calling res.next() repetitively. Thing is each call to res.next() moves cursor (ResultSet internal implementation) one step ahead. A general rule is to associated each call to next() with a read of the row.

Try it this way

Caution : code is not a tested one. Expect corner case issues
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

Are you doing this to speed things up?

If the two tables are on the same database, and you don't do any complicated calculations on them, you could use a single SQL command:

That's the fastest way to do it. The data won't even travel over the network from the database to wherever your Java code runs.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: select rows in batch of 5 from resultset