您的位置:首页 > 编程语言 > Java开发

JAVA WEB - JDBC事务开发

2017-06-26 10:40 465 查看
###1.mysql事务操作

*1.Start Transaction 开启事务。

*2.Commit 提交事务。

*3.rollback 回滚事务。

1.1.举例说明

*首先在mysql中创建表和数据。

CREATE TABLE account(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(50),
money DOUBLE
);
INSERT INTO account(id,NAME,money) VALUES(NULL,'jack',10000);
INSERT INTO account(id,NAME,money) VALUES(NULL,'rose',20000);


*打开CMD,启动mysql。

C:\Users\wangfra>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 314
Server version: 5.7.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql
+--------------------+
| Database           |
+--------------------+
| information_schema |
| boxuegu            |
| frank1             |
| mydb1              |
| mysql              |
| performance_schema |
| sys                |
| web_db3            |
| webdb_1            |
| webdb_4            |
+--------------------+

Database changed
mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | jack | 10000 |
|  2 | rose | 20000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> update account set money = money +1000 where name = 'jack';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update account set money = money +1000 where name = 'jack';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | jack | 12000 |
|  2 | rose | 20000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> start transaction;
Query OK, 0 rows affected (0.00 sec)

mysql> update account set money = money -1000 where name = 'jack';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | jack | 11000 |
|  2 | rose | 20000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> update account set money = money -1000 where name = 'jack';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from account;
+----+------+-------+
| id | name | money |
+----+------+-------+
|  1 | jack | 10000 |
|  2 | rose | 20000 |
+----+------+-------+
2 rows in set (0.00 sec)

mysql> update account set money = money + 1000 where name = 'jack';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> update account set money = money + 2000 where name = 'jack';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> commit;
Query OK, 0 rows affected (0.01 sec)

mysql>


*
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java
相关文章推荐