wood burning stoves 2.0*
The moose likes JDBC and the fly likes ResultSet can not print the first row Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ResultSet can not print the first row" Watch "ResultSet can not print the first row" New topic
Author

ResultSet can not print the first row

Shounak Kundu
Ranch Hand

Joined: Jul 29, 2010
Posts: 60



Here's my code :


My Problem is that :
This code does not print the very first row.

How can I do that ?

Also the table is distorted..

Please help


Shounak
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Check the documentation for ResultSet.next(). Not certain, but I think you are going to the first row when you call if (rs.next()) . . . and when you get to the while (rs.next()) . . . loop, you are going to the second row.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Campbell is right. Each time you call "rs.getNext()" you move the cursor down one row. That means on the first iteration of your loop, you'll be getting data out of the second row. Use a do-while loop instead.


(Alternatively, you could call "rs.beforeFirst()" after doing your initial check, but it's more fun to annoy the people who lay down the hate against poor, poor do-while.)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

but it's more fun to annoy the people who lay down the hate against poor, poor do-while


I didnt know they existed


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Deepak Bala wrote: . . . I didnt know they existed
You'd be surprised
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ResultSet can not print the first row