Reputation: 197
Statement createStatement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet executeQuery = createStatement.executeQuery("select * from stu");
executeQuery.afterLast();
while (executeQuery.previous()) { //15th line<--exception occurs at this place after first record is retrieved
int rollno = executeQuery.getInt(1);
System.out.println("rollno " + rollno + " name " + executeQuery.getString(2));
if (rollno == 1) {
if (createStatement.executeUpdate("update stu set name=" + "'vinay'" + "where rollno=1") >= 1) {
System.out.println("row updated");
}
}
}
Output:
rollno 1 name vinay
row updated
//After first record is retrieved it throws exception
Exception in thread "main" java.lang.NullPointerException
at oracle.jdbc.driver.ScrollableResultSet.previous(ScrollableResultSet.java:630)
at com.vk.db.ScrollableResultSet.main(ScrollableResultSet.java:15)
Upvotes: 1
Views: 283
Reputation: 6346
The cause of error is recycling statement object. Try this code/
Statement createStatement = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet executeQuery = createStatement.executeQuery("select id, name from stu");
executeQuery.afterLast();
while (executeQuery.previous()) {
int rollno = executeQuery.getInt("id");
System.out.println("rollno " + executeQuery.getString("id") + " name " + executeQuery.getString("name"));
if (rollno == 1) {
executeQuery.updateString("NAME", "new value");
executeQuery.updateRow();
}
}
Note. A query cannot use SELECT * from table
Upvotes: 1
Reputation: 8387
try to change
executeQuery.afterLast();
with
executeQuery.last();
or use a while loop like
while (executeQuery.next()) {
code....
}
Upvotes: 0