DEVFYI - Developer Resource - FYI

How can I know when I reach the last record in a table, since JDBC doesn't provide an EOF method?

JDBC Interview Questions and Answers


(Continued from previous question...)

How can I know when I reach the last record in a table, since JDBC doesn't provide an EOF method?

Answer1
You can use last() method of java.sql.ResultSet, if you make it scrollable.
You can also use isLast() as you are reading the ResultSet.
One thing to keep in mind, though, is that both methods tell you that you have reached the end of the current ResultSet, not necessarily the end of the table. SQL and RDBMSes make no guarantees about the order of rows, even from sequential SELECTs, unless you specifically use ORDER BY. Even then, that doesn't necessarily tell you the order of data in the table.

Answer2
Assuming you mean ResultSet instead of Table, the usual idiom for iterating over a forward only resultset is:
ResultSet rs=statement.executeQuery(...);
while (rs.next()) {
// Manipulate row here
}

(Continued on next question...)

Other Interview Questions