wood burning stoves 2.0*
The moose likes JDBC and the fly likes Difference between if(resultSet.next()) and while(resultSet.next()) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Difference between if(resultSet.next()) and while(resultSet.next())" Watch "Difference between if(resultSet.next()) and while(resultSet.next())" New topic
Author

Difference between if(resultSet.next()) and while(resultSet.next())

Christian Nash
Ranch Hand

Joined: Jan 17, 2006
Posts: 107
What is the difference between if(resultSet.next()) and while(resultSet.next())and under what circumstances are they implemented?


- Christian
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
"if" block is run only once (if the condition is satisfied). Whereas "while" block is run as long as the condition holds true.



MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
now coming back to your question:

when would i use if(resultSet.next())

(a) To do something (only once) if the query returns atleast 1 row(s)
(b) To do something to the first row only

when would i use if(resultSet.next())
(a) To do something to every row returned by the query

Hope this helps
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

For some more examples:

You usually use "while" as you want to loop through all the data in the result set. You use "if" when the query returns one row by definition. For example, "select count(*) from table."


[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
Sreenivasan M
Greenhorn

Joined: Oct 05, 2006
Posts: 10
Lovely lynette...good explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between if(resultSet.next()) and while(resultSet.next())
 
Similar Threads
JRE vrs. JVM
checking resultsets
Quick Question HttpSessionBinding vs HttpSessionAttribute Listeners
Class.forName vs new
Run as AWT or SWT Application Eclipse Question