In database operations, a transaction is an inseparable unit of work consisting of one or more SQL statements updated to the database. The entire transaction can be committed to the database only when all operations in the transaction are completed normally. If one operation is not completed, the entire transaction must be canceled. For example, in a bank transfer transaction, assume that Michael Jacob transfers 1000 yuan from his account to Mr. Li's account. The related SQL statement is as follows: update account set monery = monery-1000 where name = 'hangsan' update account set monery = monery + 1000 where name = 'lisi' the two statements must be processed as a completed transaction. This transaction can be committed only when both are successfully executed. If one sentence fails, the entire transaction must be undone. The connection class provides three methods to control transactions: (1) setautocommit (Boolean autocommit): Set whether to automatically commit transactions; (2) Commit (); Commit transactions; (3) rollback (); undo the transaction. In the jdbc api, the transaction is automatically committed by default. That is to say, each SQL statement updated to the database represents a transaction. After the operation is successful, the system automatically calls commit () to submit the transaction. Otherwise, rollback () is called to cancel the transaction. You can disable automatic transaction commit by calling setautocommit (false. Then, multiple SQL statements for updating the database can be used as a transaction. After all operations are completed, commit () is called for overall commit. If one of the SQL operations fails, the Commit () method is not executed, but sqlexception is generated. In this case, you can capture the Exception Code block and call the rollback () method to cancel the transaction. Try {con = Java. SQL. drivermanager. getconnection (dburl, dbuser, dbpass); con. setautocommit (false); stmt = con. createstatement (); stmt.exe cuteupdate ("update account set monery = monery-1000 where name = 'hangsan '"); stmt.exe cuteupdate ("update account set monery = monery + 1000 where name = 'lisi'"); con. commit ();} catch (exception ex) {ex. printstacktrace (); try {con. rollback ();} catch (exception e) {e. printstacktrace () ;}} finally {try {stmt. close (); con. close ();} catch (exception e) {e. printstacktrace ();}}