Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Cách cập nhật nội dung ResultSet bằng chương trình JDBC?


Để cập nhật nội dung ResultSet, bạn cần tạo statement với kiểu ResultSet updatable:

// Tạo Statement object 
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);

Giống như getXXX() và setXXX(), interface ResultSet cũng cung cấp phương thức updateXXX() để cập nhật nội dung hàng trong result set. Các phương thức này chấp nhận giá trị nguyên đại diện cho chỉ số hoặc String đại diện cho nhãn cột của hàng cần cập nhật.

Lưu ý: để cập nhật ResultSet, bảng phải có khóa chính.

Ví dụ

Giả sử bảng Employees có 5 bản ghi:

+----+---------+--------+----------------+ 
| Id | Name    | Salary | Location       | 
+----+---------+--------+----------------+ 
| 1  | Amit    | 3000   | Hyderabad      | 
| 2  | Kalyan  | 4000   | Vishakhapatnam | 
| 3  | Renuka  | 6000   | Delhi          | 
| 4  | Archana | 9000   | Mumbai         | 
| 5  | Sumith  | 11000  | Hyderabad      | 
+----+---------+--------+----------------+

Ví dụ cập nhật nội dung result set:

import java.sql.*; 

public class ResultSetExample { 
    public static void main(String[] args) throws Exception { 
        // Đăng ký Driver 
        DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 
        // Lấy kết nối 
        String mysqlUrl = "jdbc:mysql://localhost/TestDB"; 
        Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); 
        System.out.println("Kết nối thành công......"); 
        // Tạo Statement object 
        Statement stmt = con.createStatement( 
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
        // Truy xuất dữ liệu 
        ResultSet rs = stmt.executeQuery("select * from Employees"); 
        // In nội dung bảng 
        System.out.println("Nội dung bảng: "); 
        printRs(rs); 
        // Di chuyển con trỏ về đầu 
        rs.beforeFirst(); 
        // Cập nhật lương mỗi nhân viên +5000 
        while(rs.next()){ 
            // Lấy theo tên cột 
            int newSal = rs.getInt("Salary") + 5000; 
            rs.updateInt("Salary", newSal); 
            rs.updateRow(); 
        } 
        System.out.println("Nội dung ResultSet sau khi tăng lương"); 
        printRs(rs); 
        // Đặt vị trí bản ghi thứ 2 
        rs.beforeFirst(); 
        rs.absolute(2); 
        System.out.println("Bản ghi cần xóa: "); 
        System.out.print("ID: " + rs.getInt("id")); 
        System.out.print(", Salary: " + rs.getInt("Salary")); 
        System.out.print(", Name: " + rs.getString("Name")); 
        System.out.println(", Location: " + rs.getString("Location")); 
        System.out.println(" "); 
        // Xóa hàng 
        rs.deleteRow(); 
        System.out.println("Nội dung ResultSet sau khi xóa một bản ghi..."); 
        printRs(rs); 
        System.out.println("Tạm biệt!"); 
    } 
    public static void printRs(ResultSet rs) throws SQLException{ 
        // Đảm bảo bắt đầu từ hàng đầu 
        rs.beforeFirst(); 
        while(rs.next()){ 
            System.out.print("ID: " + rs.getInt("id")); 
            System.out.print(", Salary: " + rs.getInt("Salary")); 
            System.out.print(", Name: " + rs.getString("Name")); 
            System.out.println(", Location: " + rs.getString("Location")); 
        } 
        System.out.println(); 
    } 
}

Kết quả

Kết nối thành công...... 
Nội dung bảng: 
ID: 1, Salary: 3000, Name: Amit, Location: Hyderabad 
ID: 2, Salary: 4000, Name: Kalyan, Location: Vishakhapatnam 
ID: 3, Salary: 6000, Name: Renuka, Location: Delhi 
ID: 4, Salary: 9000, Name: Archana, Location: Mumbai 
ID: 5, Salary: 11000, Name: Sumith, Location: Hyderabad 
Nội dung resultset sau khi tăng lương 
ID: 1, Salary: 8000, Name: Amit, Location: Hyderabad 
ID: 2, Salary: 9000, Name: Kalyan, Location: Vishakhapatnam 
ID: 3, Salary: 11000, Name: Renuka, Location: Delhi 
ID: 4, Salary: 14000, Name: Archana, Location: Mumbai 
ID: 5, Salary: 16000, Name: Sumith, Location: Hyderabad 
Bản ghi cần xóa: 
ID: 2, Salary: 9000, Name: Kalyan, Location: Vishakhapatnam 
Nội dung resultset sau khi xóa một bản ghi... 
ID: 1, Salary: 8000, Name: Amit, Location: Hyderabad 
ID: 3, Salary: 11000, Name: Renuka, Location: Delhi 
ID: 4, Salary: 14000, Name: Archana, Location: Mumbai 
ID: 5, Salary: 16000, Name: Sumith, Location: Hyderabad 
Tạm biệt!