1, when the need to mass insert or update records, you can use the Java batch update mechanism, this mechanism allows multiple statements to be submitted to the database batch processing, which can improve processing speed.
2, the JDBC batch processing statement includes two methods:
--1) Addbatch (String): Add SQL statements or parameters that require bulk processing;
--2) ExecuteBatch (); Execute batch processing statements;
3. You will typically need to execute SQL statements in batches of the following two scenarios:
--1) batch processing of multiple SQL statements:
--2) A batch parameter for an SQL statement.
4. Example
Public voidTestbatch () {Connection conn=NULL; PreparedStatement PS=NULL; String SQL= "INSERT into customers values (?,?,?)"; Try{conn=jdbcutils.getconnection (); Jdbcutils.starttransaction (conn); PS=conn.preparestatement (SQL); Date Date=NewDate (Newjava.util.Date (). GetTime ()); LongStart =System.currenttimemillis (); for(inti = 0; I < 100000; i++) {Ps.setint (1, i + 1); Ps.setstring (2, "name_" +i); Ps.setdate (3, date); Ps.addbatch (); if((i + 1)% 300 = = 0) {ps.executebatch (); Ps.clearbatch (); }} ps.executebatch (); Ps.clearbatch (); LongEnd =System.currenttimemillis (); System.out.println ("Time:" + (End-start)); JDBCUTILS.COMMIT (conn); }Catch(Exception e) {e.printstacktrace (); Jdbcutils.rollback (conn); }finally{jdbcutils.release (conn, PS,NULL); }}
jdbc--Batch Processing