JDBC ResultSet Update Record with Refresh Row

Refresh Row JDBC

One of the new features of JDBC 2.0 is the introduction of updateXXX() methods in ResultSet interface. With these methods, without using SQL commands, a record in the table can be updated, inserted, deleted and now let us see updating a record with refreshRow() method.

In the following code, a record’s old salary is retrieved, then updated and then new salary is again retrieved and printed. Once the data is updated, the operation is committed with commit() method of Connection interface.

Example on Refresh Row JDBC using refreshRow()
import java.sql.*;
public class RefreshRowDemo
  public static void main(String args[]) throws Exception
    DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger");
    Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    ResultSet res = stmt.executeQuery("select empid,empname,empsal from Employee");

    res.absolute(4);                                  // keeps the cursor on the 4th row
    double salary1 = res.getDouble("empsal"); 
    System.out.println("Old salary is: "+salary1);

    res.absolute(4);                                  // keeps the cursor again on the same row.
    res.refreshRow();                                 // Refresh Row JDBC
    double salary2 = res.getDouble("empsal"); 
    System.out.println("New salary is: "+salary2);
    con.commit();                                     // commit the database changes, if required

con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

The above statement is explained in ResultSet Types and Concurrency.

The updateXXX() methods are discussed in Scrollable ResultSet.

Pass your comments and suggestions on this tutorial "Refresh Row JDBC" to improve quality.

Leave a Comment

Your email address will not be published.