Win a copy of Design for the Mind this week in the Design forum!
  • 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
Pie
Posts: 64688
86
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
Marshal
Posts: 34178
340
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
Pie
Posts: 64688
86
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic