Java Utililty Methods JDBC Connection Create

List of utility methods to do JDBC Connection Create

Description

The list of methods to do JDBC Connection Create are organized into topic(s).

Method

ConnectiongetConnection(String driver, String url, String user, String pass)
get Connection
Class<?> c = Class.forName(driver);
Driver d = (Driver) c.newInstance();
DriverManager.registerDriver(d);
return DriverManager.getConnection(url, user, pass);
ConnectiongetConnection(String driver, String url, String username, String password)
get Connection
Connection conn = null;
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);
return conn;
ConnectiongetConnection(String driverClass, String url, String user, String password)
Opens a connection to the specified database using the JDBC driver class , the url, the user name, and the password provided.
Class.forName(driverClass).newInstance();
return DriverManager.getConnection(url, user, password);
ConnectiongetConnection(String driverName, String urlprefix, String host, String port, String database, String user, String password)
Creates a database connection specific to the input arguments
Class.forName(driverName);
String url = urlprefix + host + ":" + port + "/" + database;
return DriverManager.getConnection(url, user, password);
ConnectiongetConnection(String drvr, String url, String nm, String pass)
get Connection
Class.forName(drvr);
return DriverManager.getConnection(url, nm, pass);
ConnectiongetConnection(String filename)
A helper method to get the global default RDBMS connection.
return getDerbyConnection(filename);
ConnectiongetConnection(String jdbcDriverName, String connectionURL)

Look up an open connection.

Connection conn = _openConnections.get(connectionURL);
if (conn == null) {
    try {
        Class.forName(jdbcDriverName);
    } catch (ClassNotFoundException e) {
        throw new SQLException("Could not find " + jdbcDriverName + " on the classpath.");
    try {
...
ConnectiongetConnection(String propertiesFilePath)
Reads a properties file from the classpath and instantiates a java.sql.Connection with the given parameters.
Properties properties = new Properties();
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertiesFilePath);
try {
    properties.load(is);
} catch (IOException e) {
    throw new RuntimeException("Could not load " + propertiesFilePath + " from the classpath");
String driver = properties.getProperty("driver");
...
ConnectiongetConnection(String subprotocol, String subname, String user, String password)
get Connection
Connection conn = null;
String conDetails = "jdbc:" + subprotocol + ":" + subname + "?user=" + user + "&password=" + password;
try {
    conn = DriverManager.getConnection(conDetails);
} catch (Exception e) {
return conn;
ConnectiongetConnection(String url, String username, String password)
get Connection
if (null == conn) {
    try {
        conn = DriverManager.getConnection(url, username, password);
    } catch (SQLException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
return conn;