"JAVA" Oracle BULK INSERT data operation

Source: Internet
Author: User

Recently done projects need to insert large amounts of data into the database, before having the impression of Preparedstatement.executebatch (), so that you can reduce the interaction with the database; today, online search for how to achieve, Finding out more than just using Addbatch () and ExecuteBatch (), you also need to Conn.setautocommit (false) to turn off automatic transaction submission, or to reduce interaction. Also note that Addbatch () to a certain number of times is to be submitted in time, otherwise prone to memory overflow problem.

try {string url = "Jdbc:oracle:thin: @IP: 1521:ORCL";//ORCL is sid string user = "Oracle" for the database;
   String Password = "Oracle";
   StringBuffer sql = new StringBuffer ();
   Sql.append ("INSERT into Ex_log (ex_log_id,ex_log_date) VALUES (?,?)");
   Class.forName ("Oracle.jdbc.driver.OracleDriver");
   Connection con = (Connection) drivermanager.getconnection (Url,user,password);
   Close Transaction autocommit Con.setautocommit (false);
   Final int batchsize = 10000;
   int count = 0;
   Long starttime = System.currenttimemillis ();
   PreparedStatement PST = (preparedstatement) con.preparestatement (sql.tostring ());
    for (int i = 0; i < list.size (); i++) {Exlog Exlog = (exlog) list.get (i);
    Pst.setstring (1, Exlog.getexlogid ());
    Pst.setstring (2, Exlog.getexlogdate ());
    Add an SQL command to the command list Pst.addbatch ();
        if (++count% batchsize = = 0) {pst.executebatch ();
    Count = 0;
   }///Perform batch update pst.executebatch ();
   Execution of the statement, submit the transaction con.commit (); Long Endtime= System.currenttimemillis ();
   System.out.println ("spents:" + (Endtime-starttime));
   Pst.close ();
  Con.close ();
  catch (ClassNotFoundException e) {e.printstacktrace ();
  catch (SQLException e) {e.printstacktrace (); }

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.