Java Utililty Methods JDBC MySQL Connection

List of utility methods to do JDBC MySQL Connection

Description

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

Method

ConnectiongetConnection(String ip, String dataBaseName, String password, String username)
get Connection
try {
    Class.forName(driver);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
String urlStr = url + ip + "/" + dataBaseName + "?useUnicode=true&characterEncoding=UTF-8";
try {
    con = DriverManager.getConnection(urlStr, username, password);
...
ConnectiongetConnection(String ip, String db, String user, String passWord)
get Connection
Connection conn = null;
try {
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://" + ip + "/" + db;
    conn = (Connection) DriverManager.getConnection(url, user, passWord);
} catch (Exception e) {
    e.printStackTrace();
return conn;
ConnectiongetConnetion(String url, String username, String password)
get Connetion
final String driverName = "com.mysql.jdbc.Driver";
Connection connection = null;
try {
    Class.forName(driverName);
    connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
    e.printStackTrace();
} catch (SQLException e) {
...
intgetcount()
getcount
PreparedStatement statement = null;
ResultSet rs = null;
Connection conn = getConnection();
try {
    String sql = "SELECT count(1) from user ";
    statement = conn.prepareStatement(sql);
    rs = statement.executeQuery();
    int count = 0;
...
ConnectiongetDBConn()
get DB Conn
return DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
ConnectiongetDbConnection()
Gets a connection to the database.
Class.forName("com.mysql.jdbc.Driver");
Connection connect = DriverManager.getConnection(
        "jdbc:mysql://" + DB_HOST + "/" + DB_NAME + "?" + "user=" + DB_USER + "&password=" + DB_PASSWORD);
return connect;
ConnectiongetDBConnection()
get DB Connection
Connection dbConnection = null;
try {
    Class.forName(JDBC_DRIVER);
} catch (ClassNotFoundException e) {
    System.out.println(e.getMessage());
try {
    dbConnection = DriverManager.getConnection(DB_URL, USER, PASS);
...
ConnectiongetDBConnection(String database)
get DB Connection
Connection connection = null;
try {
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection(connectionString(database));
} catch (ClassNotFoundException | SQLException ex) {
    ex.printStackTrace();
return connection;
...
ConnectiongetDBConnection(String dbServer, String dbName, String dbUser, String dbPass)
Establishes a connection to a database.
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection(
        "jdbc:mysql://" + dbServer + "/" + dbName + "?useUnicode=true&characterEncoding=utf-8", dbUser,
        dbPass);
ConnectiongetDBConnection(String urlFormat, String host, Integer port, String database, String user, String password)
get DB Connection
String url;
if (host.startsWith("jdbc:mysql://")) {
    url = host;
} else {
    url = String.format(urlFormat, host, port, database);
return DriverManager.getConnection(url, user, password);