It was always said that bulk INSERT and update efficiency is higher than non-bulk, but has not used the bulk processing of data, and now because the project needs to deal with a large amount of data, so the use of bulk processing features, Java code is as follows:
1.java implements BULK INSERT data:
Connection Conn; Try{class.forname ("Com.mysql.jdbc.Driver"); Conn= Drivermanager.getconnection ("Jdbc:mysql://192.168.0.200:3306/xxx", "root", "root"); Conn.setautocommit (false); String SQL= "INSERT into Test_user (u_name,create_date) value (?, Sysdate ())"; PreparedStatement Prest=conn.preparestatement (SQL, resultset.type_scroll_sensitive, resultset.concur_read_only); Conn.setautocommit (false); for(inti = 1; I <= 100; i++) {prest.setstring (1, "a" +i); Prest.addbatch (); } prest.executebatch (); Conn.commit (); Conn.close (); } Catch(Exception ex) {ex.printstacktrace (); }
2.java implements batch update data:
Connection Conn; Try{class.forname ("Com.mysql.jdbc.Driver"); Conn= Drivermanager.getconnection ("Jdbc:mysql://192.168.0.200:3306/xxx", "root", "root"); Conn.setautocommit (false); //Save current Auto-commit mode BooleanAutocommit =Conn.getautocommit (); //Turn off auto-commitConn.setautocommit (false); Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_read_only); for(inti = 1; I <= num; i++) { //multiple SQL statements can be inserted at once by traversalStmt.addbatch ("update test_user set test_user.u_name = (' d" +i+ "') Where Test_user.u_name = (' C ' +i+" ') "); } stmt.executebatch (); Stmt.clearbatch (); Conn.commit (); Conn.close (); } Catch(Exception ex) {ex.printstacktrace (); }
Java BULK INSERT Update operation