2016-08-16 41 views
0

我有以下代码片段。我删除了更多细节。我有一个for循环与其中的所有数据。我运行for循环for(int i = 0; i < list.getLength(); i ++)。会发生什么情况是,当任何一个数据导致错误的sql说数据有一个斜杠等,然后它会导致异常和for循环的其余部分不能继续。我怎么能跳过那个例外,继续休息?忽略异常并继续插入其余的数据

这是一段代码。

Connection dbconn = null; 
    Statement stmt1 = null; 
    Statement stmt2 = null; 
    try 
    { 
     dbconn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1", "tes1", "te15"); 
     stmt1 = dbconn.createStatement(); 
     stmt2 = dbconn.createStatement(); 
     DateFormat outDf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
     Date date = Calendar.getInstance().getTime(); 

     String value = null; 
     for (int i = 0; i < list.getLength(); i++) 
     { 

      String insertCommand = "INSERT INTO command SET ........."; 
      System.out.println("\n SET INSERT :" + insertCommand); 
      int count = stmt1.executeUpdate(insertCommand); 
     } 

    } 
    catch (SQLException ex) 
    { 
     System.out.println("MyError Error SQL Exception : " + ex.toString()); 
    } 
    catch (Exception rollback) 
    { 
     System.out.println("\nRollback :"); 
     rollback.printStackTrace(System.out); 
    } 
    catch (Exception e) 
    { 
     System.out.println("\n Error here :"); 
     e.printStackTrace(System.out); 

    } 
    finally 
    { 
     try 
     { 
      if (stmt1 != null) 
      { 
       stmt1.close(); 
      } 

     } 
     catch (SQLException ex) 
     { 
      System.out.println("MyError: SQLException has been caught for stmt1 close"); 
      ex.printStackTrace(System.out); 
     } 
     try 
     { 
      if (stmt2 != null) 
      { 
       stmt2.close(); 
      } 

     } 
     catch (SQLException ex) 
     { 
      System.out.println("MyError: SQLException has been caught for stmt2 close"); 
      ex.printStackTrace(System.out); 
     } 
     try 
     { 
      if (dbconn != null) 
      { 
       dbconn.close(); 
      } 
      else 
      { 
       System.out.println("MyError: dbConn is null in finally close"); 
      } 
     } 
     catch (SQLException ex) 
     { 
      System.out.println("MyError: SQLException has been caught for dbConn close"); 
      ex.printStackTrace(); 
     } 
    } 
+0

怎么样'INSERT忽略...作为一个测试。那么它真正解决了真正的问题?我不确定*忽略*实际上是否会幸免于例外部分。 – Drew

+0

如果因为数据导致sql语法错误而出现异常,那么您可能容易受到[sql注入攻击](http://bobby-tables.com) –

+0

是的,我了解该漏洞,但我没有选择但是要忽略那个例外,继续剩下的事情? – user5313398

回答

1

您需要捕获错误在环太

.... 
for (int i = 0; i < list.getLength(); i++) { 
    try { 
     String insertCommand = "INSERT INTO command SET ........."; 
     System.out.println("\n SET INSERT :" + insertCommand); 
     int count = stmt1.executeUpdate(insertCommand); 
     } catch (Exception e) { 
      // Better catch the real exception 
      // Handle the exception 
     } 
} 
.... 
+0

是的,这就是我在做什么我试过这个\t 如果我把这个样子{catch(Exception e){System.out.println(“Ignore Error SQL Exception:”+ ex.toString()); ex.printStackTrace(S ystem.out); }我已经把这个词忽略,所以至少我知道这是不理会这会足够吗? – user5313398

2

你需要把try/catch块的内幕,各地executeUpdate(insertCommand);

+0

如果我这样写,请尝试{catch(Exception e){System.out.println(“忽略错误SQL异常:”+ ex.toString()); ex.printStackTrace(System.out); }我已经把这个词忽略,所以至少我知道这是不理会这会足够吗? – user5313398