java-捕获异常时更新表状态
我想了很久,想不出解决这个问题的办法 我有一个Java程序,它从MySQL中读取一个具有活动状态的表。该表如下所示:java-捕获异常时更新表状态,java,mysql,Java,Mysql,我想了很久,想不出解决这个问题的办法 我有一个Java程序,它从MySQL中读取一个具有活动状态的表。该表如下所示: UniqueID FilePath Status 1 C:\Folder1\abc.pdf Active 2 C:\Folder1\def.pdf Active 3 C:\Folder1\efg.pdf Error 想法很简单:我从fil
UniqueID FilePath Status
1 C:\Folder1\abc.pdf Active
2 C:\Folder1\def.pdf Active
3 C:\Folder1\efg.pdf Error
想法很简单:我从filepath找到文件位置,调用一些函数来提取文件并对其执行索引处理。在整个过程中,程序完成后,状态将从“活动”变为“处理”,然后变为“完成”
代码如下:
public void doScan_DB() throws Exception {
try {
Statement statement = con.connect().createStatement();
ResultSet rs = statement.executeQuery("select * from filequeue where Status='Active'");
while (rs.next()) {
// get the filepath of the PDF document
String path1 = rs.getString(2);
int getNum= rs.getInt(1);
// while running the process, update status : Processing
updateProcess_DB(getNum);
// call the index function
Indexing conn = new Indexing();
conn.extractDocuments(path1);
// After completing the process, update status: Complete
updateComplete_DB(getNum);
// if error occurs
// call this method updateError_DB(getNum);
}
}catch(SQLException|IOException e){
e.printStackTrace();
}
}
提取文档方法:
public void extractDocuments(String path) throws Exception{
ArrayList<String> list = new ArrayList<String>();
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(" ");
// words.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
for (String word : words) {
// check if one or more special characters at end of string then remove OR
// check special characters in beginning of the string then remove
// uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
list.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
// uniqueWords.add(word.replaceAll("([\\W]+$)|(^[\\W]+)", ""));
}
}
}
} catch (IOException e) {
System.err.println("Exception while trying to read pdf document - " + e);
}
String[] words1 =list.toArray(new String[list.size()]);
// String[] words2 =uniqueWords.toArray(new String[uniqueWords.size()]);
// MysqlAccessIndex connection = new MysqlAccessIndex();
index(words1,path);
System.out.println("Completed");
}
}
我有另一种方法用于将错误状态更新到表中,还有另一种方法用于完成该过程
是否有方法将abc.pdf的状态更新为Error,将def.pdf的状态更新为Complete
到目前为止,我的代码将为文件抛出一个异常,但仍然会更新这两个状态以完成。正确的做法是只更新在extractDocuments()方法中抛出FileException错误的状态。由于它捕获了异常,因此它仍将始终在doScan\u DB
中运行updateComplete\u DB
是否有适当的方法来处理此问题,因为任何建议都是值得赞赏的?有几种方法可以处理此问题,但我将从extractDocuments内部删除try/catch,并在doScan\u DB中调用相同的方法
try {
conn.extractDocuments(path1);
updateComplete_DB(getNum);
} catch (Exception e) {
updateError_DB(getNum);
}
也许你也应该换一种方法来更新状态
public void updateStatus_DB(int id, String status){
try{
Statement test = con.connect().createStatement();
test.executeUpdate("update filequeue SET STATUS ='" + status + "' where UniqueID= "+ id);
} catch(Exception e){
e.printStackTrace();
}
}
有几种方法可以处理这个问题,但我会从extractDocuments内部删除try/catch,并在doScan_DB中调用相同的方法
try {
conn.extractDocuments(path1);
updateComplete_DB(getNum);
} catch (Exception e) {
updateError_DB(getNum);
}
也许你也应该换一种方法来更新状态
public void updateStatus_DB(int id, String status){
try{
Statement test = con.connect().createStatement();
test.executeUpdate("update filequeue SET STATUS ='" + status + "' where UniqueID= "+ id);
} catch(Exception e){
e.printStackTrace();
}
}
extractDocuments
没有引发异常,因此,此代码将失败。您需要抛出异常(首先不要捕获)。此外,使用“e.printStackTrace();”消费异常这是一个非常糟糕的主意。@SukhpalSingh正如我在文本中所写的那样,try/catch应该被删除,这样代码就不会失败,请在键入之前阅读。是的,错误处理很糟糕,但我只是复制现有代码以建议改进方法,使方法可重用,错误处理不是这里的重点。然后从内部删除try/catch提取器文档必须从内部删除try/catchextractDocuments@JoakimDanielson谢谢你的建议,我要试试看tomorrow@SukhpalSingh威尔提到了这句话的第一部分“有几种方法来处理这件事……”。让它休息一下,尝试添加一些建设性的响应,而不是错误地尝试标记单词。extractDocuments
不会引发异常,因此,此代码将失败。您需要抛出异常(首先不要捕获)。此外,使用“e.printStackTrace();”消费异常这是一个非常糟糕的主意。@SukhpalSingh正如我在文本中所写的那样,try/catch应该被删除,这样代码就不会失败,请在键入之前阅读。是的,错误处理很糟糕,但我只是复制现有代码以建议改进方法,使方法可重用,错误处理不是这里的重点。然后从内部删除try/catch提取器文档必须从内部删除try/catchextractDocuments@JoakimDanielson谢谢你的建议,我要试试看tomorrow@SukhpalSingh威尔提到了这句话的第一部分“有几种方法来处理这件事……”。让它休息一下,尝试添加一些建设性的回应,而不是错误地试图标记单词。