aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 36579
    
  16
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: 2771
    
  10

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: 6657
    
    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: 36579
    
  16
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
 
Similar Threads
How can I generate dynamic tables taking values from database
getObject () not returning values
Number Format Exception
SqlException accured at processing Jsp
How can I retrieve date using rs.getDate() ?