Java BULK INSERT Update operation

Source: Internet
Author: User
Tags bulk insert

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

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.