- JDBC 教程
- JDBC - 首页
- JDBC - 简介
- JDBC - SQL 语法
- JDBC - 环境配置
- JDBC - 示例代码
- JDBC - 驱动程序类型
- JDBC - 连接
- JDBC - 语句
- JDBC - 结果集
- JDBC - 数据类型
- JDBC - 事务
- JDBC - 异常
- JDBC - 批处理
- JDBC - 存储过程
- JDBC - 流式数据
- JDBC - RowSet
- JDBC - 复制数据库
- JDBC - ACID 属性
- JDBC - 连接池
- JDBC 示例
- JDBC - 创建数据库
- JDBC - 选择数据库
- JDBC - 删除数据库
- JDBC - 创建表
- JDBC - 删除表
- JDBC - 插入记录
- JDBC - 选择记录
- JDBC - 更新记录
- JDBC - 删除记录
- JDBC - WHERE 子句
- JDBC - LIKE 子句
- JDBC - 数据排序
- JDBC 有用资源
- JDBC - 常见问题解答
- JDBC - 快速指南
- JDBC - 有用资源
- JDBC - 讨论
- 有用 - Java 教程
JDBC - 设置保存点示例
以下是示例,它使用了事务教程中描述的 setSavepoint 和 rollback 方法。
此示例代码是基于前面章节中完成的环境和数据库设置编写的。
使用 SavePoint 回滚到之前的稳定更改示例
在这个示例中,我们有六个静态字符串,包含数据库连接 URL、用户名、密码、SELECT 查询和两个 DELETE 查询。DELETE 查询用于删除 Employees 表中 ID 为 8 和 9 的两条记录。现在,使用 DriverManager.getConnection() 方法,我们准备了一个数据库连接。使用 setAutoCommit(false),我们将自动提交设置为 false,默认为 true。连接准备就绪后,我们使用 connection.createStatement() 方法创建了一个 Statement 对象。在创建 Statement 对象时,我们使用了 ResultSet 类型 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY。
使用 setSavepoint() 方法,我们设置了一个保存点,然后删除了 ID 为 8 的记录。下一步,使用回滚到之前的保存点来恢复更改。这将恢复删除行的更改。然后,我们删除了 ID 为 9 的记录,并使用 printResultSet() 方法打印记录,该方法打印结果集的所有记录。
将以下示例复制并粘贴到 JDBCExample.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 JDBCExample { static final String DB_URL = "jdbc:mysql://127.0.0.1/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 JDBCExample.java C:\>
运行 JDBCExample 时,将产生以下结果:
C:\>java JDBCExample 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:\>
使用 SavePoint 回滚到之前的更改示例
在这个示例中,我们有六个静态字符串,包含数据库连接 URL、用户名、密码、SELECT 查询和两个 DELETE 查询。DELETE 查询用于删除 Employees 表中 ID 为 8 的记录。现在,使用 DriverManager.getConnection() 方法,我们准备了一个数据库连接。使用 setAutoCommit(false),我们将自动提交设置为 false,默认为 true。连接准备就绪后,我们使用 connection.createStatement() 方法创建了一个 Statement 对象。在创建 Statement 对象时,我们使用了 ResultSet 类型 TYPE_SCROLL_INSENSITIVE 和 CONCUR_READ_ONLY。
使用 setSavepoint() 方法,我们设置了一个保存点,然后删除了 ID 为 8 的记录。下一步,使用回滚到之前的保存点来恢复更改。这将恢复删除行的更改。然后,使用 printResultSet() 方法打印记录,该方法打印结果集的所有记录。
将以下示例复制并粘贴到 JDBCExample.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 JDBCExample { static final String DB_URL = "jdbc:mysql://127.0.0.1/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"; 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); // Create a savepoint object before executing the // deleteQuery Savepoint beforeDeleteSavepoint = conn.setSavepoint(); stmt.executeUpdate(DELETE_QUERY); System.out.println("Table EMPLOYEES after executing deleteQuery"); ResultSet rs = stmt.executeQuery(QUERY); printResultSet(rs); // Rollback the changes after delete save point. conn.rollback(beforeDeleteSavepoint); System.out.println("Table Employees after rollback"); rs = stmt.executeQuery(QUERY); printResultSet(rs); // Clean-up environment rs.close(); } catch (SQLException e) { e.printStackTrace(); } } }
输出
现在,让我们编译上面的示例,如下所示:
C:\>javac JDBCExample.java C:\>
运行 JDBCExample 时,将产生以下结果:
C:\>java JDBCExample Table EMPLOYEES after executing deleteQuery ID: 1, Age: 18, First: Zara, Last: Ali ID: 2, Age: 25, First: Mahnaz, Last: Fatma ID: 3, Age: 30, First: Zaid, Last: Khan ID: 4, Age: 28, First: Sumit, Last: Mittal ID: 7, Age: 20, First: Rita, Last: Tez Table Employees after rollback ID: 1, Age: 18, First: Zara, Last: Ali ID: 2, Age: 25, First: Mahnaz, Last: Fatma ID: 3, Age: 30, First: Zaid, Last: Khan ID: 4, Age: 28, First: Sumit, Last: Mittal ID: 7, Age: 20, First: Rita, Last: Tez ID: 8, Age: 20, First: Sita, Last: Singh C:\>