`

JdbcTemplate批量处理数据方法batchUpdate

 
阅读更多
final int count = 2000;    
    final List<String> firstNames = new ArrayList<String>(count);    
    final List<String> lastNames = new ArrayList<String>(count);    
    for (int i = 0; i < count; i++) {    
      firstNames.add("First Name " + i);    
      lastNames.add("Last Name " + i);    
    }    
    jdbcTemplate.batchUpdate(    
            "insert into customer (id, first_name, last_name, last_login, comments) values (?, ?, ?, ?, ?)",    
            new BatchPreparedStatementSetter() {    
           //为prepared statement设置参数。这个方法将在整个过程中被调用的次数    
        public void setValues(PreparedStatement ps, int i) throws SQLException {    
                ps.setLong(1, i + 10);    
                ps.setString(2, firstNames.get(i));    
                ps.setString(3, lastNames.get(i));    
                ps.setNull(4, Types.TIMESTAMP);    
                ps.setNull(5, Types.CLOB);    
              }    
              //返回更新的结果集条数    
          public int getBatchSize() {    
                   return count;    
              }    
            });    
  }  

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics