JDBC Tutorial

Updatable Resultset

Whenever we create a ResultSet object which never allows us to update the database through ResultSet object and it allows retrieving the data only in forward direction. Such type of ResultSet is known as non-updatable and non-scrollable ResultSet.

In order to make the ResultSet object as updatable and scrollable we must use the following constants which are present in ResultSet interface.

int Typeint Mode
TYPE_SCROLL_SENSITIVECONCUR_UPDATABLE

The above two constants must be specified while we are creating Statement object by using the following method:

Updatable resultset

For example:

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

On ResultSet we can perform the following three operations, they are inserting a record, deleting a record and updating a record.

Steps for INSERTING a record through ResultSet object:

  1. Decide at which position we are inserting a record by calling absolute method.
  2. For example:

    rs.absolute (3);
    
  3. Since we are inserting a record we must use the following method to make the ResultSet object to hold the record.
  4. result set object to hold

    For example:

    rs.moveToInsertRow ();
    
  5. Update all columns of the database or provide the values to all columns of database by using the following generalized method which is present in ResultSet interface.
  6. update resultset

    For example:

    rs.updateInt (1, 5);
    rs.updateString (2, "abc");
    rs.updateInt (3, 80);
    
  7. Upto step-3 the data is inserted in ResultSet object and whose data must be inserted in the database permanently by calling the following method:
  8. resultset insertrow

    It throws an exception called SQLException.

    For example:

    rs.insertRow ();
    

Steps for DELETING a record through ResultSet object:

  1. Decide which record you want to delete.
  2. For example:

    rs.absolute (3); // rs pointing to 3rd record & marked for deletion
    
  3. To delete the record permanently from the database we must call the following method which is present in ResultSet interface.
  4. resultset delete row

    For example:

    rs.deleteRow ();
    

Steps for UPDATING a record through ResultSet object:

  1. Decide which record to update.
  2. For example:

    rs.absolute (2);
    
  3. Decide which columns to be updated.
  4. For example:

    rs.updateString (2, "pqr");
    rs.updateInt (3, 91);
    
  5. Using step-2 we can modify the content of ResultSet object and the content of ResultSet object must be updated to the database permanently by calling the following method which is present in ResultSet interface.
  6. resultset update row

    For example:

    rs.updateRow ();
    

For example:

Write a java program which illustrates the concept of updatable ResultSet?

For example:

Answer:
import java.sql.*;

class UpdateResultSet {

    public static void main(String[] args) {
        try {
            Class.forName("Sun.jdbc.odbc.JdbcOdbcDriver");
            System.out.println("DRIVERS LOADED...");
            Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
            System.out.println("CONNECTION ESTABLISHED...");
            Statement st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            ResultSet rs = st.executeQuery("select * from emp1");
            rs.next();
            rs.updateInt(2, 8000);
            rs.updateRow();
            System.out.println("1 ROW UPDATED...");
            rs.moveToInsertRow();
            rs.updateInt(1, 104);
            rs.updateInt(2, 2000);
            rs.insertRow();
            System.out.println("1 ROW INSERTED...");
            rs.absolute(2);
            rs.deleteRow();
            System.out.println("1 ROW DELETED...");
            con.close();
        } catch (Exception e) {

            e.printStackTrace();
        }
    }// main
};// UpdateResultSet

Note:The scrollability and updatability of a ResultSet depends on the development of the driver of the driver vendors. OracleDriver and JdbcOdbcDriver will support the concept of scrollability and updatability of a ResultSet but there may be same drivers which are available in the industry which are not supporting the concept of scrollability and updatability.