• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

select rows in batch of 5 from resultset

 
nikki sinha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nikki sinha
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic