JDBC BULK INSERT and update

Source: Internet
Author: User
Tags bulk insert getmessage rollback stmt
Bulk Add
    public void Updatefiledata (String sSQL, arraylist<string[]> objparams) {getconn ();

        int iresult = 0;
            try {//Statement PS = _conn.createstatement ();
            PreparedStatement PreparedStatement = _conn.preparestatement (sSQL);
                for (int i = 0; I <objparams.size (); i++) {preparedstatement.setstring (1,objparams.get (i) [0]);
                Preparedstatement.setstring (2,objparams.get (i) [1]);
                Preparedstatement.setstring (3,objparams.get (i) [2]);
                Preparedstatement.setlong (4,integer.parseint (Objparams.get (i) [3]));
                Preparedstatement.setstring (5,objparams.get (i) [4]);
                Preparedstatement.setstring (6,objparams.get (i) [5]);
            Preparedstatement.addbatch ();
        } preparedstatement.executebatch ();

        catch (Exception ex) {System.out.println (Ex.getmessage ());
    finally {        Closeconn (); }

    }
references to online examples
Import java.sql.Connection;
Import Java.sql.Date;
Import java.sql.PreparedStatement;

Import java.sql.Statement;

Import Org.junit.Test; Import Xuezaipiao1.
Jdbc_tools; 
     /** * Add 100,000 records to Oracle's TEMP datasheet * Test how to insert, shortest/public class Jdbctest {/** * * 1. Use Statement.
        * Test When: 35535 */@Test public void Testbbatchstatement () {Connection conn = null;
        Statement Statement = null;
        String sql = null;
            try {conn = jdbc_tools.getconnection ();

            JDBC_TOOLS.BEGINTX (conn);
            Long begintime = System.currenttimemillis ();
            statement = Conn.createstatement (); for (int i = 0;i<100000;i++) {sql = "INSERT into temp values (" + (i+1) + ", ' name_" +
                (i+1) + "', ' 1 March-June-15 ')";
            Statement.executeupdate (SQL);
            Long endtime = System.currenttimemillis (); System.out.println ("Time:" + (Endtime-begintime));
        JDBC_TOOLS.COMMIT (conn);
            catch (Exception e) {e.printstacktrace ();
        Jdbc_tools.rollback (conn);
        }finally{Jdbc_tools.relasesource (conn, statement); }/** * Using PreparedStatement * Test: 9717/@Test public void Testbbatchpreparedstatemen
        T () {Connection conn = null;
        PreparedStatement PS = null;
        String sql = null;
            try {conn = jdbc_tools.getconnection ();

            JDBC_TOOLS.BEGINTX (conn);
            Long begintime = System.currenttimemillis ();
            sql = "INSERT into temp values (?,?,?)";
            PS = conn.preparestatement (SQL);
            Date date = new Date (new Java.util.Date (). GetTime ());
                for (int i = 0;i<100000;i++) {ps.setint (1, i+1);
                Ps.setstring (2, "name_" +i);
                Ps.setdate (3, date); Ps.executeupdate ();//9717} long Endtime = System.currenttimemillis ();
            System.out.println ("Time:" + (Endtime-begintime));
        JDBC_TOOLS.COMMIT (conn);
            catch (Exception e) {e.printstacktrace ();
        Jdbc_tools.rollback (conn);
        }finally{Jdbc_tools.relasesource (conn, PS);
        }/** * Test when: 658/@Test public void Testbbatch () {Connection conn = null;
        PreparedStatement PS = null;
        String sql = null;
            try {conn = jdbc_tools.getconnection ();

            JDBC_TOOLS.BEGINTX (conn);
            Long begintime = System.currenttimemillis ();
            sql = "INSERT into temp values (?,?,?)";
            PS = conn.preparestatement (SQL);
            Date date = new Date (new Java.util.Date (). GetTime ());
                for (int i = 0;i<100000;i++) {ps.setint (1, i+1);
                Ps.setstring (2, "name_" +i);

                Ps.setdate (3, date); //Accumulate SQL Ps.addbatch ();
                    When accumulated to a certain extent, execute once, and empty the record if ((i+1)% 300==0) {ps.executebatch ();
                Ps.clearbatch ();
                The total bar number is not a batch value integer multiple, you also need to perform an if (100000%!= 0) {ps.executebatch ();
            Ps.clearbatch ();
            Long endtime = System.currenttimemillis ();
            System.out.println ("Time:" + (Endtime-begintime));
        JDBC_TOOLS.COMMIT (conn);
            catch (Exception e) {e.printstacktrace ();
        Jdbc_tools.rollback (conn);
        }finally{Jdbc_tools.relasesource (conn, PS); }
    }
}
JDBC Batch update
public void Insertintransaction (string[] sqls) throws sqlexception{
          try (Connection conn = Drivermanager.getconnection (URL, user, pass)) {
              //Turn off autocommit, that is, open transaction
              Conn.setautocommit (false);
              Try (Statement stmt = Conn.createstatement ()) {for
                  (String sql:sqls) {
                      stmt.addbatch (SQL);
                  }
                  Stmt.executebatch ();
                 Commit TRANSACTION
                 conn.commit ();
             } catch (SQLException e) {
                 conn.rollback ();
                 System.out.println ("Exception:");
                 System.out.println (E.getmessage ());
             } finally {
                 conn.setautocommit (true);}} 
     

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.