One-point record of the Java JDBC database: Transaction

Source: Internet
Author: User

 
One-point record of the Java JDBC database: Transaction

Import Java. SQL. *; public class testbatch {public static void main (string [] ARGs) throws sqlexception {connection conn = NULL; statement stmt = NULL; preparedstatement pstmt = NULL; try {class. forname ("com. mySQL. JDBC. driver "); Conn = drivermanager. getconnection ("JDBC: mysql: // localhost/test", "root", "Zhen"); Conn. setautocommit (false); // transaction 1 transactionstmt = Conn. createstatement (); // execute a static SQL statement and return Object. Stmt. addbatch ("insert into student values (30, 'ddd ', 12, 'II')"); stmt. addbatch ("insert into student values (31, 'ddd ', 12, 'II')"); stmt. addbatch ("insert into student values (32, 'ddd ', 12, 'II')"); stmt. addbatch ("insert into student values (33, 'ddd ', 12, 'II')" Wait until stmt.exe cutebatch (); // transaction 2 pstmt = Conn. preparestatement ("insert into student values (?,?,?,?) "); Pstmt. setint (1, 40); pstmt. setstring (2, "QQ"); pstmt. setint (3, 5); pstmt. setstring (4, "ww"); pstmt. addbatch (); pstmt. setint (1, 41); pstmt. setstring (2, "QQ"); pstmt. setint (3, 5); pstmt. setstring (4, "ww" using fig (); // Conn. commit (); // If transaction 1 and transaction 2 must be completed at the same time or not at the same time, and only one of them cannot be completed, use transaction} catch (classnotfoundexception E1) {e1.printstacktrace ();} catch (sqlexception e) {e. printstacktrace (); Conn. rollback (); // if the transaction processing is lost, the conn must be rolled back. setautocommit (true);} finally {stmt. close (); pstmt. close (); Conn. close ();}}}

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.