Java JDBC Connection Create createDatabase(String dbConnectionString, String dbName, String user, String pass, String driver)

Here you can find the source of createDatabase(String dbConnectionString, String dbName, String user, String pass, String driver)

Description

create Database

License

Apache License

Declaration

public static void createDatabase(String dbConnectionString, String dbName, String user, String pass,
            String driver) throws Exception 

Method Source Code

//package com.java2s;
/**//from  ww  w  .  j a v  a  2  s.  com
 * Copyright 2016 Symantec Corporation.
 * 
 * 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 {
    public static void createDatabase(String dbConnectionString, String dbName, String user, String pass,
            String driver) throws Exception {
        Connection conn = null;
        Statement stmt = null;
        try {
            // STEP 2: Register JDBC driver
            Class.forName(driver);

            // STEP 3: Open a connection
            System.out.println("Connecting to database...");
            conn = DriverManager.getConnection(dbConnectionString, user, pass);

            // STEP 4: Execute a query
            System.out.println("Creating database...");
            stmt = conn.createStatement();

            String sql = "CREATE DATABASE IF NOT EXISTS " + dbName;
            stmt.executeUpdate(sql);
            System.out.println("Database created successfully...");
        } finally {
            // finally block used to close resources
            try {
                if (stmt != null)
                    stmt.close();
            } catch (SQLException se2) {
            } // nothing we can do
            try {
                if (conn != null)
                    conn.close();
            } catch (SQLException se) {
                se.printStackTrace();
            } // end finally try
        } // end try
    }
}

Related

  1. canConnect(Properties dbSettings)
  2. clearTable(String tableName, String driver, String url, String user, String password)
  3. createConnection()
  4. createConnection(Properties props, String prefix)
  5. createDbConnection(String dbUrl, String dbUser, String dbPass, String dbDriverClass)
  6. getAccessConn(String path)
  7. getConnect(String driver, String url, String user, String password)
  8. getConnection()