Java JDBC Connection Create getConnection(String driverClass, String url, String user, String password)

Here you can find the source of getConnection(String driverClass, String url, String user, String password)

Description

Opens a connection to the specified database using the JDBC driver class , the url, the user name, and the password provided.

License

Apache License

Parameter

Parameter Description
driverClass The name of the JDBC driver class.
url The URL to the database server / JDBC connection URL.
user The user name to be used to connect to the database.
password The password of the user to be used to connect to the database.

Exception

Parameter Description
ClassNotFoundException If the JDBC driver class could not be found (is not on the classpath)
SQLException If something goes wrong during the connection, for instance if thedatabase/catalogue name in the URL is not recognized by the database server.
IllegalAccessException If your application does not have permission toinstantiate the database driver.
InstantiationException If an instance of the JDBC driver class could not be created.

Return

A connection to the database if it could be opened.

Declaration

public static Connection getConnection(String driverClass, String url,
        String user, String password) throws ClassNotFoundException,
        SQLException, IllegalAccessException, InstantiationException 

Method Source Code

//package com.java2s;
/*/*from w w w  .ja  va2  s . com*/
 Copyright 2008 Jenkov Development

 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.*;

public class Main {
    /**
     * Opens a connection to the specified database using the JDBC driver class ,
     * the url, the user name, and the password provided.
     * @param driverClass The name of the JDBC driver class.
     * @param url         The URL to the database server / JDBC connection URL.
     * @param user        The user name to be used to connect to the database.
     * @param password    The password of the user to be used to connect to the database.
     * @return A connection to the database if it could be opened.
     * @throws ClassNotFoundException If the JDBC driver class could not be found (is not on the classpath)
     * @throws SQLException If something goes wrong during the connection, for instance if the
     *                    database/catalogue name in the URL is not recognized by the database server.
     * @throws IllegalAccessException If your application does not have permission to
     *                    instantiate the database driver.
     * @throws InstantiationException If an instance of the JDBC driver class could not be created.
     */
    public static Connection getConnection(String driverClass, String url,
            String user, String password) throws ClassNotFoundException,
            SQLException, IllegalAccessException, InstantiationException {
        Class.forName(driverClass).newInstance();
        return DriverManager.getConnection(url, user, password);
    }
}

Related

  1. getConnection(String dbUrl, String dbUser, String dbPassword)
  2. getConnection(String driver, String connectionString, String userName, String password)
  3. getConnection(String driver, String connectString)
  4. getConnection(String driver, String url, String user, String pass)
  5. getConnection(String driver, String url, String username, String password)
  6. getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password)
  7. getConnection(String drvr, String url, String nm, String pass)
  8. getConnection(String filename)
  9. getConnection(String jdbcDriverName, String connectionURL)