我有以下代码片段。我删除了更多细节。我有一个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();
}
}
怎么样'INSERT忽略...作为一个测试。那么它真正解决了真正的问题?我不确定*忽略*实际上是否会幸免于例外部分。 – Drew
如果因为数据导致sql语法错误而出现异常,那么您可能容易受到[sql注入攻击](http://bobby-tables.com) –
是的,我了解该漏洞,但我没有选择但是要忽略那个例外,继续剩下的事情? – user5313398