Java SQL Execute executeDDL(final Connection connection, final String sql, final int... ignoreErrors)

Here you can find the source of executeDDL(final Connection connection, final String sql, final int... ignoreErrors)

Description

Helper method for executing DDL (or technically: any statement), ignoring the specified list of error codes.

License

LGPL

Parameter

Parameter Description
connection Connection to execute statement
sql DDL statement
ignoreErrors Firebird error codes to ignore

Exception

Parameter Description
SQLException SQLException for executing statement, except for errors with the error code listed in<code>ignoreErrors</code>

Declaration

public static void executeDDL(final Connection connection,
        final String sql, final int... ignoreErrors)
        throws SQLException 

Method Source Code

//package com.java2s;
/*// w  ww . j a v a2  s  . co  m
 * Firebird Open Source JavaEE Connector - JDBC Driver
 *
 * Distributable under LGPL license.
 * You may obtain a copy of the License at http://www.gnu.org/copyleft/lgpl.html
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * LGPL License for more details.
 *
 * This file was created by members of the firebird development team.
 * All individual contributions remain the Copyright (C) of those
 * individuals.  Contributors to this file are either listed here or
 * can be obtained from a source control history command.
 *
 * All rights reserved.
 */

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;

public class Main {
    /**
     * Helper method for executing DDL (or technically: any statement), ignoring the specified list of error codes.
     *
     * @param connection
     *         Connection to execute statement
     * @param sql
     *         DDL statement
     * @param ignoreErrors
     *         Firebird error codes to ignore
     * @throws SQLException
     *         SQLException for executing statement, except for errors with the error code listed in
     *         <code>ignoreErrors</code>
     * @see org.firebirdsql.gds.ISCConstants
     */
    public static void executeDDL(final Connection connection,
            final String sql, final int... ignoreErrors)
            throws SQLException {
        if (ignoreErrors != null) {
            Arrays.sort(ignoreErrors);
        }

        try (Statement stmt = connection.createStatement()) {
            stmt.execute(sql);
        } catch (SQLException ex) {
            if (ignoreErrors == null || ignoreErrors.length == 0)
                throw ex;

            for (Throwable current : ex) {
                if (current instanceof SQLException
                        && Arrays.binarySearch(ignoreErrors,
                                ((SQLException) current).getErrorCode()) >= 0) {
                    return;
                }
            }

            throw ex;
        }
    }
}

Related

  1. execute(Statement statement, String sql)
  2. execute(String sql, Connection connection)
  3. executeCall(Connection connection, String command, String schemaName, String tableName, int paramLength)
  4. executeCallable(Connection conn, String sql)
  5. executeCreateDB(String className, String URL, String userName, String password, String dbName)
  6. executeDDL(String ddl)
  7. executeImmediate(String sql)
  8. executeInsert(Connection conn, String sql, boolean hasAutoGeneratedKey, Object... parameters)
  9. executeInsert(Connection conn, String targetTable, String destinationTable)