AJP Lab Exercise-5


 5. Design a JDBC application which will demonstrate Updatable ResultSet functionality.




STORED PROCEDURE

CREATE TABLE employee (
    id INT PRIMARY KEY,
    name VARCHAR(50),
    salary DOUBLE
);

INSERT INTO employee VALUES (101, 'Alice', 50000);
INSERT INTO employee VALUES (102, 'Bob', 55000);
INSERT INTO employee VALUES (103, 'Charlie', 60000);



import java.sql.*;

public class UpdatableResultSetDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/testdb";  // Change 'testdb' to your actual database name
        String user = "root";  // Your DB username
        String password = "root";  // Your DB password

        try {
            // Load JDBC Driver (optional for newer versions)
            Class.forName("com.mysql.cj.jdbc.Driver");

            Connection conn = DriverManager.getConnection(url, user, password);
            Statement stmt = conn.createStatement(
                    ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);

            ResultSet rs = stmt.executeQuery("SELECT id, name, salary FROM employee");

            // -------- Display Original Data --------
            System.out.println("Original Data:");
            while (rs.next()) {
                System.out.println(rs.getInt("id") + " - " + rs.getString("name") + " - " + rs.getDouble("salary"));
            }

            // -------- Update a Row --------
            rs.absolute(1);  // Move to first row
            rs.updateDouble("salary", 80000);
            rs.updateRow();  // Commit update
            System.out.println("\nUpdated first row salary to 80000.");

            // -------- Insert a New Row --------
            rs.moveToInsertRow();
            rs.updateInt("id", 105);
            rs.updateString("name", "John");
            rs.updateDouble("salary", 60000);
            rs.insertRow();
            System.out.println("Inserted new row (105, John, 60000).");
            rs.moveToCurrentRow(); // Move back after insert

            // -------- Delete a Row --------
            rs.absolute(2);  // Move to second row
            rs.deleteRow();
            System.out.println("Deleted second row.");

            // -------- Display Final Data --------
            rs.beforeFirst();
            System.out.println("\nFinal Data:");
            while (rs.next()) {
                System.out.println(rs.getInt("id") + " - " + rs.getString("name") + " - " + rs.getDouble("salary"));
            }

            // Close connections
            rs.close();
            stmt.close();
            conn.close();

        } catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}




Sample Output:

Original Data:
101 - Alice - 50000.0
102 - Bob - 55000.0
103 - Carol - 60000.0

Updated first row salary to 80000.
Inserted new row (105, John, 60000).
Deleted second row.

Final Data:
101 - Alice - 80000.0
103 - Carol - 60000.0
105 - John - 60000.0