tags:

views:

112

answers:

3

I have this java code which does this

ResulSet rs = stmt.executeQuery();
while (rs.next()) {
    .... //do regular processing

    if (rs.last()) {
       //do last record processing
    }

}
+4  A: 

You should use: isLast() instead.

Be warned, not all the JDCB drivers support this feature. Obviously you have to check it with your installation.

Most major DB work fine.

The fixed code should look like this.

if( rs.isLast() ) { 
   // do last record processing
}
OscarRyz
+2  A: 

Personally, I would declare the variables that are retrieved from the query outside the loop, and then do the "last record processing" after the loop.

ResulSet rs = stmt.executeQuery();
long primaryKey;

while (rs.next()) {
    primaryKey = rs.getLong(1);
    .... //do regular processing
}

// Do last record processing.
System.out.println("last primary key = " + primaryKey);
Paul Tomblin
+2  A: 

No: rs.last() will actually send the cursor to the last row in the ResultSet, which is definitely not what you want to do. You would only ever process at most 2 rows (first and last) from the result set.

harto