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(final String driverName, final String driverUrl, final String userName, final String password)
Gets the connection.
Class.forName(driverName);
return DriverManager.getConnection(driverUrl, userName, password);
ConnectiongetConnection(final String url)
get Connection
try {
    return DriverManager.getConnection(url);
} catch (SQLException e) {
    return null;
ConnectiongetConnection(final String url, final String username, final String password, final String driverClass)
get Connection
Class.forName(driverClass);
return DriverManager.getConnection(url, username, password);
ConnectiongetConnection(Properties pro)
get Connection
Class.forName(pro.getProperty("jdbc.driver"));
return DriverManager.getConnection(pro.getProperty("jdbc.url"), pro.getProperty("jdbc.username"),
        pro.getProperty("jdbc.password"));
ConnectiongetConnection(Properties prop)
get Connection
Connection conn = null;
try {
    if (conn == null) {
        conn = DriverManager.getConnection("jdbc:phoenix:yahoo005.nicl.cs.duke.edu:2181", prop);
        ResultSet rs = conn.prepareStatement("select count(*) from nation").executeQuery();
        while (rs.next()) {
            System.out.println("row count " + rs.getInt(1));
        System.out.println("Connection established successfully");
} catch (SQLException e) {
    System.err.println("Error in getting zookeeper connection");
    e.printStackTrace();
return conn;
ConnectiongetConnection(Properties properties)
get Connection
Class.forName((String) properties.get(DRIVER_NAME));
return DriverManager.getConnection((String) properties.get(JDBC_URL), properties);
ConnectiongetConnection(String dbName)
get Connection
try {
    Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
try {
    return DriverManager.getConnection(PROTOCOL + dbName + ";create=true");
} catch (SQLException e) {
...
ConnectiongetConnection(String dbUrl, String dbUser, String dbPassword)
get Connection
try {
    final Connection conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
    conn.setAutoCommit(true);
    return conn;
} catch (Exception e) {
    throw new RuntimeException("Cannot get connection", e);
ConnectiongetConnection(String driver, String connectionString, String userName, String password)
get Connection
Connection con = null;
try {
    Class.forName(driver);
    con = DriverManager.getConnection(connectionString, userName, password);
} catch (Exception e) {
    e.printStackTrace();
return con;
...
ConnectiongetConnection(String driver, String connectString)
get Connection
Connection conn = null;
try {
    if (!_driverMap.containsKey(driver))
        _driverMap.put(driver, (Driver) Class.forName(driver).newInstance());
    conn = DriverManager.getConnection(connectString);
} catch (SQLException ex) {
    System.out.println(String.format("driver: %s\nconnectString: %s", driver, connectString));
    throw new RemoteException("Unable to connect to SQL database", ex);
...