20160322 javaweb jdbc--MySQL batch processing

Source: Internet
Author: User

 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.