JDBC Batch Processing

Source: Internet
Author: User

Statement execute () and other methods can only execute one SQL statement at a time. If Multiple SQL statements are to be executed at the same time, you can use addbatch () the method will only contain the SQL statement and then execute the executebatch () method. In this way, multiple SQL statements are executed in one method call to improve the execution efficiency.

To ensure the success or failure of all these statements, you should place the batch processing in the transaction:

Try {

Conn. setautocommite (false); // set Automatic commit transaction to false

Statement stmt = conn. createstatement ();

Stmt. addbatch ("…"); // Add an SQL statement

Stmt. addbatch ("…"); // Add an SQL statement

...

Stmt.exe cutebatch (); // execute batch processing

Conn. Commit (); // submit the transaction

} Catch (sqlexception e ){

Try {

Conn. rollback (); // roll back the transaction

} Catch (sqlexceptione1 ){

E 1. printstacktrace ();

}

E. printstacktrace ();

} Finally {

Try {

Conn. setautocommit (true); // set Automatic commit transaction to true

} Catch (sqlexceptione ){

E. printstacktrace ();

}

// Close the resource

...

}

You can also use preparedstatement for batch processing:

Try {

// Start the transaction ------ set the automatic commit transaction to false

Conn. setautocommit (false );

Pstmt = conn. preparedstatement (SQL );

Pstmt. setxxx (1 ,...); // Assign a placeholder Value

...

// Add these operations to batch processing

Pstmt. addbatch ();

 

Pstmt. setxxx (1 ,...); // Assign a placeholder Value

...

// Add these operations to batch processing

Pstmt. addbatch ();

 

Pstmt.exe cutebatch (); // execute batch processing

// Submit the transaction

Conn. Commit ();

} Catch (sqlexception e ){

// Roll back the transaction ------- cancel all operations

Try {

Conn. rollback ();

} Catch (sqlexceptione1 ){

E 1. printstacktrace ();

}

E. printstacktrace ();

} Finally {

// Close the transaction ----- set the automatic commit transaction to true

Try {

Conn. setautcommit (true );

} Catch (sqlexceptione ){

E. printstracktrace ();

}

// Close the resource

...

}

The statements executed in batch processing can only be update statements (insert, delete, and update). Otherwise, an exception is thrown.

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.