- Java 和 MySQL 教程
- Java 和 MySQL - 主页
- Java 和 MySQL - 概述
- Java 和 MySQL - 环境设置
- Java 和 MySQL - 示例代码
- Java 和 MySQL - 连接
- Java 和 MySQL - 异常
- Java 和 MySQL 语句
- Java 和 MySQL - 声明
- Java 和 MySQL - 准备语句
- Java 和 MySQL - CallableStatement
- Java 和 MySQL 结果
- Java 和 MySQL - 结果集
- Java 和 MySQL - 导航结果集
- Java 和 MySQL - 查看结果集
- Java 和 MySQL - 更新结果集
- Java 和 MySQL 事务
- Java 和 MySQL - 事务
- Java 和 MySQL - 提交和回滚
- Java 和 MySQL - SavePoint 事务
- Java 和 MySQL 批处理
- Java 和 MySQL - 批处理
- 批处理 - 语句
- 批处理-PreparedStatement
- Java 和 MySQL 流
- Java 和 MySQL - 流数据
- Java 和 MySQL 示例
- Java 和 MySQL - 创建数据库
- Java 和 MySQL - 选择数据库
- Java 和 MySQL - 删除数据库
- Java 和 MySQL - 创建表
- Java 和 MySQL - 删除表
- Java 和 MySQL - 插入记录
- Java 和 MySQL - 选择记录
- Java & MySQL - 更新记录
- Java 和 MySQL - 删除记录
- Java 和 MySQL -Where 子句
- Java 和 MySQL - Like 子句
- Java 和 MySQL - 数据排序
- Java 和 MySQL 有用资源
- Java 和 MySQL - 快速指南
- Java 和 MySQL - 有用的资源
- Java 和 MySQL - 讨论
Java 和 MySQL - SavePoint
新的 JDBC 3.0 Savepoint 接口为您提供了额外的事务控制。大多数现代 DBMS 都支持其环境中的保存点,例如 Oracle 的 PL/SQL。
当您设置保存点时,您就在事务中定义了逻辑回滚点。如果在保存点之后发生错误,您可以使用回滚方法撤消所有更改或仅撤消保存点之后所做的更改。
Connection 对象有两个新方法可以帮助您管理保存点 -
setSavepoint(String savepointName) - 定义一个新的保存点。它还返回一个 Savepoint 对象。
releaseSavepoint(Savepoint savepointName) - 删除保存点。请注意,它需要一个 Savepoint 对象作为参数。该对象通常是由 setSavepoint() 方法生成的保存点。
有一种回滚 (String savepointName)方法,可将工作回滚到指定的保存点。
以下示例说明了 Savepoint 对象的使用 -
try{ //Assume a valid connection object conn conn.setAutoCommit(false); Statement stmt = conn.createStatement(); //set a Savepoint Savepoint savepoint1 = conn.setSavepoint("Savepoint1"); String SQL = "INSERT INTO Employees " + "VALUES (106, 20, 'Rita', 'Tez')"; stmt.executeUpdate(SQL); //Submit a malformed SQL statement that breaks String SQL = "INSERTED IN Employees " + "VALUES (107, 22, 'Sita', 'Tez')"; stmt.executeUpdate(SQL); // If there is no error, commit the changes. conn.commit(); }catch(SQLException se){ // If there is any error. conn.rollback(savepoint1); }
在这种情况下,上述 INSERT 语句都不会成功,并且所有内容都会回滚。
以下是使用setSavepoint和rollback的示例。
该示例代码是根据前面章节中完成的环境和数据库设置编写的。
将以下示例复制并粘贴到 TestApplication.java 中,编译并运行如下 -
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Savepoint; import java.sql.Statement; public class TestApplication { static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT"; static final String USER = "guest"; static final String PASS = "guest123"; static final String QUERY = "SELECT id, first, last, age FROM Employees"; static final String DELETE_QUERY = "DELETE FROM Employees WHERE ID = 8"; static final String DELETE_QUERY_1 = "DELETE FROM Employees WHERE ID = 9"; public static void printResultSet(ResultSet rs) throws SQLException{ // Ensure we start with first row rs.beforeFirst(); while(rs.next()){ // Display values System.out.print("ID: " + rs.getInt("id")); System.out.print(", Age: " + rs.getInt("age")); System.out.print(", First: " + rs.getString("first")); System.out.println(", Last: " + rs.getString("last")); } System.out.println(); } public static void main(String[] args) { // Open a connection try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); ) { conn.setAutoCommit(false); ResultSet rs = stmt.executeQuery(QUERY); System.out.println("List result set for reference...."); printResultSet(rs); // delete row having ID = 8 // But save point before doing so. Savepoint savepoint1 = conn.setSavepoint("ROWS_DELETED_1"); System.out.println("Deleting row...."); stmt.executeUpdate(DELETE_QUERY); // Rollback the changes after save point 1. conn.rollback(savepoint1); // delete rows having ID = 9 // But save point before doing so. conn.setSavepoint("ROWS_DELETED_2"); System.out.println("Deleting row...."); stmt.executeUpdate(DELETE_QUERY_1); rs = stmt.executeQuery(QUERY); System.out.println("List result set for reference...."); printResultSet(rs); // Clean-up environment rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }
现在,让我们按如下方式编译上面的示例 -
C:\>javac TestApplication.java C:\>
当您运行TestApplication时,它会产生以下结果 -
C:\>java TestApplication List result set for reference.... ID: 1, Age: 23, First: Zara, Last: Ali ID: 2, Age: 30, First: Mahnaz, Last: Fatma ID: 3, Age: 35, First: Zaid, Last: Khan ID: 4, Age: 33, First: Sumit, Last: Mittal ID: 5, Age: 40, First: John, Last: Paul ID: 7, Age: 20, First: Sita, Last: Singh ID: 8, Age: 20, First: Rita, Last: Tez ID: 9, Age: 20, First: Sita, Last: Singh Deleting row.... Deleting row.... List result set for reference.... ID: 1, Age: 23, First: Zara, Last: Ali ID: 2, Age: 30, First: Mahnaz, Last: Fatma ID: 3, Age: 35, First: Zaid, Last: Khan ID: 4, Age: 33, First: Sumit, Last: Mittal ID: 5, Age: 40, First: John, Last: Paul ID: 7, Age: 20, First: Sita, Last: Singh ID: 8, Age: 20, First: Rita, Last: Tez C:\>