This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Christian Nash
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between if(resultSet.next()) and while(resultSet.next())and under what circumstances are they implemented?
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"if" block is run only once (if the condition is satisfied). Whereas "while" block is run as long as the condition holds true.

 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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."
 
Sreenivasan M
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lovely lynette...good explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic