The Black Horse Day11 Business Introduction case

Source: Internet
Author: User
Tags savepoint

Transactions: A transaction is a logical set of operations that either executes at the same time or does not complete at the same time

Management of transactions: By default, the database automatically manages transactions, which is managed by a single statement that is exclusive of a transaction.

If you need to control the transaction yourself, you can also open/commit/rollback the transaction with the following command

Start transaction

Commit

Rollback

Transaction Management for JDBC:

Conn.setautocommit (FALSE)//set itself to open transaction

Conn.commit ()//COMMIT TRANSACTION

Conn.rollback ()//ROLLBACK TRANSACTION

SavePoint sp=con.setsavepoint ()//Set rollback point

Conn.rollback (SP)//rollback to the specified rollback point

Case:

Database:

Mysql> CREATE TABLE account (    ID int primary key anto_increment,    name varchar, and money    D ouble);
To view the database:

Mysql> SELECT * FROM account;+----+------+-------+| ID | name | Money |+----+------+-------+|  1 | A    |   | |  2 | b    |   |+----+------+-------+
Transationdemo:

Package Cn.itheima.transation;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import Java.sql.savepoint;import cn.itheima.utils.JDBCUtils; public class TransationDemo1 {public static void main (string[] args) {Connection con=null; PreparedStatement Ps=null; ResultSet Rs=null; SavePoint save=null;try {con=jdbcutils.getconnection ();//database default is a SQL statement is a transaction, that is, execute an SQL statement is automatically committed, We need to change the default setting for the database to Falsecon.setautocommit (false);p s=con.preparestatement ("Update account set money=money-10 where name= ?"); Ps.setstring (1, "a");p s.executeupdate ();p s=con.preparestatement ("Update account set money= money+10 where name=?");  Ps.setstring (1, "B");p s.executeupdate ();//Set a rollback point, save=con.setsavepoint ();p s=con.preparestatement ("Update Account set money=money-10 where Name=? "); Ps.setstring (1, "a");p s.executeupdate ();p s=con.preparestatement ("Update account set money= money+10 where name=?"); Ps.setstring (1, "B");p s.executeupdate (); int i=1/0;//commit a transaction, from opening a transaction to committingA transaction this is a transaction, these SQL statements are Con.commit () in a transaction;} catch (Exception e) {e.printstacktrace ();//If an exception is thrown, I also commit the transaction try {if (save==null) {//description does not execute to the rollback point throws an exception Con.rollback ();} else{//is not NULL, it rolls back to the set rollback point con.rollback (save); Con.commit ();}} catch (SQLException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();}} Finally{jdbcutils.closeresource (RS, ps, con);}}}
Re-check the database:

We found

Mysql> select * from account;
+----+------+-------+
| ID | name | Money |
+----+------+-------+
| 1 |    A | 90 |
| 2 |   B | 110 |
+----+------+-------+




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The Black Horse Day11 Business Introduction case

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.