Java SQL Execute executeDDL(String ddl)

Here you can find the source of executeDDL(String ddl)

Description

Execute a DDL statement

License

Apache License

Declaration

public static void executeDDL(String ddl) throws SQLException 

Method Source Code

//package com.java2s;
/*//from   w  w w .  jav  a  2 s  .co  m

 Derby - Class org.apache.derbyDemo.vtis.core.VTIHelper

 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 */

import java.sql.*;

public class Main {
    private static final boolean DEBUG = true;

    /**
     * <p>
     * Execute a DDL statement
     * </p>
     */
    public static void executeDDL(String ddl) throws SQLException {
        Connection conn = getLocalConnection();

        // now register the function
        print(ddl);
        try {
            PreparedStatement createStatement = conn.prepareStatement(ddl);

            createStatement.execute();
            createStatement.close();
        } catch (SQLException t) {
            SQLException s = new SQLException("Could not execute DDL:\n"
                    + ddl);

            s.setNextException(t);

            throw s;
        }
    }

    /**
     * <p>
     * Get the connection to the local database.
     * </p>
     */
    public static Connection getLocalConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:default:connection");
    }

    /**
     * <p>
     * Debug helper method to print a diagnostic.
     * </p>
     */
    public static void print(String text) {
        if (DEBUG) {
            System.out.println(text);
        }
    }
}

Related

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