Announcement Announcement Module
Collapse
No announcement yet.
Batch Insert/update using SPRING JDBC? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch Insert/update using SPRING JDBC?

    Hi,
    I am new to Spring JDBC, I was going through the tutorial from Spring website on implementing spring JDBC withoug using SQL (http://static.springsource.org/sprin...ence/jdbc.html)
    However I could not find any tutorial related to performing a batch insert or update on a table(Similar to addBatch function on Java PreparedStatement).
    Please can you let me know if this is possible using Spring JDBC?

    Thanks in Advance
    PS

  • #2
    Here's the code I use in production:

    Code:
      private void executeBatchStatement(final List<Object[]> paramsList)
      {
        final BatchPreparedStatementSetter psSetter = new BatchPreparedStatementSetter()
        {
          @Override
          public void setValues(PreparedStatement ps, int i) throws SQLException
          {
            ps.setString(1, paramsList.get(i)[0];
            // ...
          }
    
          @Override
          public int getBatchSize()
          {
            return paramsList.size();
          }
        };
    
        this.jdbc.execute("INSERT INTO ...", new PreparedStatementCallback<int[]>()
        {
          @Override
          public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException
          {
            for (int i = 0; i < psSetter.getBatchSize(); i++) {
              psSetter.setValues(ps, i);
              ps.addBatch();
            }
            return ps.executeBatch();
          }
        });
      }
    It's copied from the class which has much more methods, but the above code just uses plain JDBC's ps.executeBatch().

    You could also use:

    Code:
        this.jdbc.batchUpdate("INSERT INTO ...", paramsList);
    (this.jdbc is just an instance of JdbcTemplate).

    regards
    Grzegorz Grzybek

    Comment

    Working...
    X