• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

resultSet.next() behaviour

 
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
S Shehab
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()) . . .
 
S Shehab
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
S Shehab
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
S Shehab
Ranch Hand
Posts: 493
Android Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys for the advice ..
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic