JDBC Batch Processing

Source: Internet
Author: User

The necessary text description:

When you need to insert or update a record in batches. Java can be used in the batch update mechanism,

This mechanism allows multiple statements to be submitted to the database for batch processing at one time. Usually more efficient than single-commit processing;
The batch processing statements for JDBC include the following two methods:
Addbatch (String): Add SQL statements or parameters that require bulk processing;
ExecuteBatch (); Execute batch processing statements;
Typically we encounter two scenarios in which SQL statements are executed in bulk:
batch processing of multiple SQL statements;

A batch parameter for a SQL statement;

Concise Code:

    /** * < A sentence function description > batch processing Insert 10,000 data * < feature details > * @see [Class, Class # method, Class # Member] */public void update ()        {String sql = "INSERT into T_EMP13 (first_name,salary) VALUES (?,?)";        Connection conn = null;        PreparedStatement preparedstatement = null;                ResultSet result = null;                Long begin = System.currenttimemillis ();            try {conn = Getconn ();            Conn.setautocommit (FALSE);                        PreparedStatement = conn.preparestatement (sql); Place the parameter in the unknown parameter of PreparedStatement for (int i = 0; i < 10000; i++) {Prep                Aredstatement.setstring (1, "name_" + i);                Preparedstatement.setobject (2, I);                Accumulate SQL Preparedstatement.addbatch (); Once saved to a certain extent, and emptied of the accumulated SQL if (0 = = (i + 1)%) {Preparedstatemen              T.executebatch ();      Preparedstatement.clearbatch ();                }} if (0! = 10000) {Preparedstatement.executebatch ();            Preparedstatement.clearbatch ();                        } conn.commit ();            Long end = System.currenttimemillis ();        System.out.println (Begin-end);        } catch (Exception e) {e.printstacktrace ();        } finally {Releasesource (PreparedStatement, conn, result);     }            }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JDBC Batch Processing

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.