我正在通过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();}
答案 0 :(得分: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.
}
答案 1 :(得分:1)
下面的代码为您提供了一个提示。发生错误时打印ClientNumber取决于您要打印的确切内容。但是你可以在内部catch中添加另一个日志。我故意使用异常而不是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();}
}