Java JDBC Connection Create getConnection(final String driver, final String url)

Here you can find the source of getConnection(final String driver, final String url)

Description

Create a new JDBC Connection.

License

Open Source License

Parameter

Parameter Description
driver driver class name.
url driver specific url.

Exception

Parameter Description
ClassNotFoundException if driver class is not found.
IllegalAccessException if driver empty constructor is not public.
InstantiationException if an exception occurs while instantiating driver.
SQLException if an error occurs while making connection.

Return

Connection to database.

Declaration

public static Connection getConnection(final String driver, final String url)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.Connection;
import java.sql.DriverManager;

import java.sql.SQLException;

public class Main {
    /**//w  ww.jav  a 2 s.c  o  m
     * Create a new JDBC Connection.
     * 
     * @param driver
     *            driver class name.
     * @param url
     *            driver specific url.
     * @return Connection to database.
     * @throws ClassNotFoundException
     *             if driver class is not found.
     * @throws IllegalAccessException
     *             if driver empty constructor is not public.
     * @throws InstantiationException
     *             if an exception occurs while instantiating driver.
     * @throws SQLException
     *             if an error occurs while making connection.
     */
    public static Connection getConnection(final String driver, final String url)
            throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        // create driver class, which registers with DriverManager
        Class.forName(driver).newInstance();

        // request connection from DriverManager
        return DriverManager.getConnection(url);
    }
}

Related

  1. getConnection()
  2. getConnection()
  3. getConnection()
  4. getConnection()
  5. getConnection(@Nonnull String connectUrl)
  6. getConnection(final String driverName, final String driverUrl, final String userName, final String password)
  7. getConnection(final String url)
  8. getConnection(final String url, final String username, final String password, final String driverClass)
  9. getConnection(Properties pro)