zoukankan      html  css  js  c++  java
  • JdbcTemplate 批量

    JDBC 批量处理

    public void insertBatch(final List<Customer> customers){
            
      String sql = "INSERT INTO CUSTOMER (CUST_ID, NAME, AGE) VALUES (?, ?, ?)";
                
      getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                
        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            Customer customer = customers.get(i);
            ps.setLong(1, customer.getCustId());
            ps.setString(2, customer.getName());
            ps.setInt(3, customer.getAge() );
        }
                
        @Override
        public int getBatchSize() {
            return customers.size();
        }
      });
    }

    注意:url    jdbc:mysql://localhost:3306/yiibaijava  注意驱动url 的编写

    实例

    List<Object[]> batchargs = new ArrayList<>();
    String sql = "INSERT INTO tableName (commodityId, commodityName, commodityURL, commodityPrice, commodityPromo, commodityCommentCount, cityCode, shopType, shopTrade, status, shopId, shopName, updateTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"; Object[] das = {commodityId, commodityName, commodityURL, commodityPrice, commodityPromo, commodityCommentCount, cityCode, shopType, shopTrade, status, shopId, shopName, updateTime}; batchargs.add(das); if (batchargs.size() % 1000 == 0) { jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement ps, int i) throws SQLException { for (int j = 0; j < 13; j++) { ps.setString(j + 1, batchargs.get(i)[j] == null ? "" : batchargs.get(i)[j].toString()); } } @Override public int getBatchSize() { return batchargs.size(); } }); batchargs.clear(); LOG.info("#######################-入库 :" + totalCount + "-#########################"); }
    故乡明
  • 相关阅读:
    [ubuntu] aptget 详解
    zoj 1048 求平均数 python
    zoj 1001 A+B Python版本
    HustOj 数据库分析(r1292) [—夏 夏 ]
    poj 3020 Antenna Placement 夜
    poj 3349 Snowfalke Snow Snowflakes 夜
    poj 1062 昂贵的聘礼 夜
    poj 3368 Frequent values 夜
    poj 2524 Ubiquitous Religions 夜
    poj 1273 Drainage Ditches 夜
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/14954117.html
Copyright © 2011-2022 走看看