Spring JdbcTemplate Bulk Update

Source: Internet
Author: User
Tags getdate

Batch update for spring JdbcTemplate:

1, JdbcTemplate batchUpdate (New string[]{});

Execute multiple SQL statements at a time;

2, Java code   final list tmplist = ....;    Int count = jdbctemplate.batchupdate (sql, new  BatchPreparedStatementSetter () {                public int getbatchsize ()  {                    return tmplist.size ();                }                 public void setvalues (PreparedStatement  ps, int i) throws sqlexception {                    ps.setstring (1, tmplist .get (i). GetName ( ));                     Ps.sEtstring (2,tmplist .get (i). GetCode ());                     Ps.setstring (3,tmplist .get (i). GetDate ());                     Ps.setint (4,tmplist .get (i). GetRank ());               &nbsp}}) .length;  

Final List tmplist = ...;
int count = jdbctemplate.batchupdate (sql, new BatchPreparedStatementSetter () {public
			int getbatchsize () {
				return Tmplist.size ();
			}
			public void Setvalues (preparedstatement ps, int i) throws SQLException {
				ps.setstring (1, tmplist. Get (i) getName ());
				ps.setstring (2,tmplist. Get (i). GetCode ());
				Ps.setstring (3,tmplist. Get (i). GetDate ());
				Ps.setint (4,tmplist. Get (i). GetRank ());
			Length

This method can be batch, but in spring it is executed in one piece, and a large amount of data execution is inefficient

3, write preparedstatement for batch Java code final list<tmpbean> tmpbeanlist=...;    final string sql = ....;    int[] count =  (int[])  this.pushjdbctemplate.execute (sql, new  PreparedStatementCallback () {                public object doinpreparedstatement (PREPAREDSTATEMENT&NBSP;PS) throws SQLException,  dataaccessexception {                    int length = stockranlist.size ();                     Ps.getconnection (). Setautocommit (false);                     for (int i=0;i<length;i++) {          &Nbsp;            ps.setstring (1,  Tmpbeanlist.get (i). GetName ());                         ps.setstring (2,tmpbeanlist.get (i). GetCode ());                         ps.setstring (3,tmpbeanlist.get (i). GetDate ());                         ps.setint (4,tmpbeanlist.get (i). GetRank ());                         ps.addbatch ();                    }                     object o = ps.executebatch ();                     Ps.getconnection (). commit ();                     Ps.getconnection (). Setautocommit (True);                                  //If you use <aop:config>    to control the transaction, you need to note the previous line, or you will be the error                     return  o;               &nbsp}});  

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.