MySQL turns off batching by default, so we store 10,000 of data in the database by default (batch is not open), with the following core code:
PackageCn.itcast.demo5;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importjava.sql.SQLException;Importorg.junit.Test;Importcn.itcast.demo3.JdbcUtils; Public classDemo5 {@Test Public voidFun5 ()throwsSQLException {/** PSTMT: * > Add parameters to the batch * > Execute batch! */Connection con=jdbcutils.getconnection (); String SQL= "INSERT into T_stu VALUES (?,?,?,?)"; PreparedStatement pstmt=con.preparestatement (SQL); //crazy to add parameters for(inti = 0; I < 10000; i++) {Pstmt.setint (1, i+1); Pstmt.setstring (2, "stu_" +i); Pstmt.setint (3, i); Pstmt.setstring (4, i%2==0? " Male ":" Female "); Pstmt.addbatch ();//Add Batch! This set of parameters is saved to the collection. } LongStart =System.currenttimemillis (); Pstmt.executebatch ();//Execute the batch! LongEnd =System.currenttimemillis (); System.out.println (End-start); }}
The above program execution end time consuming 412764MS
This is the batch that opens MySQL, open in the way:
Url=jdbc:mysql://localhost:3306/exam the MySQL parameter
Change to Url=jdbc:mysql://localhost:3306/exam?rewritebatchedstatements=true
Execute the program again, time consuming 301MS, speed 1000 times times faster than!
Batch processing for MySQL