This is my code:
这是我的代码:
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();
The problem is after the line : updatepage.executeUpdate()
is run, I check the database using workbench and I don't see any changes on that table.
问题是在行:updatepage.executeUpdate()运行后,我使用工作台检查数据库,我没有看到该表上的任何更改。
2 个解决方案
#1
Your current code is only executing the update when the value of count
is greater than 10000, and it executes a single update. Seems like you want/need to use a batch processing, so you have to add the statements into the batch on every iteration (something you're not doing) and execute all the statements in the batch inside your if
(something you're not doing either).
您的当前代码仅在count的值大于10000时执行更新,并且它执行单个更新。好像你想要/需要使用批处理,所以你必须在每次迭代时将这些语句添加到批处理中(你没有做的事情)并在你的if中执行批处理中的所有语句(你不是做任何一个)。
The code will be like this:
代码将是这样的:
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();
#2
Can you give paste the whole code ? From the code what you have provided updatepage.addBatch();/ updatepage.executeBatch(); is missing. Also check if dbConnection.setAutoCommit(false);.
你能粘贴整个代码吗?从你提供的代码updatepage.addBatch(); / updatepage.executeBatch();不见了。另请检查dbConnection.setAutoCommit(false);.
#1
Your current code is only executing the update when the value of count
is greater than 10000, and it executes a single update. Seems like you want/need to use a batch processing, so you have to add the statements into the batch on every iteration (something you're not doing) and execute all the statements in the batch inside your if
(something you're not doing either).
您的当前代码仅在count的值大于10000时执行更新,并且它执行单个更新。好像你想要/需要使用批处理,所以你必须在每次迭代时将这些语句添加到批处理中(你没有做的事情)并在你的if中执行批处理中的所有语句(你不是做任何一个)。
The code will be like this:
代码将是这样的:
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();
#2
Can you give paste the whole code ? From the code what you have provided updatepage.addBatch();/ updatepage.executeBatch(); is missing. Also check if dbConnection.setAutoCommit(false);.
你能粘贴整个代码吗?从你提供的代码updatepage.addBatch(); / updatepage.executeBatch();不见了。另请检查dbConnection.setAutoCommit(false);.