my dog learned polymorphism*
The moose likes JDBC and the fly likes resultSet.next()  behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "resultSet.next()  behaviour " Watch "resultSet.next()  behaviour " New topic
Author

resultSet.next() behaviour

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Hi Guys ,

i got a weird behaviour for resultSet.next() in line 16 .. here is the code :

the problem is when i debug this code it goes to 1 to checks the conditon and this returns false , so it goes to 2 then execute it , then goes directly to 3 without executing what inside while although the conditon in 1 returns false , and i run the query outside the code i found it is returning data , so what i'm missing ??


Thanks,
Sherif
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

each call to next moves you forward in the ResultSet, so the while loop will never see the first item because next is always called twice before it gets to that code.
One in the if, one at the start of the 'while'
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

David O'Meara wrote:each call to next moves you forward in the ResultSet, so the while loop will never see the first item because next is always called twice before it gets to that code.
One in the if, one at the start of the 'while'

Thanks David the problem was solved when i commented the if , but i still need to check the resultset before i go to while, how i can do that ?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
And never write == false or == true. You can get compiler errors, or worse, logical errors, if you write = by mistake, and == false is poor style.

If you want to check the next method returns false, write

if (!mySR.next()) . . .
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Campbell Ritchie wrote:And never write == false or == true. You can get compiler errors, or worse, logical errors, if you write = by mistake, and == false is poor style.

If you want to check the next method returns false, write

if (!mySR.next()) . . .


Hi Campbell ,
this (f (!mySR.next()) ) will not work in case there is only one row retrieved ..
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

from your code it looks like you are only expecting zero or one rows, so the while loop is superfluous

Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

David O'Meara wrote:from your code it looks like you are only expecting zero or one rows, so the while loop is superfluous



what do you think i'll use , just in case there are more than 1 row retrieved

Am i right ?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

that depends on whether you expect more than one row.
For more than one row there are several options, but it could look like this:


It all depends.
Sherif Shehab
Ranch Hand

Joined: Mar 05, 2007
Posts: 483

Thanks Guys for the advice ..
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
There is no significant difference in functionality between !boo and boo == false, but !boo is much neater and less error-prone.

If you can set up your booleans so you can always say if (boo) . . . and avoid the ! in if (!boo) . . . that would be even better. But it is sometimes impossible.
Malhar Me
Greenhorn

Joined: Jun 22, 2009
Posts: 27


rs.next() increase by 1 in if, if the record is their
then it wil execute the else block that time again decrese the count by 1 i.e. it again to its original position.

Hope it will help you...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
Sherif Shehab wrote:. . .
Hi Campbell ,
this if (!mySR.next()) ) will not work in case there is only one row retrieved ..
Why not? It ought to.

And sorry I didn't notice your reply earlier.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: resultSet.next() behaviour
 
Similar Threads
Compilation problem with if condition
JQ+ Question ID :952739443290
problem adding Arrays to an ArrayList in a loop
resultset value in vector
Returning and using booleans