Java JDBC HSQL Connection createSchemasForHSQLDB(String dbName, String[] schemaNames)

Here you can find the source of createSchemasForHSQLDB(String dbName, String[] schemaNames)

Description

This will connect to the in-memory HSQL DB dbName and issue CREATE SCHEMA commands for each schema named in schemaNames .

License

Apache License

Parameter

Parameter Description
dbName the name of the database.
schemaNames the names of the schemas which need to be created.

Exception

Parameter Description
ClassNotFoundException if the HSQL JDBC driver class cannot be found.
SQLException if there are SQL problems.

Declaration

public static void createSchemasForHSQLDB(String dbName, String[] schemaNames)
        throws ClassNotFoundException, SQLException 

Method Source Code


//package com.java2s;
/*/*w  ww . j a v  a 2  s . c  om*/
 * Copyright (C) 2010 Trustees of the University of Pennsylvania
 *
 * Licensed 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.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Main {
    /** Qualified name of HSQL JDBC driver class. */
    static final String HSQL_DRIVER = "org.hsqldb.jdbcDriver";

    /**
     * This will connect to the in-memory HSQL DB {@code dbName} and issue
     * {@code CREATE SCHEMA} commands for each schema named in {@code
     * schemaNames}.
     * 
     * <p>
     * The HSQLDB (unlike DB2?) does not seem to handle implicitly created DB
     * schemas, so if A does not already exist, things like {@code CREATE TABLE
     * A.B} ... fail instead of causing A to be created.
     * </p>
     * 
     * @param dbName the name of the database.
     * @param schemaNames the names of the schemas which need to be created.
     * 
     * @throws ClassNotFoundException if the HSQL JDBC driver class cannot be found.
     * @throws SQLException if there are SQL problems. 
     */
    public static void createSchemasForHSQLDB(String dbName, String[] schemaNames)
            throws ClassNotFoundException, SQLException {
        Class.forName(HSQL_DRIVER);

        Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:" + dbName, "sa", "");
        Statement createSchema = conn.createStatement();
        for (String schema : schemaNames) {
            createSchema.executeUpdate("CREATE SCHEMA " + schema + " AUTHORIZATION dba");
        }
    }
}

Related

  1. createConnection()
  2. createHSQLDBConnection(String hsqldbName)
  3. getConnection()
  4. getDbConnection()
  5. createTweetTable()
  6. getHSQLConnection()