Statement batch update

Source: Internet
Author: User

To perform a statement batch update, you must turn off auto-commit. in Java (TM) database connectivity (JDBC), auto-commit is on by default. auto-commit means any updates to the database are committed after each SQL statement is processed. if you want to treat a group of statements being handed to the database as one functional group, you do not want the database committing each statement individually. if you do not turn off auto-commit and a statement in the middle of the batch fails, you cannot roll back the entire batch and try it again because half of the statements have been made final. further, the additional work of committing each statement in a batch creates a lot of overhead. see transactions for more details.

After turning off auto-commit, you can create a standard statement object. instead of processing statements with methods such as executeupdate, you add them to the batch with the addbatch method. once you have added all the statements you want to the batch, You can process all of them with the executebatch method. you can empty the batch at anytime with the clearbatch method.

The following example shows how you can use these methods:

Example:Statement batch update

Note:Read the code example disclaimer for important legal information.

Code
1 Connection. setautocommit ( False );
2 Statement statement = Connection. createstatement ();
3 Statement. addbatch ( " Insert into tablex values (1, 'cujo ') " );
4 Statement. addbatch ( " Insert into tablex values (2, 'fred ') " );
5 Statement. addbatch ( " Insert into tablex values (3, 'mark ') " );
6 Int [] Counts = Statement.exe cutebatch ();
7 Connection. Commit ();

 

In this example, an array of integers is returned from the executebatch method. this array has one integer value for each statement that is processed in the batch. if values are being inserted into the database, the value for each statement is 1 (that is, Assuming successful processing ). however, some of the statements may be update statements that affect multiple rows. if you put any statements in the batch other than insert, update, or delete, an exception occurs.

Backtrack:Https://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp? Topic =/rzaha/batchw.htm

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.