发现异常后继续执行

时间:2018-06-21 16:08:27

标签: java sql-server exception try-catch sql-insert

我正在制作一个小程序,用于从文本文档插入数据库。我使用哈希图从文本文档中获取值,因为我不想使用数组,并且文本文档的格式为Key:value。目前,我的代码正确插入,但是当发现异常时,程序在该异常点结束。我的目标是即使发现异常后也可以运行它,它并不一定会忽略它,但是无论如何我都希望继续执行。

BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(
                                "C:\\Users\\darroyo\\Documents\\pruebasx.txt"));
} catch (FileNotFoundException e1) {

e1.printStackTrace();
}
String line;
try {
line = reader.readLine();
} catch (IOException e1) {
e1.printStackTrace();
}

String query = " insert into FRONTMC.HECHO (folio_hecho, folio_orden, emisora, serie,"
                            + "clave_sentido, titulos_hecho, precio, importe, liquidacion, contraparte, id_estatus, isin, contrato,"
                            + "secondary_exec_id, exec_id, F11_ClOrdID, fecha_recepcion, fecha_sentra)"
                                + " values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,convert(varchar(30),cast(? as datetime),120),convert(varchar(30),cast(? as datetime),120))";

PreparedStatement preparedStmt = null;
try {
preparedStmt = con.prepareStatement(query);
} catch (SQLException e1) {
e1.printStackTrace();
}
Map<Integer,String> hm1 = new HashMap<Integer,String>();
try {
    while ((line = reader.readLine()) != null) {
    String[] tokens = line.split("");
    for (int i = 0; i != tokens.length; i++) {
        int dataIndex = tokens[i].indexOf('=') + 1;
        String data = tokens[i].substring(dataIndex);
        hm1.put(new Integer(i),data);
        }
for(int counter =0;counter <=line.length();counter++){
    preparedStmt.setString(1, hm1.get(19));
    preparedStmt.setString(2, hm1.get(19));
    preparedStmt.setString(3, hm1.get(3));
    preparedStmt.setString(4, hm1.get(34));
    preparedStmt.setString(5, hm1.get(15));
    preparedStmt.setString(6, hm1.get(30));
    preparedStmt.setString(7, hm1.get(16));
    preparedStmt.setString(8, hm1.get(18));
    preparedStmt.setString(9, hm1.get(8));
    preparedStmt.setString(10, hm1.get(33));
    preparedStmt.setString(11, hm1.get(27));
    preparedStmt.setString(12, hm1.get(17));
    preparedStmt.setString(13, hm1.get(26));
    preparedStmt.setString(14, hm1.get(23));
    preparedStmt.setString(15, hm1.get(10));
    preparedStmt.setString(16, hm1.get(14));

    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat sdf2 = new SimpleDateFormat("dd/MM/yyyy");
    String ds2 = sdf2.format(sdf1.parse(hm1.get(6)));
    String newfecha1 = ds2;
    preparedStmt.setString(17, newfecha1);

    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat sdf4 = new SimpleDateFormat("dd/MM/yyyy");
    String ds4 = sdf4.format(sdf3.parse(hm1.get(6)));
    String newfecha3 = ds4;
    preparedStmt.setString(18, newfecha3);
    }
    try{
        preparedStmt.execute();
    }catch(SQLException e3){
    e3.printStackTrace();
    reader.readLine();
    continue;
    }
    }
    } catch (IOException e1) {
    e1.printStackTrace();
    } catch (SQLException e1) {
    e1.printStackTrace();
    } catch (ParseException e1) {
    System.out.println("e1.getmessage()");
    }

我尝试将准备好的语句包含在try catch中,但是它仍然停止,正如您所看到的,我也使用了continue,但是它仍然停止。一切正常,我只想让它在发生错误后继续执行,问题出在哪里?目前的错误是底部ParseException的错误。我想在收到此异常后运行。

2 个答案:

答案 0 :(得分:0)

我注意到您的try块主要由for循环占用,我认为它有多次机会引发异常。

是否可以将try-catch块移至for循环的内部而不是外部?这样,您可以在循环过程中捕获多个异常而无需停止循环。

顺便问一下,您能写出块中的哪些语句抛出SQLException吗?

答案 1 :(得分:0)

您需要将try/catch块移至while

while ((line = reader.readLine()) != null) {
    try {
        String[] tokens = line.split("");
        for (int i = 0; i != tokens.length; i++) {
            int dataIndex = tokens[i].indexOf('=') + 1;
            String data = tokens[i].substring(dataIndex);
            hm1.put(new Integer(i),data);
        }
        // Try to parse line and insert into db
        preparedStmt.setString(1, hm1.get(19));
        preparedStmt.setString(2, hm1.get(19));
        // etc....
        // No other try/catch here to skip entire 
        // line on error
        preparedStmt.execute();
    }
    catch (SQLException e1) {
        e1.printStackTrace();
    }
    catch (IOException e2) {
        e2.printStackTrace();
    }
    catch (ParseException e3) {
        System.out.println("e3.getmessage()");
    }
} // while