我有以下数据库:
CREATE TABLE b (
name VARCHAR(255) NOT NULL,
text MEDIUMTEXT NOT NULL,
PRIMARY KEY (NAME)
);
我有以下代码:
public void saveBlocks(Map<String, Block> blocks) throws SQLException{
String userInsertQuery = "Insert INTO b (name, text) VALUES (?, ?)";
preparedStatement = connect.prepareStatement(userInsertQuery);
int batchCounter = 0;
for(Block block : blocks.values()){
preparedStatement.setString(1,block.getId());
preparedStatement.setString(2,block.getText());
preparedStatement.addBatch();
batchCounter++;
if(batchCounter%200 == 0) {
preparedStatement.executeBatch();
}
}
preparedStatement.executeBatch();
}
但它不会将任何内容写入数据库。 我对另一个表有几乎相同的代码,并且它可以正常工作。唯一的区别是我执行的插入数量(在工作情况下:超过 100 万,在非工作情况下:大约 70)
我找不到解决方案,也没有想法。
我尝试过的: - connect.commit() => 因为我有自动提交 true 没有变化。 (还是我用错了?) - 我的 key 没有自动增量功能,所以没问题。
你有什么想法吗?
最佳答案
您最终必须提交
交易:
connect.commit();
与connect.setAutoCommit(false);
String userInsertQuery = "Insert INTO b (name, text) VALUES (?, ?)";
preparedStatement = connect.prepareStatement(userInsertQuery);
connect.setAutoCommit(false);
int batchCounter = 0;
for(Block block : blocks.values()){
preparedStatement.setString(1,block.getId());
preparedStatement.setString(2,block.getText());
preparedStatement.addBatch();
batchCounter++;
}
int[] results = preparedStatement.executeBatch();
connect.commit();
关于java - ReadyStatement 不将数据插入 MySQL DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53433415/