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