PackageCom.dzq.batch;Importjava.sql.Connection;Importjava.sql.Statement;Importcom.itheima.util.JDBCUtils;/*CREATE DATABASE Day10batch; Use Day10batch; CREATE TABLE Batchdemo (ID int primary key auto_increment, name varchar (20)); INSERT into Batchdemo values (null, ' AAAA '); INSERT into Batchdemo values (null, ' BBB '); INSERT into Batchdemo values (null, ' CC '); INSERT into Batchdemo values (null, ' d '); *//*Statement Way Batch processing: Advantages: You can execute multiple SQL statements with different structures disadvantage: no pre-compilation mechanism, inefficient, if you want to execute multiple structures with the same parameters only different SQL, still need to write multiple SQL statement skeleton*/ Public classStatementbatch { Public Static voidMain (string[] args) {Connection conn=NULL; Statement Stat=NULL; Try{conn=Jdbcutils.getconn (); Stat=conn.createstatement (); Stat.addbatch ("CREATE Database Day10batch"); Stat.addbatch ("Use Day10batch"); Stat.addbatch ("CREATE TABLE Batchdemo (" + "ID int primary key auto_increment," + "Name varchar (20)" + ")"); Stat.addbatch ("INSERT into Batchdemo values (null, ' AAAA ')"); Stat.addbatch ("INSERT into Batchdemo values (null, ' BBB ')"); Stat.addbatch ("INSERT into Batchdemo values (null, ' CC ')"); Stat.addbatch ("INSERT into Batchdemo values (null, ' d ')"); Stat.executebatch (); }Catch(Exception e) {e.printstacktrace (); }finally{jdbcutils.close (NULL, STAT, conn); } }}
PackageCom.dzq.batch;Importjava.sql.Connection;Importjava.sql.PreparedStatement;Importcom.itheima.util.JDBCUtils;/*CREATE TABLE Psbatch (ID int primary key auto_increment, name varchar ());*//*prparedstatement implementation of batch processing: advantages: There is a pre-compilation mechanism, high efficiency. When you execute multiple structures that have the same parameters, you do not need to repeat the skeleton of sql: You can only execute SQL with different backbone parameters, there is no way to Add SQL with different structure in one batch*/ Public classPsbatch { Public Static voidMain (string[] args) {Connection conn=NULL; PreparedStatement PS=NULL; Try{conn=Jdbcutils.getconn (); PS= Conn.preparestatement ("INSERT into psbatch values (null,?)"); for(inti=1;i<=100000;i++) {ps.setstring (1, "name" +i); Ps.addbatch (); if(i%1000==0) {ps.executebatch (); Ps.clearbatch (); }} ps.executebatch (); }Catch(Exception e) {e.printstacktrace (); }finally{jdbcutils.close (NULL, PS, conn); } }}
20160322 javaweb jdbc--MySQL batch processing