【问题标题】:Ignore exception and continue to insert the rest of the data忽略异常,继续插入其余数据
【发布时间】:2016-08-16 16:21:09
【问题描述】:

我有以下代码sn-p。我删除了更多细节。我有一个包含所有数据的 for 循环。我为 (int i = 0; i

这是代码。

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

【问题讨论】:

  • 如何将INSERT IGNORE .... 作为测试。那么它现实解决了真正的问题吗?我不确定 ignore 是否真的会在异常部分中幸存下来。
  • 如果您因为您的数据导致 sql 语法错误而导致异常,那么您可能容易受到sql injection attacks
  • 是的,我了解该漏洞,但我别无选择,只能忽略该异常并继续其余部分?
  • 在波浪形包裹的错误状态下回滚不是你的捕获吗?
  • 你指的是哪一个?我不明白你的?

标签: java mysql exception-handling


【解决方案1】:

您需要将 try/catch 块放在 for 中,围绕 executeUpdate(insertCommand);

【讨论】:

  • 如果我像这样尝试 { catch (Exception e) { System.out.println("Ignore Error SQL Exception : "+ex.toString()); ex.printStackTrace(System.out);我已经输入了忽略这个词,所以至少我知道这是忽略这是否足够?
【解决方案2】:

你也需要在循环中捕获错误

....
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 
        }
}
....

【讨论】:

  • 是的,这就是我正在做的我试过这个如果我像这样尝试 { catch (Exception e) { System.out.println("‌​忽略错误 SQL 异常:"+ex. toString()); ex.printStackTrace(S‌​ystem.out);我已经输入了忽略这个词,所以至少我知道这是忽略这是否足够?
猜你喜欢
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-09
  • 2015-12-01
  • 2010-11-22
  • 1970-01-01
相关资源
最近更新 更多