Updatable resultset with Oracle Driver : ResultSet Updatable « Database SQL JDBC « Java






Updatable resultset with Oracle Driver

  

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateableRs {
  public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL", "yourName", "mypwd");
    Statement stmt = conn.createStatement();
    stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    ResultSet rs = stmt.executeQuery("SELECT ssn, name, salary FROM EMPLOYEES");
    printRs(rs);

    rs.beforeFirst();

    while (rs.next()) {
      double newSalary = rs.getDouble("salary") * 1.053;
      rs.updateDouble("salary", newSalary);
      rs.updateRow();
    }
    printRs(rs);
    conn.close();
  }

  public static void printRs(ResultSet rs) throws SQLException {
    rs.beforeFirst();

    while (rs.next()) {
      int ssn = rs.getInt("ssn");
      String name = rs.getString("name");
      double salary = rs.getDouble("salary");

      System.out.print("Row Number=" + rs.getRow());
      System.out.print(", SSN: " + ssn);
      System.out.print(", Name: " + name);
      System.out.println(", Salary: $" + salary);
    }
    System.out.println();
  }
}

   
  








Related examples in the same category

1.If database support updatable result sets
2.Using UpdatableResultSet to insert a new row
3.Delete Row from Updatable ResultSet for MySQL
4.Insert Row to Updatable ResultSet from MySQL
5.Make updates in Updatable ResultSet
6.Demo Updatable ResultSet
7.Determining If a Database Supports Updatable Result Sets: An updatable result set allows modification to data in a table through the result set.
8.Creating an Updatable Result Set
9.Determining If a Result Set Is Updatable
10.Updating a Row in a Database Table Using an Updatable Result Set
11.Cancelling Updates to an Updatable Result Set
12.Inserting a Row into a Database Table Using an Updatable Result Set
13.Deleting a Row from a Database Table Using an Updatable Result Set
14.Refreshing a Row in an Updatable Result Set