rt:
插入数据库的操作时调用的存储过程。不过可以改。现在只能传参数执行一次插一次。数据量大概不到10万。for循环10万次岂不是太土了,其中某此出错咋办。rollback?10万次出错的概率应该很大。那我岂不是每次都不能成功插入。不知道问题说清楚了没。求教了。

解决方案 »

  1.   

    EXCEL么?如果是EXCEL、TXT文件的话,可以直接导入到数据库中的,建议导入到一张新表里,然后和目标表做对比,插入楼主需要的记录。
      

  2.   

    怎么导?bcp吗?
    我需求没说清楚。这样的表有6张。字段都不一样。搞新表的话。内存岂不是浪费很多。
    6张目标表都是有数据的。
      

  3.   

    我明白你的意思。可是循环10万次。我怕在其中某此出错。那我就要rollback了
      

  4.   

    6张表,直接将数据一次性导入目标表不就行了?反正只是单纯的导入,又不需要进行任何规则的判断。
    或者存储过程里直接使用insert into oldtable select * from new newtable不行吗?
    就算你有条件约束,那就直接在后面跟where子句好了。
    一条一条导入,导到何年何月去了
      

  5.   

    这个不做导入,估计你只能在程序里处理了。
    <!- Hibernate 批量插入 -->
    public void batchAddSendCus(final List<ShpSendVouchDto> shpSendVouchs) throws Exception {
            super.getHibernateTemplate().execute(new HibernateCallback(){
                public Object doInHibernate(Session session){
                    int rows = 0;
                      for(ShpSendVouchDto dto: shpSendVouchs)
                      {
                           session.save(dto);
                           rows++;
                           if ( rows  % 10000 == 0 ) {                  
                            //flush a batch of inserts and release memory:
                             //将本批插入的对象立即写入数据库并释放内存
                           session.flush();
                           session.clear();
                           }
                      }
               return new Integer(rows);
                }
              });
       }
    ShpSendVouchDto为要插入的对向
    List<ShpSendVouchDto> shpSendVouchs插入的对向的集合
    <!-JDBC批量插入-->
    public void batchAddSendCus(final List<ShpSendVouchDto> shpSendVouchs) throws Exception {
       if (shpSendVouchs != null) {
    (数据库针对MySql)
                 String sql = " insert into SHPSENDVOUCH(vouchid,vouchmodelid,Cusid,shopid) values(?,?,?,?) ";
    (数据库针对Oracle,SEQ_SHP_SENDVOUCH.NEXTVAL为ORACLE中的序列)
        //      String sql = " insert into shpsendvouch(id,vouchid,vouchmodelid,Cusid,shopid)values(SEQ_SHP_SENDVOUCH.NEXTVAL,?,?,?,?) ";
                this.getJdbcTemplate().batchUpdate(sql, new BatchPreparedStatementSetter() {
                    public void setValues(PreparedStatement ps, int i) throws SQLException {
                        String vouchid = shpSendVouchs.get(i).getVouchid();
                        Long vouchmodelid = shpSendVouchs.get(i).getVouchmodelid();
                        Long cusid = shpSendVouchs.get(i).getCusid();
                        Long shopid = shpSendVouchs.get(i).getShopid();
                        Long difstatus = shpSendVouchs.get(i).getDifstatus();
                        String vouchpassword = shpSendVouchs.get(i).getVouchpassword();
                        Date gettime = shpSendVouchs.get(i).getGettime();
                        Long status = shpSendVouchs.get(i).getStatus();
                        ps.setString(1, vouchid);
                        ps.setLong(2, vouchmodelid);
                        ps.setLong(3, cusid);
                        ps.setLong(4, shopid);
                    }
                    public int getBatchSize() {
                        return shpSendVouchs.size();
                    }
                });
            }

    ShpSendVouchDto为要插入的对向
    List<ShpSendVouchDto> shpSendVouchs插入的对向的集合