如何在 JDBC 中创建和释放保存点?
当您设置保存点时,您就在事务中定义了一个逻辑回滚点。如果在保存点之后发生错误,您可以使用回滚方法撤消所有更改或仅撤消保存点之后所做的更改。
Savepoint 接口为您提供了额外的交易控制。大多数现代 DBMS 在其环境中支持保存点,例如 Oracle 的 PL/SQL。
设置保存点
您可以使用 Connection 接口的 **setSavepoint(String savepointName)** 方法在数据库中设置保存点,此方法接受表示保存点名称的字符串值并返回一个 Savepoint 对象。
释放保存点
您可以使用 Connection 接口的 **releaseSavepoint(Savepoint savepointName)** 方法释放保存点,此方法接受保存点的名称并释放/删除指定的保存点。
示例
假设我们有一个名为 customers 的数据库表,其中包含 5 条记录,如下所示
+----+-----------+------+---------+----------------+ | ID | NAME | AGE | SALARY | ADDRESS | +----+-----------+------+---------+----------------+ | 1 | Amit | 25 | 3000.00 | Hyderabad | | 2 | Kalyan | 27 | 4000.00 | Vishakhapatnam | | 3 | Renuka | 30 | 5000.00 | Delhi | | 4 | Archana | 24 | 1500.00 | Mumbai | | 5 | Koushik | 30 | 9000.00 | Kota | +----+-----------+------+---------+----------------+
以下 JDBC 程序向其中插入 7 条更多记录,设置一个保存点,删除一些记录并回滚到保存点。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.sql.Savepoint; public class SavepointExample { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://127.0.0.1/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Setting auto-commit false con.setAutoCommit(false); System.out.println(" "); //Creating the Statement PreparedStatement pstmt = con.prepareStatement("INSERT into customers VALUES (?, ?, ?, ?, ?) "); pstmt.setInt(1, 6); pstmt.setString(2, "Hardik"); pstmt.setInt(3, 45); pstmt.setInt(4, 6400); pstmt.setString(5, "Bhopal"); pstmt.executeUpdate(); pstmt.setInt(1, 7); pstmt.setString(2, "Trupti"); pstmt.setInt(3, 33); pstmt.setInt(4, 4360); pstmt.setString(5, "Ahmedabad"); pstmt.executeUpdate(); pstmt.setInt(1, 8); pstmt.setString(2, "Mithili"); pstmt.setInt(3, 26); pstmt.setInt(4, 4100); pstmt.setString(5, "Vijayawada"); pstmt.executeUpdate(); pstmt.setInt(1, 9); pstmt.setString(2, "Maneesh"); pstmt.setInt(3, 39); pstmt.setInt(4, 4000); pstmt.setString(5, "Hyderabad"); pstmt.executeUpdate(); pstmt.setInt(1, 10); pstmt.setString(2, "Rajaneesh"); pstmt.setInt(3, 30); pstmt.setInt(4, 6400); pstmt.setString(5, "Delhi"); pstmt.executeUpdate(); pstmt.setInt(1, 11); pstmt.setString(2, "Komal"); pstmt.setInt(3, 29); pstmt.setInt(4, 8000); pstmt.setString(5, "Ahmedabad"); pstmt.executeUpdate(); pstmt.setInt(1, 12); pstmt.setString(2, "Manyata"); pstmt.setInt(3, 25); pstmt.setInt(4, 5000); pstmt.setString(5, "Vijayawada"); pstmt.executeUpdate(); //Setting save point Savepoint savePoint = con.setSavepoint("mysavepoint"); System.out.println(" "); System.out.println("Contents of the customers table after inserting the records: "); Statement stmt = con.createStatement(); //Retrieving data ResultSet rs = stmt.executeQuery("Select * from customers"); while(rs.next()) { System.out.print("ID: "+rs.getInt("ID")+", "); System.out.print("Name: "+rs.getString("Name")+", "); System.out.print("Age: "+rs.getInt("Age")+", "); System.out.print("Salary: "+rs.getInt("Salary")+", "); System.out.print("Address: "+rs.getString("Address")); System.out.println(); } //Deleting the records stmt.execute("Delete from customers where id > 5"); System.out.println(" "); System.out.println("Contents of the customers table after deleting the records: "); //Retrieving data rs = stmt.executeQuery("Select * from customers"); while(rs.next()) { System.out.print("ID: "+rs.getInt("ID")+", "); System.out.print("Name: "+rs.getString("Name")+", "); System.out.print("Age: "+rs.getInt("Age")+", "); System.out.print("Salary: "+rs.getInt("Salary")+", "); System.out.print("Address: "+rs.getString("Address")); System.out.println(); } //Rolling back to the save point con.rollback(savePoint); System.out.println(" "); System.out.println("Contents of the table at the save point: "); //Retrieving data rs = stmt.executeQuery("Select * from customers"); while(rs.next()) { System.out.print("ID: "+rs.getInt("ID")+", "); System.out.print("Name: "+rs.getString("Name")+", "); System.out.print("Age: "+rs.getInt("Age")+", "); System.out.print("Salary: "+rs.getInt("Salary")+", "); System.out.print("Address: "+rs.getString("Address")); System.out.println(); } } }
输出
Connection established...... Contents of the customers table after inserting the records: ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Mumbai ID: 5, Name: Koushik, Age: 30, Salary: 9000, Address: Kota ID: 6, Name: Hardik, Age: 45, Salary: 6400, Address: Bhopal ID: 7, Name: Trupti, Age: 33, Salary: 4360, Address: Ahmedabad ID: 8, Name: Mithili, Age: 26, Salary: 4100, Address: Vijayawada ID: 9, Name: Maneesh, Age: 39, Salary: 4000, Address: Hyderabad ID: 10, Name: Rajaneesh, Age: 30, Salary: 6400, Address: Delhi ID: 11, Name: Komal, Age: 29, Salary: 8000, Address: Ahmedabad ID: 12, Name: Manyata, Age: 25, Salary: 5000, Address: Vijayawada Contents of the customers table after deleting the records: ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Mumbai ID: 5, Name: Koushik, Age: 30, Salary: 9000, Address: Kota Contents of the table at the save point: ID: 1, Name: Amit, Age: 25, Salary: 3000, Address: Hyderabad ID: 2, Name: Kalyan, Age: 27, Salary: 4000, Address: Vishakhapatnam ID: 3, Name: Renuka, Age: 30, Salary: 5000, Address: Delhi ID: 4, Name: Archana, Age: 24, Salary: 1500, Address: Mumbai ID: 5, Name: Koushik, Age: 30, Salary: 9000, Address: Kota ID: 6, Name: Hardik, Age: 45, Salary: 6400, Address: Bhopal ID: 7, Name: Trupti, Age: 33, Salary: 4360, Address: Ahmedabad ID: 8, Name: Mithili, Age: 26, Salary: 4100, Address: Vijayawada ID: 9, Name: Maneesh, Age: 39, Salary: 4000, Address: Hyderabad ID: 10, Name: Rajaneesh, Age: 30, Salary: 6400, Address: Delhi ID: 11, Name: Komal, Age: 29, Salary: 8000, Address: Ahmedabad ID: 12, Name: Manyata, Age: 25, Salary: 5000, Address: Vijayawada
广告