java - Java - 将记录插入数据库时执行批处理不起作用
问题描述
从文件中提取单词并将它们存储在哈希集中后,我试图在 MYSQL 中的表中插入记录。
在获得 500 条记录后,我尝试使用executeBatch()插入我的数据库,但是当执行完成时,我检查了我的表,根本没有插入任何记录。
注意:当我使用ExecuteUpdate()时,记录将显示在我的表中。但不是 ExecuteBatch() 因为我想批量插入,而不是一个一个地插入。我可以知道我做错了什么吗?
代码:
public void readDataBase(String path,String word) throws Exception {
try {
// Result set get the result of the SQL query
int i=0;
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
+ "user=root&password=root");
// Statements allow to issue SQL queries to the database
// statement = connect.createStatement();
System.out.print("Connected");
// Result set get the result of the SQL query
preparedStatement = connect
.prepareStatement("insert IGNORE into fulltext_ltat.indextable values (default,?, ?) ");
preparedStatement.setString( 1, path);
preparedStatement.setString(2, word);
preparedStatement.addBatch();
i++;
// preparedStatement.executeUpdate();
if(i%500==0){
preparedStatement.executeBatch();
}
preparedStatement.close();
// writeResultSet(resultSet);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
这是我调用该方法的循环(单词只是一个包含要插入到表中的单词的数组):
for(int i = 1 ; i <= words.length - 1 ; i++ ) {
connection.readDataBase(path, words[i].toString());
}
我的主要方法:
public static void main(String[] args) throws Exception {
StopWatch stopwatch = new StopWatch();
stopwatch.start();
File folder = new File("D:\\PDF1");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles) {
if (file.isFile()) {
HashSet<String> uniqueWords = new HashSet<>();
String path = "D:\\PDF1\\" + file.getName();
try (PDDocument document = PDDocument.load(new File(path))) {
if (!document.isEncrypted()) {
PDFTextStripper tStripper = new PDFTextStripper();
String pdfFileInText = tStripper.getText(document);
String lines[] = pdfFileInText.split("\\r?\\n");
for (String line : lines) {
String[] words = line.split(" ");
for (String word : words) {
uniqueWords.add(word)
;
}
}
// System.out.println(uniqueWords);
}
} catch (IOException e) {
System.err.println("Exception while trying to read pdf document - " + e);
}
Object[] words = uniqueWords.toArray();
MysqlAccessIndex connection = new MysqlAccessIndex();
for(int i = 1 ; i <= words.length - 1 ; i++ ) {
connection.readDataBase(path, words[i].toString());
}
System.out.println("Completed");
}
}
解决方案
您进行批量更新的模式已关闭。您应该只打开一次连接并准备语句。然后,迭代多次,绑定参数,并将该语句添加到批处理中。
// define a collection of paths and words somewhere
List<String> paths = new ArrayList<>();
List<String> words = new ArrayList<>();
try {
// presumably you only want to insert so many records
int LIMIT = 10000;
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager
.getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
+ "user=root&password=root");
String sql = "INSERT IGNORE INTO fulltext_ltat.indextable VALUES (default, ?, ?);";
preparedStatement = connect.prepareStatement(sql);
for (int i=0; i < LIMIT; ++i) {
preparedStatement.setString(1, paths.get(i));
preparedStatement.setString(2, word.get(i));
preparedStatement.addBatch();
if (i % 500 == 0) {
preparedStatement.executeBatch();
}
}
// execute remaining batches
preparedStatement.executeBatch();
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
preparedStatement.close();
connect.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
我在此处所做的一项关键更改是为何时应该停止插入添加逻辑。目前,您的代码看起来有一个无限循环,这意味着它将永远运行。这可能不是你打算做的。
推荐阅读
- python - 使用 python pandas 解析 CSV 列包含字符串和 JSON 的混合值
- json - 如何将json文件解析为对象类中的数组?
- django - Django:按多级反向查找排序
- reactjs - 如何按需导入 Ant 设计以使用进行解构的组件?
- jquery-ui - 在非空字段上设置 jQuery UI 日期选择器上的 dateFormat
- python - 为 sklearn 管道实现自定义 one-hot-encoding 功能
- javascript - 直到并且除非在摩纳哥编辑器中点击最后一行,否则不会触发任何事件
- reactjs - 返回功能组件内的组件
- c++ - 将类对象推送到 std::vector 时使用已删除函数?
- slam - 如何获得罗德里格斯向量的导数并在非线性最小二乘中进行更新?