Java Connection rollBack()方法与示例
回滚操作将撤消当前事务所做的所有更改,即,如果调用Connection接口的 rollBack()方法,则所有修改都将还原到最后一次提交。
您还可以通过将所需的Savepoint对象作为参数传递给此方法,将数据库中的更改回滚到特定的保存点,如下所示:
//Setting the save point con.rollback("MysavePoint");
回滚事务
使用DriverManager类的registerDriver()方法将驱动程序注册为-
//注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver());
使用getConnection()
DriverManager类的方法获取连接,如下所示:
//获得连接 String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password");
使用setAutoCommit()方法关闭自动提交,如下所示:
//将自动提交设置为false- con.setAutoCommit(false);
使用setSavepoint() 设置保存点,或者使用以下commit()
方法提交事务:
Savepoint savePoint = con.setSavepoint("MysavePoint"); Con.commit();
最后,回滚到最后一个保存点,或者使用rollback()或rollback(Savepointsavepoint)方法最后提交。
con.rollback() Or, con. rollback(mySavepoint);
让我们使用CREATE语句在MySQL数据库中创建一个名为MyPlayers 的表,如下所示-
CREATE TABLE MyPlayers( ID INT, First_Name VARCHAR(255), Last_Name VARCHAR(255), Date_Of_Birth date, Place_Of_Birth VARCHAR(255), Country VARCHAR(255), PRIMARY KEY (ID) );
现在,我们将使用INSERT语句在MyPlayers表中插入7条记录-
insert into MyPlayers values(1, 'Shikhar', 'Dhawan', DATE('1981-12-05'), 'Delhi', 'India'); insert into MyPlayers values(2, 'Jonathan', 'Trott', DATE('1981-04-22'), 'CapeTown', 'SouthAfrica'); insert into MyPlayers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27'), 'Matale', 'Srilanka'); insert into MyPlayers values(4, 'Virat', 'Kohli', DATE('1988-11-05'), 'Delhi', 'India'); insert into MyPlayers values(5, 'Rohit', 'Sharma', DATE('1987-04-30'), 'Nagpur', 'India'); insert into MyPlayers values(6, 'Ravindra', 'Jadeja', DATE('1988-12-06'), 'Nagpur', 'India'); insert into MyPlayers values(7, 'James', 'Anderson', DATE('1982-06-30'), 'Burnley', 'England');
以下JDBC程序演示了Connection接口的rollback()方法。在这里,我们在Myplayers表中插入了一条新记录。使用该commit()
方法完成事务,删除先前插入的记录,然后回滚到创建的保存点,并显示表的内容
示例
import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Connection_rollBack { public static void main(String args[]) throws SQLException { //注册驱动程序 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //获得连接 String url = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(url, "root", "password"); System.out.println("Connection established......"); //将自动提交设置为false- con.setAutoCommit(false); //创建一个Statement对象 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); //检索数据 ResultSet rs = stmt.executeQuery("select * from MyPlayers"); System.out.println("Contents of the table initially"); while(rs.next()) { System.out.print("ID: "+rs.getString("ID")+", "); System.out.print("First_Name: "+rs.getString("First_Name")+", "); System.out.print("Last_Name: "+rs.getString("Last_Name")+", "); System.out.print("Date_Of_Birth: "+rs.getString("Date_Of_Birth")+", "); System.out.print("Place_Of_Birth: "+rs.getString("Place_Of_Birth")+", "); System.out.print("Country: "+rs.getString("Country")); System.out.println(""); } PreparedStatement pstmt = con.prepareStatement("INSERT INTO MyPlayers VALUES (?, ?, ?, ?, ?, ?)"); pstmt.setInt(1, 8); pstmt.setString(2, "Ishant"); pstmt.setString(3, "Sharma"); pstmt.setDate(4, new Date(904694400000L)); pstmt.setString(5, "Delhi"); pstmt.setString(6, "India"); pstmt.executeUpdate(); //提交事务 con.commit(); //删除记录 stmt.execute("Delete from MyPlayers where id = 8"); //回滚到保存点 con.rollback(); //回滚后表的内容 System.out.println("Contents of the table"); rs = stmt.executeQuery("select * from MyPlayers"); while(rs.next()) { System.out.print("ID: "+rs.getString("ID")+", "); System.out.print("First_Name: "+rs.getString("First_Name")+", "); System.out.print("Last_Name: "+rs.getString("Last_Name")+", "); System.out.print("Date_Of_Birth: "+rs.getString("Date_Of_Birth")+", "); System.out.print("Place_Of_Birth: "+rs.getString("Place_Of_Birth")+", "); System.out.print("Country: "+rs.getString("Country")); System.out.println(""); } } }
现在,您可以观察结果中保存点之前创建的新记录,尽管我们已将其删除。
如果我们回滚一个事务,那么在上一次提交之后所做的所有更改都将被还原。
由于我们已在设置提交后删除了第8条记录,因此在回滚时将还原此删除。
输出结果
Connection established...... Contents of the table initially ID: 1, First_Name: Shikhar, Last_Name: Dhawan, Date_Of_Birth: 1981-12-05, Place_Of_Birth: Delhi, Country: India ID: 2, First_Name: Jonathan, Last_Name: Trott, Date_Of_Birth: 1981-04-22, Place_Of_Birth: CapeTown, Country: SouthAfrica ID: 3, First_Name: Kumara, Last_Name: Sangakkara, Date_Of_Birth: 1977-10-27, Place_Of_Birth: Matale, Country: Srilanka ID: 4, First_Name: Virat, Last_Name: Kohli, Date_Of_Birth: 1988-11-05, Place_Of_Birth: Mumbai, Country: India ID: 5, First_Name: Rohit, Last_Name: Sharma, Date_Of_Birth: 1987-04-30, Place_Of_Birth: Nagpur, Country: India ID: 6, First_Name: Ravindra, Last_Name: Jadeja, Date_Of_Birth: 1988-12-06, Place_Of_Birth: Nagpur, Country: India ID: 7, First_Name: James, Last_Name: Anderson, Date_Of_Birth: 1982-06-30, Place_Of_Birth: Burnley , Country: England Contents of the table after rollback ID: 1, First_Name: Shikhar, Last_Name: Dhawan, Date_Of_Birth: 1981-12-05, Place_Of_Birth: Delhi, Country: India ID: 2, First_Name: Jonathan, Last_Name: Trott, Date_Of_Birth: 1981-04-22, Place_Of_Birth: CapeTown, Country: SouthAfrica ID: 3, First_Name: Kumara, Last_Name: Sangakkara, Date_Of_Birth: 1977-10-27, Place_Of_Birth: Matale, Country: Srilanka ID: 4, First_Name: Virat, Last_Name: Kohli, Date_Of_Birth: 1988-11-05, Place_Of_Birth: Mumbai, Country: India ID: 5, First_Name: Rohit, Last_Name: Sharma, Date_Of_Birth: 1987-04-30, Place_Of_Birth: Nagpur, Country: India ID: 6, First_Name: Ravindra, Last_Name: Jadeja, Date_Of_Birth: 1988-12-06, Place_Of_Birth: Nagpur, Country: India ID: 7, First_Name: James, Last_Name: Anderson, Date_Of_Birth: 1982-06-30, Place_Of_Birth: Burnley , Country: England ID: 8, First_Name: Ishant, Last_Name: Sharma, Date_Of_Birth: 1998-09-02, Place_Of_Birth: Delhi, Country: India