zoukankan      html  css  js  c++  java
  • Java JDBC Batch

    Java批量处理数据

    import java.sql.Connection;
    import java.sql.PreparedStatement;
    
    //import
    
    String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
    Connection connection = new getConnection();
    PreparedStatement ps = connection.prepareStatement(sql);
    
    for (Employee employee: employees) {
    
        ps.setString(1, employee.getName());
        ps.setString(2, employee.getCity());
        ps.setString(3, employee.getPhone());
        ps.addBatch();
    }
    ps.executeBatch();
    ps.close();
    connection.close();

    Java进行批处理过程中,如果一次处理数据过多就会出现内存溢出错误。解决方法就是为批处理设置BatchSize。

    String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
    Connection connection = new getConnection();
    PreparedStatement ps = connection.prepareStatement(sql);
     
    final int batchSize = 1000;
    int count = 0;
     
    for (Employee employee: employees) {
     
        ps.setString(1, employee.getName());
        ps.setString(2, employee.getCity());
        ps.setString(3, employee.getPhone());
        ps.addBatch();
         
        if(++count % batchSize == 0) {
            ps.executeBatch();
        }
    }
    // insert remaining records
    ps.executeBatch();
    ps.close();
    connection.close();

     Spring Framework也有针对数据库Batch操作,摘抄Spring Framework文档片段

    //Spring FrameWork JdbcTemplate
    public class JdbcActorDao implements ActorDao {
        private JdbcTemplate jdbcTemplate;
          public void setDataSource(DataSource dataSource) {
            this.jdbcTemplate = new JdbcTemplate(dataSource);
          }
    
          //这里返回整数数组,在分批次执行一系列sql,每次都返回一次执行数据库操作影响到的数据条数.如果batch操作失败JDBC驱动则返回-2
          public int[] batchUpdate(final List<Actor> actors) {
              int[] updateCounts = jdbcTemplate.batchUpdate(
                      "update t_actor set first_name = ?, last_name = ? where id = ?",
                      new BatchPreparedStatementSetter() {
                          public void setValues(PreparedStatement ps, int i) throws SQLException {
                              ps.setString(1, actors.get(i).getFirstName());
                              ps.setString(2, actors.get(i).getLastName());
                              //longValue()
                              ps.setLong(3, actors.get(i).getId().longValue());
                          }
                          // 这里设置批处理池容量
                          public int getBatchSize() {
                              return actors.size();
                          }
                      } );
              return updateCounts;
          }
    
          //  ... additional methods
    }

    参考文章 http://viralpatel.net/blogs/batch-insert-in-java-jdbc/

  • 相关阅读:
    org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON
    小程序用户表wx_user设计
    CSDN支持语法高亮的常用语言(Markdown使用之)
    查看CentOS版本信息
    Java操作MongoDB之mongodb-driver
    使用SpringCache进行缓存数据库查询
    MYSQL:WARN: Establishing SSL connection without server's identity verification is not recommended.
    SpringDataRedis常用方法
    SpringBoot整合Redis进行缓存数据库查询
    java连接neo4j
  • 原文地址:https://www.cnblogs.com/title/p/3528976.html
Copyright © 2011-2022 走看看