You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ResultSetrs = stmt.executeQuery("SELECT id, name FROM species")
while(rs.next()) {
ObjectidField = rs.getObject("id");
ObjectnameField = rs.getObject("name");
// you can cast the above to int and String respectively
}
🟥 10.6.3 Scrolling ResultSet
A scrollable ResultSet lets you position the cursor at any row
You can use .pevious() to obtain the previous record
.first() and .last() methods give you the first and last rows.
.beforeFirst() and .afterLast() position cursor before and after the rows beginning and end
Here is an example in play:
Stringurl = "jdbc:derby:zoo";
StringsqlQuery = "SELECT id FROM species"
+ " ORDER BY id";
try (Connectionconn = DriverManager.getConnection(url);
Statementstmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSetrs = stmt.executeQuery(sqlQuery))
{
rs.afterLast();
System.out.println(rs.previous()); // trueSystem.out.println(rs.getInt(1)); // 2System.out.println(rs.previous()); // trueSystem.out.println(rs.getInt(1)); // 1System.out.println(rs.previous()); // false/* this will throw SqlException: rs.getInt(1); */System.out.println(rs.next()); // trueSystem.out.println(rs.getInt(1)); // 1
}
Another method I must be aware of is .absolute(int) - takes the absolute index of a row which you want to move the cursor to
The method returns whether you can get data from the row: