James
James

Reputation: 315

Java : How to ignore some SQLException?

I'm trying to drop some objects in Oracle database using jdbc. I want to skip if specific ORA-04043 error occurs. The followings are the code I built.

This array string variable is 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"
    };

and this is the code.

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) {}
        }
    }

If I run this code, it executes just one item in the array and stops the entire method. Please help me..

Upvotes: 0

Views: 2130

Answers (2)

Ruelos Joel
Ruelos Joel

Reputation: 2389

you might consider to put your try and catch inside the loop.

Upvotes: 0

Ahmed
Ahmed

Reputation: 184

Create one new method and call the new method inloop

Like

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

Upvotes: 1

Related Questions