Spring jdbctemplate Batch Update data

Source: Internet
Author: User

method: First realize Batchpreparedstatementse tter interface, and then call the JdbcTemplate batchUpdate (Sql,setter) operation, the parameter SQL is a precompiled statement, and the setter is an instance of BatchPreparedStatementSetter. Or use the internal anonymous class method.

BatchPreparedStatementSetter interface

 Public Interface batchpreparedstatementsetter{  public int  getbatchsize ();//Returns the size    of the batch  Public void setvalues (preparedstatement PS,int  i);
//
PS: We are going to set the value of the PreparedStatement
I: In this batch rollup, the index of the operation being performed, starting from 0
}

Implementation (Internal anonymous Class):

Jdbctemplate.batchupdate (SQL,NewBatchPreparedStatementSetter () { Public intgetbatchsize () {//TODO auto-generated Method Stub                    returnresourcelist.size (); Read the size of the data source list} Public voidSetvalues (PreparedStatement PS,intIthrowsSQLException {//TODO auto-generated Method StubResourcebean Resourcebean =(Resourcebean) resourcelist.get (i);//Read the list of source data to be inserted pstmt.setstring (1, Resourcebean.getid ()); Pstmt.setstring (2, Resourcebean.getname ()); Pstmt.setstring (3, Resourcebean.getauthor ()); ... ...                }                            });

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.