jdbc--Batch Processing

Source: Internet
Author: User

1, when the need to mass insert or update records, you can use the Java batch update mechanism, this mechanism allows multiple statements to be submitted to the database batch processing, which can improve processing speed.

2, the JDBC batch processing statement includes two methods:

--1) Addbatch (String): Add SQL statements or parameters that require bulk processing;

--2) ExecuteBatch (); Execute batch processing statements;

3. You will typically need to execute SQL statements in batches of the following two scenarios:

--1) batch processing of multiple SQL statements:

--2) A batch parameter for an SQL statement.

4. Example

 Public voidTestbatch () {Connection conn=NULL; PreparedStatement PS=NULL; String SQL= "INSERT into customers values (?,?,?)"; Try{conn=jdbcutils.getconnection ();                Jdbcutils.starttransaction (conn); PS=conn.preparestatement (SQL); Date Date=NewDate (Newjava.util.Date (). GetTime ()); LongStart =System.currenttimemillis ();  for(inti = 0; I < 100000; i++) {Ps.setint (1, i + 1); Ps.setstring (2, "name_" +i); Ps.setdate (3, date);                        Ps.addbatch (); if((i + 1)% 300 = = 0) {ps.executebatch ();            Ps.clearbatch ();        }} ps.executebatch ();        Ps.clearbatch (); LongEnd =System.currenttimemillis (); System.out.println ("Time:" + (End-start));            JDBCUTILS.COMMIT (conn); }Catch(Exception e) {e.printstacktrace ();    Jdbcutils.rollback (conn); }finally{jdbcutils.release (conn, PS,NULL); }}

jdbc--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.