JDBC ResultSet Enhancement Update Methods Example


ResultSet methods JDBC

JDBC 2.0 ResultSet enhancement operations include the methods to update, insert or delete a record without writing the SQL commands.

That is, SQL commands need not be known, of course for CRUD operations. This is the greatness of ResultSet methods JDBC.
Example on ResultSet methods JDBC
import java.sql.*;
public class Updates
{
  public static void main(String args[]) throws Exception
  {
    Class.forName("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");
                      // updating a record
    res.absolute(3);                    // to place the cursor on the 3rd record
    res.updateString(2, "Raju");        // updates the name of the employee
    res.updateDouble(3, 1111.11);       // updates the salary of the employee
    res.updateRow(); 
		      // deleting a row
    res.absolute(4);
    res.deleteRow();                    // now the 4th record is deleted
		      // inserting a record
    res.moveToInsertRow();
    res.updateInt(1, 3000); 
    res.updateString(2, "Reddy"); 
    res.updateDouble(3, 2222.22); 
    res.insertRow();

    con.commit();                       // commit the updates, if required
    System.out.println("modifications done");

    res.close();
    stmt.close();
    con.close();
  }
}

ResultSet methods JDBC 2.0 includes insertRow(), moveToInsertRow(), last(), absolute() etc. These methods are used in the following link example.

Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

The above statement is explained in ResultSet Types and Concurrency.

res.absolute(3);

The above statement is explained in Scrollable ResultSet.

A similar program is available to update a record using refreshRow() method – ResultSet Update Record with Refresh Row.

Leave a Comment

Your email address will not be published.