Java JDBC Connection Create getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password)

Here you can find the source of getConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password)

Description

Creates a database connection specific to the input arguments

License

Open Source License

Parameter

Parameter Description
driverName a parameter
urlprefix a parameter
host a parameter
port a parameter
database a parameter
user a parameter
password a parameter

Exception

Parameter Description
ClassNotFoundException an exception
SQLException an exception

Declaration

public static Connection getConnection(String driverName, String urlprefix, String host, String port,
        String database, String user, String password) throws ClassNotFoundException, SQLException 

Method Source Code


//package com.java2s;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class Main {
    /**/*from  w  ww  .jav a 2s .  c o  m*/
     * JDBC driver name
     */
    public static final String DRIVER_NAME = "DRIVER_NAME";
    /**
     * URL prefix for JDBC access
     */
    public static final String URL_PREFIX = "URL_PREFIX";
    /**
     * Host computer for the database
     */
    public static final String HOST = "HOST";
    /**
     * Port where database access is available
     */
    public static final String PORT = "PORT";
    /**
     * Name of the database
     */
    public static final String DATABASE = "DATABASE";
    /**
     * User name
     */
    public static final String USER = "USER";
    /**
     * Password
     */
    public static final String PASSWORD = "PASSWORD";

    /**
     * Returns a database connection given the specified properties
     * 
     * @param properties
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection(Properties properties) throws ClassNotFoundException, SQLException {
        String driverName = properties.getProperty(DRIVER_NAME);
        String urlprefix = properties.getProperty(URL_PREFIX);
        String host = properties.getProperty(HOST);
        String port = properties.getProperty(PORT);
        String database = properties.getProperty(DATABASE);
        String user = properties.getProperty(USER);
        String password = properties.getProperty(PASSWORD);

        if (!(urlprefix.endsWith("://") || urlprefix.endsWith(":@//"))) {
            throw new RuntimeException("URL prefix must end with ://");
        }
        return getConnection(driverName, urlprefix, host, port, database, user, password);

    }

    /**
     * Creates a database connection specific to the input arguments
     * 
     * @param driverName
     * @param urlprefix
     * @param host
     * @param port
     * @param database
     * @param user
     * @param password
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection(String driverName, String urlprefix, String host, String port,
            String database, String user, String password) throws ClassNotFoundException, SQLException {
        Class.forName(driverName);
        String url = urlprefix + host + ":" + port + "/" + database;
        return DriverManager.getConnection(url, user, password);
    }
}

Related

  1. getConnection(String driver, String connectionString, String userName, String password)
  2. getConnection(String driver, String connectString)
  3. getConnection(String driver, String url, String user, String pass)
  4. getConnection(String driver, String url, String username, String password)
  5. getConnection(String driverClass, String url, String user, String password)
  6. getConnection(String drvr, String url, String nm, String pass)
  7. getConnection(String filename)
  8. getConnection(String jdbcDriverName, String connectionURL)
  9. getConnection(String propertiesFilePath)