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 Using FOR loop to loop in a resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Using FOR loop to loop in a resultset " Watch "Using FOR loop to loop in a resultset " New topic
Author

Using FOR loop to loop in a resultset

Madhavan Sundararajan
Greenhorn

Joined: Jan 06, 2004
Posts: 3
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

Joined: Mar 11, 2004
Posts: 108
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

I need to use FOR loop instead of while (rs.next()) ...

Why? That makes little sense.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29233
    
138

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
for loops and while loops are logically equivilant:

[ March 29, 2004: Message edited by: Lu Battist ]
fox tie
Greenhorn

Joined: Mar 29, 2004
Posts: 7
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

For loops and while loops are logically equivilant

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

makes no sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using FOR loop to loop in a resultset
 
Similar Threads
how can show the data in database to jtable??
servlets
resultset
resultset
Use a ResultSet twice