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 select rows in batch of 5 from resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply 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: 47

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: 47

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
Bartender

Joined: Aug 22, 2010
Posts: 2329
    
    2

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: select rows in batch of 5 from resultset
 
Similar Threads
callable statement question with sybase
what is difference between JDBC1 and JDBC2 ?
What is -Xlint in compiling java program?why this is done?
JDBC Callable Statement JConnect2 Returning null resultset
MULTIPLE Query IN a RS