JAVA learning notes ()-Transaction and batch processing, javatransaction

Source: Internet
Author: User

JAVA learning notes ()-Transaction and batch processing, javatransaction
Transaction

/** Transaction * JDBC Enables automatic Transaction commit by default. You need to disable the Transaction to manually control the Transaction. ** by default, the MySQL storage engine is My ..., it does not support transaction processing. It is changed to InnoDB engine * create table Name () engine = InnoDB default charset = utf8; ** the InnoDB engine disables automatic transaction commit by default, modify MySQL to automatically submit a transaction * set autocommit = 1; // 1 indicates that the transaction is automatically committed, and 0 indicates that the automatic commit is disabled * show variables like 'autocommit '; */public class Test01 {public static void main (String [] args) {Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = n Ull; conn = DBUtil. getConnection (); try {conn. setAutoCommit (false); // close the automatic commit transaction and manually control the transaction String SQL = "insert into user values (null, 'Eee ', '000000')"; pstmt = conn. prepareStatement (SQL); pstmt.exe cuteUpdate (); // insert the first user String sql2 = "insert into user values ('fff', '000000')"; pstmt = conn. prepareStatement (sql2); pstmt.exe cuteUpdate (); // Insert the second user conn. commit (); // submit the transaction System. out. println ("User inserted successfully! ");} Catch (SQLException e) {System. out. println ("exception occurred, rollback transaction"); try {conn. rollback (); // rollback transaction} catch (SQLException e1) {e1.printStackTrace () ;}} finally {DBUtil. closeAll (rs, pstmt, conn );}}}
Batch Processing
/** Batch Processing */public class Test02 {Connection conn = null; Statement stmt = null; PreparedStatement pstmt = null; ResultSet rs = null; public void test01 () {try {conn = DBUtil. getConnection (); stmt = conn. createStatement (); // Add a batch processing statement stmt. addBatch ("insert into user values (null, 's001', '000000')"); stmt. addBatch ("insert into user values (null, 's002 ', '000000')"); stmt. addBatch ("insert into user values (null, 'S003 ', '000000') "); // execute the batch processing int [] nums = stmt.exe cuteBatch (); System. out. println (Arrays. toString (nums);} catch (SQLException e) {e. printStackTrace ();} finally {DBUtil. closeAll (rs, stmt, conn) ;}} public void test02 () {try {conn = DBUtil. getConnection (); conn. setAutoCommit (false); pstmt = conn. prepareStatement ("insert into user values (null ,?,?) "); // Add the batch processing statement pstmt. setObject (1, "t001"); pstmt. setObject (2, "123"); pstmt. addBatch (); pstmt. setObject (1, "t002"); pstmt. setObject (2, "123"); pstmt. addBatch (); pstmt. setObject (1, "t003"); pstmt. setObject (2, "123"); pstmt. addBatch (); // execute batch processing int [] nums?pstmt.exe cuteBatch (); System. out. println (Arrays. toString (nums); conn. commit ();} catch (SQLException e) {try {conn. rollback ();} catch (SQLException e1) {e1.printStackTrace ();} e. printStackTrace ();} finally {DBUtil. closeAll (rs, stmt, conn) ;}} public static void main (String [] args) {Test02 test = new Test02 (); test. test02 ();}}

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.