我有一个 Java 程序,它读取 MySQL 表中的文件路径,然后找到该文件并继续对该文件进行一些索引处理。
表设计如下:
UniqueID FilePath Status
1 C:\Folder1\abc.pdf Active
2 C:\Folder1\def.pdf Active
3 C:\Folder1\efg.pdf Error
为简单起见,它会在表中扫描 status = Active 的文件,然后找到该文件并进行索引处理。整个过程完成后,状态将更新为Complete。
问题是我扫描结果集中的每一个。例如,如果abc.pdf不存在,则将状态更新为错误并将状态更新def.pdf为完成。
以我目前的方式,它只是将这两个文件都更新为状态完成,我不知道如何实现我想要的。任何人都可以提出任何建议吗?
另外,假设doScan()方法抛出了一些错误,比如找不到文件,那么状态应该更改为错误abc.pdf
代码:
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);
// while running the process, update status : Processing
updateProcess_DB();
// call the index function
Indexing conn = new Indexing();
conn.doScan(path1);
updateComplete_DB();
}
}catch(SQLException|IOException e){
e.printStackTrace();
}
更新过程:
public void updateProcess_DB(){
try{
Statement statement = con.connect().createStatement();
statement.execute("update filequeue SET STATUS ='Processing' where STATUS ='Active' ");
}catch(Exception e){
e.printStackTrace();
}
更新完成:
public void updateComplete_DB() {
try {
Statement statement = con.connect().createStatement();
statement.execute("update filequeue SET STATUS ='Complete' where STATUS ='Processing' ");
} catch (Exception e) {
e.printStackTrace();
}
}
更新错误:
public void updateError_DB(){
try{
Statement statement = con.connect().createStatement();
statement.execute("update filequeue SET STATUS ='Error' where STATUS ='Processing' ");
}catch(Exception e){
e.printStackTrace();
}
}
编辑:
代码:
while (rs.next()) {
// get the filepath of the PDF document
String path1 = rs.getString(2);
// while running the process, update status : Processing
updateProcess_DB(rs.getString(2));
// call the index function
Indexing conn = new Indexing();
conn.doScan(path1);
updateComplete_DB(path1);
更新方法:
public void updateProcess_DB(String argument){
try{
Statement statement = con.connect().createStatement();
statement.execute("update filequeue SET STATUS ='Processing' where STATUS ='Active' ");
}catch(Exception e){
e.printStackTrace();
}
}