gpt4 book ai didi

java - PreparedStatement batch Execute 只执行最后一批

转载 作者:行者123 更新时间:2023-11-30 09:20:53 25 4
gpt4 key购买 nike

我在使用准备好的语句批处理执行器时遇到了问题:

try{
while (operatorsQuery.next()) {
phone = Integer.toString(operatorsQuery.getInt(1));
prepStmt1 = connBlng.prepareStatement("update table set done='yes' where phone=?");

prepStmt1.setString(1, phone);
prepStmt1.addBatch();
}
prepStmt1.executeBatch();
} catch(Exception e){
e.printStackTrace();
} finally{
closeStatmentandRS(operatorsQuery, prepStmt1);
}

出于某种原因,它只更新最后一批(最后一部手机)。

为什么会这样?

最佳答案

prepStmt1 = connBlng.prepareStatement("update table set done='yes' where phone=?");
prepStmt1.setString(1, phone);
prepStmt1.addBatch();

您正在使之前对 prepStmt1 的引用无效,因此 batch 将永远只有您尝试处理的last 元素。

你要的是这个:

prepStmt1 = connBlng.prepareStatement("update table set done='yes' where phone=?");  
while(...)
{
prepStmt1.setString(1, phone);
prepStmt1.addBatch();
}

修复方法是只分配参数化 SQL 语句一次,并在每次传递时翻转参数。类似于您编写函数并通过参数列表更改输入的方式。

关于java - PreparedStatement batch Execute 只执行最后一批,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17189947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com