• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

resultSet.next() behaviour

 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from your code it looks like you are only expecting zero or one rows, so the while loop is superfluous

 
Sherif Shehab
Ranch Hand
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 485
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys for the advice ..
 
Campbell Ritchie
Sheriff
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 48371
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic