Java mysql准备语句更新不工作
这是我的代码:Java mysql准备语句更新不工作,java,jdbc,prepared-statement,Java,Jdbc,Prepared Statement,这是我的代码: 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
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()
运行后,我使用workbench检查数据库,但没有看到该表上有任何更改。当前代码仅在count
的值大于10000时执行更新,并且它执行单个更新。似乎您希望/需要使用批处理,因此您必须在每次迭代时将语句添加到批中(您没有执行的操作),并在中执行批中的所有语句(如果(您也没有执行的操作)
代码如下所示:
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();
你能给我完整的代码吗?根据您提供的代码updatepage.addBatch();/updatepage.executeBatch();他失踪了。还要检查dbConnection.setAutoCommit(false) @Andi您的连接是否设置为自动提交模式?如果没有,则在执行所有操作后,您必须添加连接#提交
。是的,我尝试了两种方法,自动提交和正常提交,都不起作用。哦,我认为它起作用了,需要时间。我还以为日食坠毁了呢!我建议每隔几次打印一条消息(例如,成批设置的每1000条语句),以检查成批语句的进度(=