Java BULK Insert

Source: Internet
Author: User
Tags bulk insert

    1. JdbcTemplate

Public int insertcontractach (list list)  throws DataAccessException {       final List temList = list;       string sql =  "insert into contract_ach_t "  +                " values (?, To_date (?, ' Yyyy-mm-dd '),?,?)   ";      try{           Int[] ii = this.getjdbctemplate (). BatchUpdate (Sql, new mybatchpreparedstatementsetter (temlist));           return ii.length;       }catch  (org.springframework.dao.dataaccessexception e)  {           e.printstacktrace ();           throw new D ataaccessexception (E.getmessage ());      }  }     private class mybatchpreparedstatementsetter implements batchpreparedstatementsetter {      final list temlist;             public mybatchpreparedstatementsetter (list list) {           temList = list;      }       public int getbatchsize ()  {           return temlist.size ();      }         public void setvalues (Preparedstatement ps, int i)               throws sqlexception  {          contractachvo contractachvo =  (ContractAchVO) temList.get (i);           ps.setstring (1, contractachvo.getcontractcode ());           ps.setstring (2,  Contractachvo.getcreatedate ());           ps.setstring (3,  contractachvo.getemployeeid ());           ps.setstring ( 4, contractachvo.getpercent ());       }  }

Ideas for 2.JPA data,hibernate

 public  void batchsave (String sql,int batchsize,list<object[]> list)  throws SQLException{   if  (Collectionutils.isempty (list)) {    Return;   }     sessionimplementor session =em.unwrap ( Sessionimplementor.class);      connection connection =    session.connection ();    preparedstatement st = connection.preparestatement (SQL);    for (Int i=0;i<list.size (); i++) {    object[] params  = list.get (i);   if  (Arrayutils.isempty (params)) {   continue;    }      for  (int j=1;j<=params.length;j++) {    if (params[j-1] instanceof string)    st.setstring (J,String.valueOf (params[ J-1]);    else if (params[j-1] instanceof date)    st.setdate (J, new java.sql.date (   (Date)  params[j-1]);    else if (params[j-1] instanceof  BigDecimal)    st.setbigdecimal (j,  (BigDecimal)  params[j-1]);    else  if (params[j-1] ==null)    st.setnull (J, java.sql.types.varchar);    else    st.setstring (j, string.valueof (params[j-1));   }    st.addbatch ();    if (i > 0 && i%batchsize==0) {    st.executebatch ();    st.clearbatch ();   }   }   st.executebatch ();    }


Java BULK Insert

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.