JDBC Review 4 batch execution SQL

Source: Internet
Author: User
Tags bulk insert

1 using JDBC for bulk execution SQL in the actual project development, sometimes you need to send a batch of SQL statement execution to the database, you should avoid sending execution to the database, but should adopt the batch processing mechanism of JDBC, in order to improve the execution efficiency.

Package Dbex.mysql;import Java.io.ioexception;import Java.sql.connection;import java.sql.preparedstatement;import Java.sql.resultset;import java.sql.sqlexception;import java.util.date;import Dbex. Dbutil;public class Batchsql {/** * @throws IOException * @throws SQLException * @Title: Dobatch *        @Description: Batch operation with JDBC * @param * @throws */void Dobatch () throws SQLException, ioexception{        Connection conn=dbutil.getconnection ();        PreparedStatement ppst = null;        ResultSet rs = null;            try {Long startTime = System.currenttimemillis ();            Ppst = Conn.preparestatement ("INSERT into CLOB values (?, ' the ' This is a test for batch SQL ')");                for (int i = 222; i < 100222; i++) {ppst.setint (1, i);            Ppst.addbatch ();            } ppst.executebatch ();            Long endTime = System.currenttimemillis (); System.out.println (New Date (). toLocaleString () + "Execute batchThe amount of time "+ (Endtime-starttime)/1000+" s ");        } catch (Exception e) {e.printstacktrace ();        }finally{Dbutil.closeall (conn, ppst, RS);    }} public static void Main (string[] args) throws SQLException, IOException {new Batchsql (). Dobatch (); }}

Advantages
    • SQL is pre-compiled with guaranteed execution efficiency

      Disadvantages
    • This form can only be applied in the case of bulk Insert data or batch update

JDBC Review 4 batch execution SQL

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.