-2

我正在通过一个 Java 程序运行一个大型、多样化的数据集,该程序从旧模式中导入数据,对其进行转换,然后将数据插入到新模式中。该程序已在试点数据上成功测试,但在现实世界数据上抛出异常。

我希望能够计算整个数据集引发了多少异常,并记录哪些记录引发了异常。有人可以告诉我如何做到这一点吗?

就目前而言,当程序遇到第一个异常时,它当前正在崩溃,所以我不知道如果代码能够一直通过数据集,是否会有一个异常或 1,000 个异常。

我在下面附上了我的代码的相关方面。如何更改它以便在记录计数和 ClientNumber 的同时跳过异常?

try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection sourceConn = DriverManager.getConnection("jdbc:odbc:source_db");
    Statement st = sourceConn.createStatement();
    Connection destinationConn = DriverManager.getConnection("jdbc:odbc:destination_db");

    int ClientNumber;
    String Name;
    ResultSet rest = st.executeQuery("SELECT * FROM sourceTable");
    PreparedStatement ps5 = null;
    PreparedStatement ps6 = null;
    PreparedStatement ps7 = null;
    PreparedStatement ps8 = null;
    while(rest.next()){
        ClientNumber = rest.getInt(1);
        Name = rest.getString(2);//plus other variables skipped here for brevity
        ps5 = destinationConn.prepareStatement(
            "INSERT INTO Clients ("
            + "ClientNumber, Name) "
            +"VALUES (?, ?)"
            );
        ps5.setInt(1, ClientNumber);
        ps5.setString(2, Name);
        ps5.executeUpdate();
        //some other stuff for ps6,ps7,ps8
        destinationConn.commit();
    }
    //ps5.close();
}
catch (ClassNotFoundException cnfe){cnfe.printStackTrace();}
catch (SQLException e) {e.printStackTrace();}
4

2 回答 2

2

这个怎么样,只是用不同的方式编写循环:

try {
    // TODO: write your setup code here

    boolean hasNext = false;
    try {
        hasNext = rest.next();
    }catch (Exception e) {
        // TODO: log exception, increase a counter
    }
    while(hasNext){
        try {
            // TODO: write your processing code here

        }catch (Exception e) {
            // TODO: log exception, increase a counter
        }
        try {
            hasNext = rest.next();
        }catch (Exception e) {
            // TODO: log exception, increase a counter
            hasNext = false; //prevent infinite loops
        }
    }
} catch (Exception e){
    // TODO: this should never happen, handle ClassNotFoundException etc.
}

更新:我们可以摆脱两次调用 next() 的麻烦,如下所示:

try {
    // TODO: write your setup code here

    while(true){
        try {
            if(!rest.next()){
                break;
            }
        }catch (Exception e) {
            // TODO: log exception, increase a counter
            break;
        }
        try {
            // TODO: write your processing code here

        }catch (Exception e) {
            // TODO: log exception, increase a counter
        }
    }
} catch (Exception e){
    // TODO: this should never happen, handle ClassNotFoundException etc.
}
于 2013-10-13T18:28:37.887 回答
1

下面的代码给你一个提示。发生错误时打印 ClientNumber 取决于您要打印的确切内容。但是您可以在内部捕获中添加另一个日志。我故意使用异常而不是 SQLException 因为你得到的异常可能是你在帖子中没有提到的任何类型的异常

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection sourceConn = DriverManager.getConnection("jdbc:odbc:source_db");
Statement st = sourceConn.createStatement();
Connection destinationConn = DriverManager.getConnection("jdbc:odbc:destination_db");

int ClientNumber;
int errCounter = 0; 
String Name;
ResultSet rest = st.executeQuery("SELECT * FROM sourceTable");
PreparedStatement ps5 = null;
PreparedStatement ps6 = null;
PreparedStatement ps7 = null;
PreparedStatement ps8 = null;
while(rest.next()){
    try {
    ClientNumber = rest.getInt(1);
    Name = rest.getString(2);//plus other variables skipped here for brevity
    ps5 = destinationConn.prepareStatement(
        "INSERT INTO Clients ("
        + "ClientNumber, Name) "
        +"VALUES (?, ?)"
        );
    ps5.setInt(1, ClientNumber);
    ps5.setString(2, Name);
    ps5.executeUpdate();
    //some other stuff for ps6,ps7,ps8
    destinationConn.commit();
    }catch (Exception e) {
      e.printStackTrace();
      errCounter++;
    }
} //end while
 if(errCounter > 0) 
   System.out.println(String.format("Error occured %d times", errCounter));

} catch (ClassNotFoundException cnfe){
    cnfe.printStackTrace();}     
}
于 2013-10-13T18:18:24.367 回答