gpt4 book ai didi

Java mysql 准备好的语句更新不起作用

转载 作者:行者123 更新时间:2023-12-02 04:41:41 25 4
gpt4 key购买 nike

这是我的代码:

ResultSet rs = statement.executeQuery("select * from page");
PreparedStatement updatepage = mySqlCon.prepareStatement("update enwiki.page set enwiki.page.Text = ? where page_id = ?");
int count = 0;
while (rs.next())
{
int id = rs.getInt("id");
String text = rs.getString("text");
if(text == null)
text = "";
updatepage.setString(1, text);
updatepage.setInt(2, id);


if(count++ > 10000)
{
updatepage.executeUpdate();
updatepage.clearBatch();
count = 0;
}
}

updatepage.executeUpdate();

问题出在运行 updatepage.executeUpdate() 行之后,我使用工作台检查数据库,但在该表上没有看到任何更改。

最佳答案

您当前的代码仅在 count 的值大于 10000 时执行更新,并且执行单个更新。似乎您想要/需要使用批处理,因此您必须在每次迭代时将语句添加到批处理中(您没有这样做),并在 if 内执行批处理中的所有语句>(你也没有做的事情)。

代码如下:

while (rs.next()) {
int id = rs.getInt("id");
String text = rs.getString("text");
if(text == null)
text = "";
updatepage.setString(1, text);
updatepage.setInt(2, id);

//add the current statement to the batch
updatepage.addBatch();
if(count++ > 10000) {
//line below will only execute the current statement (useless)
//updatepage.executeUpdate();
//line below will clear the batch statements (useless too)
//updatepage.clearBatch();
updatepage.executeBatch();
count = 0;
}
}
//updatepage.executeUpdate();
updatepage.executeBatch();

关于Java mysql 准备好的语句更新不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30110863/

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