捕获异常后,java-更新表状态

时间:2018-11-26 11:49:45

标签: 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

想法很简单:我从文件路径中找到文件位置,调用一些函数来提取文件并对其执行索引处理。在整个过程中,程序完成后,状态将从“活动”变为“正在处理”,然后变为“完成”。

下面是代码:

 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();

        }



    }

ExtractDocument方法:

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不存在,因此它将引发文件异常错误。而且我传递参数来更新每一行以进行如下处理:

 public void updateProcess_DB(int argument){

  try{



        Statement test = con.connect().createStatement();
     test.executeUpdate("update filequeue SET STATUS ='Processing' where UniqueID= "+argument);

  }catch(Exception e){

      e.printStackTrace();

  }

    }

我有另一种将错误状态更新到表的方法,还有另一种用于完成该过程的方法。

是否可以将abc.pdf的状态更新为错误,将def.pdf的状态更新为完成?

到目前为止,我的代码将对该文件引发异常,但仍将两个状态都更新为“完成”。正确的想法是仅更新在extractDocuments() method中引发FileException错误的STATUS。由于它捕获到异常,因此它将始终始终在updateComplete_DB中运行doScan_DB

有什么建议可以解决吗?

1 个答案:

答案 0 :(得分:2)

有几种方法可以解决这个问题,但是我会从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(); 
   }
}