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: 26204
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
posted Oct 05, 2006 12:23:00
0
Lovely lynette...good explanation
subject: Difference between if(resultSet.next()) and while(resultSet.next())