Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6325
  • Last Modified:

should i use if(rs.next()) or while(rs.next())??

if the query is returning is multiple records;

then should i use if(rs.next()) or while(rs.next())??
0
chaitu chaitu
Asked:
chaitu chaitu
  • 4
  • 3
  • 3
  • +2
2 Solutions
 
CEHJCommented:
Normally the latter
0
 
chaitu chaituAuthor Commented:
can't i use former?
0
 
CEHJCommented:
The former would normally be used if you were only interested in a *single* row
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
chaitu chaituAuthor Commented:
if the query is returning is multiple records if i write if(rs.next()) will i get all records??
0
 
CEHJCommented:
>>if i write if(rs.next()) will i get all records??

No. It'll advance to the first only
0
 
Mayank SAssociate Director - Product EngineeringCommented:
if () will run only once so how to you expect it to get all the records :) ? while will iterate through it, so....
0
 
chaitu chaituAuthor Commented:
i know just conformation
0
 
Mayank SAssociate Director - Product EngineeringCommented:
In queries like SELECT COUNT (*) FROM .... which are likely to return just one record, you can use if ()
0
 
sciuriwareCommented:
while() is at the head of a loop.
if() would be inside a loop.

It depends on the complexity of your loop what to do.
If you should not start right away with an operation like next(),
you may use if().

I wonder what made you ask this question;
I though that you would know the difference by now,
as you have been with us for nearly 2 years.

;JOOP!
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Think of rs (ResultSet) as an Iterator on the rows returned by your query. Each time you call next() you are advancing to the next row making it the 'current' row. The other ResultSet methods then operate on the 'current' row  allowing you to retrieve column values from that row. When you're done with that row, you call next() again. When there are no more rows next() returns false, just like Iterator.hasNext() would do. So if you write

    if (rs.next()) { ... }

this block of code will be able to process one row of the ResultSet. Normally, we want to do the same thing for each row of the the ResultSet so we typically write:

    while (rs.next()) { ... }

so that we can process all the returned rows in one loop.

Regards,
Jim
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Welcome back, Jim ;-)
0
 
CEHJCommented:
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now