aspose file tools*
The moose likes JDBC and the fly likes while loop return only 1 row when it should return 2 rows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "while loop return only 1 row when it should return 2 rows" Watch "while loop return only 1 row when it should return 2 rows" New topic
Author

while loop return only 1 row when it should return 2 rows

ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Hello,

i have the following method that takes a username and makes a like query as follows:


In my table guesttbl there are two user don and dil, however when i run the method as username 'd' as parameter above it returns only one row which is don.
When i execute the same query directly in mysql it returns 2 rows don and dil.
I can't unserstand why my method is returning only one row when it should return 2(dil,don).
I am missing something here, any suggestion please?

Thanks.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

This line of code skips the first record:
System.out.println("result.next()"+ result.next());



OCUP UML fundamental and ITIL foundation
youtube channel
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Ohh thanks which method can i use in order to display the two rows please?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

ashley Jug wrote:Ohh thanks which method can i use in order to display the two rows please?
Your code, but without that line.

The line has a side effect. You think you are logging some info, bbut in the meanwile, because there is a next() call hidden in that logging line, the resultset already goes to the first record.

Immediately after that, youcall while (//next() ) .... and HOP, you are already at record number two

In a logging statement, you should not put code that changes the state of something. Your code changes the state of the recordset. You push it one record further.
Shinil Mohan
Ranch Hand

Joined: Aug 24, 2011
Posts: 37

Try commenting the below line of code and test


This is what excatly Jan Cumps want to convey to you. When you call 'result.next()' in the System.out.println() method, it will move to the next value in the resultset. so you are not getting the details of first element in the while loop

Are you clear now.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

And a warning:



attention: calling ResultSet.getXXX() for the same column twice is not supported by all databases.
To make your code portable, get the result into a variable, and use that later.
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Yes thanks a lot for the precious advice. Thanks again
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Why have you got two return statements?
And as you can see, it is easy to confuse l for list with 1 for number in that code. Avoid using l as far as possible.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

Campbell Ritchie wrote:Why have you got two return statements?...

Wow, good catch. Ashley's code exits at the end of the first loop execution!
Get rid of that return insie the while loop, Ashley.
ashley Jug
Ranch Hand

Joined: Sep 02, 2011
Posts: 60
Yes thanks a lot for you feedback i have made the changes accordingly. Sorry for my silly mistakes i am new to the java world.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
You're welcome
ashley Jug wrote:. . . Sorry for my silly mistakes i am new to the java world.
What makes you think "new" people have a monopoly on silly misstakes?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

Ah, and close the resources after you are finished with them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while loop return only 1 row when it should return 2 rows