UpdateableRs.java Source code

Java tutorial

Introduction

Here is the source code for UpdateableRs.java

Source

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();
    }
}