我试图从文件中提取单词并将它们存储在哈希集中后,将记录插入MYSQL表中。
我尝试使用 executeBatch()插入500条记录后插入数据库,但是执行完成后,我检查了表,根本没有插入记录。
注意:当我使用 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();
}
}
这是我调用该方法的循环(words只是一个包含要插入表中的单词的数组):
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");
}
}
答案 0 :(得分:3)
您的批处理更新模式已关闭。您应该只打开连接并仅准备一次语句。然后,重复多次,绑定参数,然后将该语句添加到批处理中。
<form role="form" action="{{ route('kecamatan.store')}}" method="POST">
@csrf
<div class="box-body">
<div class="form-group">
<label for="input">Nama Kecamatan</label>
<input type="text" class="form-control {{ $errors->has('namaKecamatan') ? 'is-invalid':''}}"
name="namaKecamatan" id="namaKecamatan" required>
</div>
<div class="form-group">
<label for="input">Nama Camat</label>
<input id="namaCamat" type="text" class="form-control {{ $errors->has('namaCamat') ? 'is-invalid':''}}"
name="namaCamat">
</div>
<div class="form-group">
<label for="input">Alamat Kantor</label>
<textarea id="alamatKantor" class="form-control {{ $errors->has('alamatKantor') ? 'is-invalid':''}}"
rows="3"
placeholder="Alamat Kantor Camat" name="alamatKantor"></textarea>
</div>
<div class="form-group">
<label for="input">No.Tlp/Hp</label>
<input id="teleponKantor" type="text" class="form-control {{ $errors->has('teleponKantor') ? 'is-invalid':''}}"
name="teleponKantor">
</div>
<div class="form-group">
<label for="input">Keterangan</label>
<textarea id="keteranganKecamatan" class="form-control {{ $errors->has('keteranganKecamatan') ? 'is-invalid':''}}"
rows="3" placeholder="Keterangan tambahan yang dibutuhkan" name="keteranganKecamatan"></textarea>
</div>
</div>
<div class="box-footer">
<button class="btn btn-primary">Tambah</button>
</div>
@slot('footer')
</form>
我在这里所做的一个关键更改是为何时应该停止执行插入操作添加逻辑。当前,您的代码看起来有一个无限循环,这意味着它将永远运行。这可能不是您打算要做的。
答案 1 :(得分:1)
您的循环在哪里。试试这个
connect = DriverManager
.getConnection("jdbc:mysql://126.32.3.20/fulltext_ltat?"
+ "user=root&password=root&rewriteBatchedStatements=true");