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