我想弄清楚使用PreparedStatement executeBatch()方法的最佳方式。使用PreparedStatement的最佳方法executeBatch()
一种方法我试过是:
try{
prepStmt1 = conn.prepareStatement("update table set done='yes' where phone=?");
while (operatorsQuery.next()) {
logger.info("phone: "+ phone + ", operator: " + operator);
process(); //0.5-1 second long
prepStmt1.setString(1, "0"+phone);
prepStmt1.addBatch();
}
prepStmt1.executeBatch();
}
catch{...}
finally{
closeStatmentand(prepStmt1);
}
我对这段代码遇到的问题是,该方案可以在中间退出,那么它可能达不到则ExecuteBatch()方法。
第二种方式我想:
try{
prepStmt1 = conn.prepareStatement("update table set done='yes' where phone=?");
while (operatorsQuery.next()) {
logger.info("phone: "+ phone + ", operator: " + operator);
process(); //0.5-1 second long
prepStmt1.setString(1, "0"+phone);
prepStmt1.addBatch();
if ((j + 1) % 100 == 0) {
prepStmt1.executeBatch();
}
}
prepStmt1.executeBatch();
}
catch{...}
finally{
closeStatmentand(prepStmt1);
}
这是做到这一点的最优选的方法是什么?
发帖时的例子确保编译和运行 –
你是问你是否应该打破批成已知大小 – DaveH
这似乎很奇怪,该程序可以(在第二种情况下100?)在执行过程中关闭。如果程序结束是基于用户交互的,请告诉用户他尚未完成,并通知他后果。这样用户可以决定他是否想要丢失数据或想要等待。 –