Java:如何忽略一些SQLException?

时间:2015-12-08 04:11:44

标签: java oracle 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) {}
        }
    }

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

2 个答案:

答案 0 :(得分:0)

创建一个新方法并调用新方法inloop

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

答案 1 :(得分:0)

你可能会考虑将你的尝试放入循环中。