• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using FOR loop to loop in a resultset

 
Madhavan Sundararajan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I use a FOR loop for looping a resultset? I need to use FOR loop instead of while (rs.next()) ...
Help me out
 
Gaurav Jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for(int iIndex=0;iIndex<RowCount;iIndex++)
{
rs.next()
}
There are some methods in the API where you can get the row count.
Alternatively you can do a count* using your where condition and you will get the row count although its a bad programming practise hence use the API.
cheers
[ March 26, 2004: Message edited by: Gaurav Jain ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65666
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to use FOR loop instead of while (rs.next()) ...

Why? That makes little sense.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36031
432
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhavan,
I'd be wary of using rowCount because the driver usually has to loop through all the rows to get it. You really should use the while loop though. It's pretty standard.
 
Lu Battist
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for loops and while loops are logically equivilant:

[ March 29, 2004: Message edited by: Lu Battist ]
 
fox tie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
quote:
--------------------------------------------------------------------------------
for (; rs.next(); ) { //everything you had in the while loop}
----------------------------------------------------------------------------
I once try this ,but when I use while (rs.next()) later, it is something wrong.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65666
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For loops and while loops are logically equivilant

Regardless. the statement
I need to use FOR loop instead of while

makes no sense.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!