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())??
LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.