各位大虾,最近小弟在做程序时碰到一个关于JDBC rs.updateRow()批量更新时的内存溢出问题,每当更新数据到30万左右时就会出现内存溢出问题,在网上查相关信息,但是问题还是没能得到解决,还望大虾们帮忙解决一下。在此小弟先谢各位了。代码如下:public void contrast(String strid){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = JdbcUtil.getConnection();
conn.setAutoCommit(false);

 String sql = "select  ver  from   test_tb_reference  " +"  where statistics_id='"+strid+"'";
System.out.println("--"+sql);  
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
int i=0;  
while(rs.next()){
i++;

rs.updateString(1,"一版");
rs.updateRow();

if(i%500==0){
conn.commit();
System.gc();

}
System.out.println("---"+i);
}
conn.commit();
System.out.println("commit执行完毕!");

}catch(Exception e){

e.printStackTrace();
}finally{
try{
if(rs!=null){
rs.close();
}
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
System.out.println("连接关闭!");
}

}catch(Exception e2){
e2.printStackTrace();
}

}
}

解决方案 »

  1.   

    update table set 字段=? where id=?这样更新不是好点吗?为什么要搜索出来再更新呢?
      

  2.   

    update table set 字段=? where id=? 这个我知道,不过我想说的是如果由于某个问题我们用到上了述所说的那种更新办法,rs.updateRow()批量更新时的内存溢出问题,有没有什么办法能够解决它。呵呵。
      

  3.   

    还有就是当表中的数据量比较大时例1000万左右,用update table set 字段=? where id=? 好像效果不是很好。
      

  4.   

    不清楚你用的数据库是哪一种,我个人认为,这种大数据量的操作,最好用数据库的store procedure
      

  5.   

    哦,我用的是ORACLE数据库,还望多多指点.
      

  6.   

    恩, 大量数据的能给数据库自己搞定就应该给它.
    还有conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 有必要给那么多选项吗, 这个将使创建的语句的的性能下降.