Java : 如何忽略一些SQLException?

Java : How to ignore some SQLException?

我正在尝试使用 jdbc 删除 Oracle 数据库中的一些对象。如果发生特定的 ORA-04043 错误,我想跳过。以下是我构建的代码。

这个数组字符串变量是SqlList.uninstall_OS_COMMAND_SQL。

public static String[] uninstall_OS_COMMAND_SQL = {
        "DROP PACKAGE OS_COMMAND",
        "DROP PACKAGE LOB_WRITER_PLSQL",
        "DROP TYPE OSCOMMAND_VC2_ARRAY",
        "DROP TYPE OSCOMMAND_DIR_ARRAY",
        "DROP TYPE OSCOMMAND_DIR_ENTRY",
        "DROP TYPE FILE_LIST_TYPE",
        "DROP TYPE FILE_TYPE",
        "DROP PACKAGE FILE_PKG",
        "DROP JAVA SOURCE \"OS_HELPER\"",
        "DROP JAVA SOURCE \"FILE_TYPE_JAVA\"",
        "DROP PACKAGE FILE_SECURITY"
    };

这是代码。

private void uninstallOS_COMMAND_Step1_For_11g() {
        Connection targetDBconn = null;
        Statement stmt = null;

        try {
            targetDBconn = globalTargetConn.connect();
            logWriter.writeLogs(logTextArea, LogWriter.INFO, "Uninstalling OS_COMMAND package...");
            for (int i = 0; i < SqlList.uninstall_OS_COMMAND_SQL.length; i++) {
                stmt = targetDBconn.createStatement();
                stmt.setEscapeProcessing(false);
                logWriter.writeLogs(logTextArea, LogWriter.INFO, "See the query below...");
                logWriter.writeLogs(logTextArea, LogWriter.INFO, "\n"+SqlList.uninstall_OS_COMMAND_SQL[i]);
                stmt.executeUpdate(SqlList.uninstall_OS_COMMAND_SQL[i]);
            }
        } catch (SQLException ex) { logWriter.writeLogs(logTextArea, LogWriter.ERROR, ex.getMessage());
        } finally {
            if (stmt != null ) try {stmt.close();} catch(SQLException ex) {}
            if (targetDBconn != null ) try {targetDBconn.close();} catch(SQLException ex) {}
        }
    }

如果我 运行 这段代码,它只执行数组中的一项并停止整个方法。请帮帮我..

创建一个新方法并在循环中调用新方法

喜欢

execute(Statement stmt, SQL Command ) throws Exception {
     try {
          stmt.executeUpdate(SqlList.uninstall_OS_COMMAND_SQL[i]);
     } catch(SQLException s) {
     //LOG error
        if (s.getMessage().contains("ORA-04043")) {

        } else {
            throw s;
        }
    }  catch (Exception ee) {
        throw ee;
    }
   }

您可能会考虑将 try 和 catch 放在循环中。