文章目錄
#相關免費學習推薦:mysql影片教學
一、MySQL的事務控制(Transaction Control Language)
(1)事務的特性(ACID)
事務指的是邏輯上的一組操作,組成這組操作的邏輯單元要麼一起成功,要麼一起失敗。
(2)MySQL的交易控制
set autocommit = false;
rollback;
commit ;
#(3)mysql事務示範
☆查看已經存在的emp表:
mysql> select * from emp;+-------+--------+--------+------+------------+----------+--------+--------+| empno | ename | job | mgr | hiredate | sal | commit | deptno |+-------+--------+--------+------+------------+----------+--------+--------+| 1002 | 白展堂 | clerk | 1001 | 1983-05-09 | 7000.00 | 200.00 | 10 || 1003 | 李大嘴 | clerk | 1002 | 1980-07-08 | 8000.00 | 100.00 | 10 || 1004 | 吕秀才 | clerk | 1002 | 1985-11-12 | 4000.00 | NULL | 10 || 1005 | 郭芙蓉 | clerk | 1002 | 1985-03-04 | 4000.00 | NULL | 10 || 1007 | 小白 | clerk | 1001 | 2019-11-25 | 5555.00 | 500.00 | NULL || 2001 | 胡一菲 | leader | NULL | 1994-03-04 | 15000.00 | NULL | 20 || 2002 | 陈美嘉 | manger | 2001 | 1993-05-24 | 10000.00 | 300.00 | 20 || 2003 | 吕子乔 | clerk | 2002 | 1995-05-19 | 7300.00 | 100.00 | 20 || 2004 | 张伟 | clerk | 2002 | 1994-10-12 | 8000.00 | 500.00 | 20 || 2005 | 曾小贤 | clerk | 2002 | 1993-05-10 | 9000.00 | 700.00 | 20 || 3001 | 刘梅 | leader | NULL | 1968-08-08 | 13000.00 | NULL | 30 || 3002 | 夏冬梅 | manger | 3001 | 1968-09-21 | 10000.00 | 600.00 | 30 || 3003 | 夏雪 | clerk | 3002 | 1989-09-21 | 8000.00 | 300.00 | 30 || 3004 | 张一山 | clerk | 3002 | 1991-06-16 | 8000.00 | 200.00 | 30 || 3007 | 嫦娥 | clerk | NULL | NULL | NULL | NULL | 10 |+-------+--------+--------+------+------------+----------+--------+--------+15 rows in set (0.00 sec)
①設定手動提交:
mysql> set autocommit=false;Query OK, 0 rows affected (0.03 sec)
②在emp表中插入一則ename為mary的語句:
mysql> insert into emp(ename,job,commit) -> values('mary','clerk',300);Query OK, 1 row affected (0.02 sec)mysql> select * from emp;+-------+--------+--------+------+------------+----------+--------+--------+| empno | ename | job | mgr | hiredate | sal | commit | deptno |+-------+--------+--------+------+------------+----------+--------+--------+| 1002 | 白展堂 | clerk | 1001 | 1983-05-09 | 7000.00 | 200.00 | 10 || 1003 | 李大嘴 | clerk | 1002 | 1980-07-08 | 8000.00 | 100.00 | 10 || 1004 | 吕秀才 | clerk | 1002 | 1985-11-12 | 4000.00 | NULL | 10 || 1005 | 郭芙蓉 | clerk | 1002 | 1985-03-04 | 4000.00 | NULL | 10 || 1007 | 小白 | clerk | 1001 | 2019-11-25 | 5555.00 | 500.00 | NULL || 2001 | 胡一菲 | leader | NULL | 1994-03-04 | 15000.00 | NULL | 20 || 2002 | 陈美嘉 | manger | 2001 | 1993-05-24 | 10000.00 | 300.00 | 20 || 2003 | 吕子乔 | clerk | 2002 | 1995-05-19 | 7300.00 | 100.00 | 20 || 2004 | 张伟 | clerk | 2002 | 1994-10-12 | 8000.00 | 500.00 | 20 || 2005 | 曾小贤 | clerk | 2002 | 1993-05-10 | 9000.00 | 700.00 | 20 || 3001 | 刘梅 | leader | NULL | 1968-08-08 | 13000.00 | NULL | 30 || 3002 | 夏冬梅 | manger | 3001 | 1968-09-21 | 10000.00 | 600.00 | 30 || 3003 | 夏雪 | clerk | 3002 | 1989-09-21 | 8000.00 | 300.00 | 30 || 3004 | 张一山 | clerk | 3002 | 1991-06-16 | 8000.00 | 200.00 | 30 || 3007 | 嫦娥 | clerk | NULL | NULL | NULL | NULL | 10 || 4008 | mary | clerk | NULL | NULL | NULL | 300.00 | NULL |+-------+--------+--------+------+------------+----------+--------+--------+16 rows in set (0.00 sec)
③使用rollback
回滾
mysql> rollback;Query OK, 0 rows affected (0.00 sec)mysql> select * from emp;+-------+--------+--------+------+------------+----------+--------+--------+| empno | ename | job | mgr | hiredate | sal | commit | deptno |+-------+--------+--------+------+------------+----------+--------+--------+| 1002 | 白展堂 | clerk | 1001 | 1983-05-09 | 7000.00 | 200.00 | 10 || 1003 | 李大嘴 | clerk | 1002 | 1980-07-08 | 8000.00 | 100.00 | 10 || 1004 | 吕秀才 | clerk | 1002 | 1985-11-12 | 4000.00 | NULL | 10 || 1005 | 郭芙蓉 | clerk | 1002 | 1985-03-04 | 4000.00 | NULL | 10 || 1007 | 小白 | clerk | 1001 | 2019-11-25 | 5555.00 | 500.00 | NULL || 2001 | 胡一菲 | leader | NULL | 1994-03-04 | 15000.00 | NULL | 20 || 2002 | 陈美嘉 | manger | 2001 | 1993-05-24 | 10000.00 | 300.00 | 20 || 2003 | 吕子乔 | clerk | 2002 | 1995-05-19 | 7300.00 | 100.00 | 20 || 2004 | 张伟 | clerk | 2002 | 1994-10-12 | 8000.00 | 500.00 | 20 || 2005 | 曾小贤 | clerk | 2002 | 1993-05-10 | 9000.00 | 700.00 | 20 || 3001 | 刘梅 | leader | NULL | 1968-08-08 | 13000.00 | NULL | 30 || 3002 | 夏冬梅 | manger | 3001 | 1968-09-21 | 10000.00 | 600.00 | 30 || 3003 | 夏雪 | clerk | 3002 | 1989-09-21 | 8000.00 | 300.00 | 30 || 3004 | 张一山 | clerk | 3002 | 1991-06-16 | 8000.00 | 200.00 | 30 || 3007 | 嫦娥 | clerk | NULL | NULL | NULL | NULL | 10 |+-------+--------+--------+------+------------+----------+--------+--------+15 rows in set (0.00 sec)
發現mary沒有插入成功,因為手動提交後進行和回滾;如果使用commit進行提交就無法回滾成功,因為直接插入進資料庫中了。
二、JDBC的交易控制
(1)JDBC交易簡介
(2)JDBC交易的API
conn.setAutoCommit(false);//将JDBC事务设置手动提交conn.commit();conn.rollback();
(3)JDBC交易控制模擬
(模擬交易)TrasactionDemo:#####在emp表中修改ename名為hellen的記錄,將其job從leader修改為clerk,獎金commit從600降低至300。
package jdbc;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import util.JdbcUtil;/** * JDBC事务控制 * * @author Administrator * */public class TestTrasaction { public static void main(String[] args) { Connection conn = null; PreparedStatement pstm = null; PreparedStatement pstm1 = null; try { // 获得连接 conn = JdbcUtil.getConnection(); //将JDBC事务设置手动提交 conn.setAutoCommit(false); // ①降职操作 String sql = "updata emp set job = 'clerk' where ename = 'hellen'"; pstm = conn.prepareStatement(sql); pstm.executeUpdate(); // ②降奖金操作 String sql1 = "updata emp set commit='3000' where ename = 'hellen'"; pstm1 = conn.prepareStatement(sql1); pstm1.executeUpdate(); //提交事务 conn.commit(); } catch (Exception e) { //事务回滚 try { conn.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } } finally { try { JdbcUtil.release(null, pstm, null); JdbcUtil.release(null, pstm1, conn); } catch (Exception e) { e.printStackTrace(); } } }}
以上是介紹MySQL和JDBC的事務控制(TCL)的詳細內容。更多資訊請關注PHP中文網其他相關文章!