`

批量处理sql

阅读更多
//获得要升级的所有sql文件
String sqls = "";
String[] args =new String[0];
DB dba = new DB();
if(sqls!=null && !"".equals(sqls)){
args = sqls.split(";");
}
//进行数据库操作
PreparedStatement pstm = null;          Connection conn=dba.getConnection();          //进行批量操作
int count = 0;
int i = 0;
for(int j=0;j<args.length;j++){
pstm =conn.prepareStatement(args[j]);
pstm.addBatch();
i++;
count++;
if(i >= 20) { // 避免最后数据量过大,每次执行一小批
pstm.executeBatch();
System.out.println("已经插入"+ count +"条");
i = 0;
}
}

if(i != 0) {
pstm.executeBatch();
System.out.println("已经插入"+ count +"条");
}

conn.close();

} catch (Exception e) {
e.printStackTrace();
flag = false;
return flag;
}finally{
if(dba!=null)dba.close();
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics