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

voidcanConnect(Properties dbSettings)
Returns whether is possible to connect to the database server for a given connection settings.
String url = dbSettings.getProperty("hibernate.connection.url");
String username = dbSettings.getProperty("hibernate.connection.username");
String password = dbSettings.getProperty("hibernate.connection.password");
Connection conn = DriverManager.getConnection(url, username, password);
conn.close();
voidclearTable(String tableName, String driver, String url, String user, String password)
clear Table
Connection conn = null;
Statement stmt = null;
try {
    Class.forName(driver);
    conn = DriverManager.getConnection(url, user, password);
    stmt = conn.createStatement();
    String sql = "DELETE FROM " + tableName;
    stmt.executeUpdate(sql);
...
ConnectioncreateConnection()
Method to return the connection
try {
    Class.forName(driver);
    connection = DriverManager.getConnection(connectionURL, user, password);
} catch (ClassNotFoundException exception) {
    System.out.println("Driver not found");
} catch (SQLException exception) {
    System.out.println("Connection exception" + exception);
return connection;
ConnectioncreateConnection(Properties props, String prefix)
Create connection based on properties:
- driverClassName
- connectionUrl
- userName
- password
Connection con;
if (prefix == null) {
    prefix = "";
String driverClassName = props.getProperty(prefix + "driverClassName");
String connectionUrl = props.getProperty(prefix + "connectionUrl");
String userName = props.getProperty(prefix + "userName");
String password = props.getProperty(prefix + "password");
...
voidcreateDatabase(String dbConnectionString, String dbName, String user, String pass, String driver)
create Database
Connection conn = null;
Statement stmt = null;
try {
    Class.forName(driver);
    System.out.println("Connecting to database...");
    conn = DriverManager.getConnection(dbConnectionString, user, pass);
    System.out.println("Creating database...");
    stmt = conn.createStatement();
...
ConnectioncreateDbConnection(String dbUrl, String dbUser, String dbPass, String dbDriverClass)
create Db Connection
try {
    Class.forName(dbDriverClass).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
    throw new RuntimeException(e);
return DriverManager.getConnection(dbUrl, dbUser, dbPass);
ConnectiongetAccessConn(String path)
get Access Conn
try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String url = "jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=" + path;
    return DriverManager.getConnection(url, "", "");
} catch (ClassNotFoundException ex) {
    ex.printStackTrace();
} catch (SQLException ex) {
    ex.printStackTrace();
...
ConnectiongetConnect(String driver, String url, String user, String password)
get Connect
Class.forName(driver);
return DriverManager.getConnection(url, user, password);
ConnectiongetConnection()
get Connection
ResourceBundle rb = ResourceBundle.getBundle("db");
Class.forName(rb.getString("drivername"));
Connection con = DriverManager.getConnection(rb.getString("dburl"), rb.getString("userid"),
        rb.getString("password"));
return con;
ConnectiongetConnection()
get Connection
String url;
String address = System.getProperty("de.njsm.stocks.internal.db.databaseAddress");
String port = System.getProperty("de.njsm.stocks.internal.db.databasePort");
String name = System.getProperty("de.njsm.stocks.internal.db.databaseName");
String username = System.getProperty("de.njsm.stocks.internal.db.databaseUsername");
String password = System.getProperty("de.njsm.stocks.internal.db.databasePassword");
url = String.format("jdbc:mariadb://%s:%s/%s", address, port, name);
return DriverManager.getConnection(url, username, password);
...