Processing of Java-language MySQL database transactions

Source: Internet
Author: User

Transaction processing Flow

1, shielded Auto-submit function

2. Handling Transactions

3. Resume Auto-submit function

Code instance

What the data table looks like before executing the program


Import java.sql.*;p ublic class getconnection{public static void Main (string[] args) {access2database adb=new Access2database (); Connection conn=adb.getconn ();//transaction dealingpreparedstatement Pstam=null;try{conn.setautocommit (false); String sql= "Delete from student where name= ' a ' and major=?"; Pstam=conn.preparestatement (SQL);p stam.setstring (1, "Chinese");p stam.executeupdate ();
<span style= "White-space:pre" ></span><pre name= "code" class= "java" ><span style= "White-space: Pre "></span>conn.rollback ();
<span style= "White-space:pre" ></span>conn.commit (); catch (SQLException e) {try {conn.rollback ();} catch (SQLException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} E.printstacktrace ();} Finally{try {Conn.setautocommit (true);} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Release the resource of the Programtry{pstam.close (); Conn.close ();} catch (SQLException e) {e.printstacktrace ();}}}
After the appearance


Visible no change, transaction rollback successful

======================================================================

In addition to this application, you can also save the middle state of the transaction, and finally restore to this intermediate save state

The state before the data table


Look at the code

Import java.sql.*;p ublic class getconnection{public static void Main (string[] args) {access2database adb=new Access2database (); Connection conn=adb.getconn ();//transaction dealingpreparedstatement Pstam=null;try{conn.setautocommit (false); String sql= "Delete from student where name= ' a ' and major=?"; Pstam=conn.preparestatement (SQL);p stam.setstring (1, "Chinese");p stam.executeupdate ();//conn.commit (); SavePoint sp=conn.setsavepoint (); sql= "INSERT into student (Name,major,score) VALUES (' G ', ' Math ', ' 99 ');"; Pstam=conn.preparestatement (SQL);p stam.executeupdate (); Conn.rollback (sp); Conn.commit ();} catch (SQLException e) {try {conn.rollback ();} catch (SQLException E1) {//TODO auto-generated catch Blocke1.printstacktrace ();} E.printstacktrace ();} Finally{try {Conn.setautocommit (true);} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Release the resource of the Programtry{pstam.close (); Conn.close ();} catch (SQLException e) {e.printstacktrace ();}}}




Processing of Java-language MySQL database transactions

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.