Java JDBC MySQL Connection getConnection(String dbUrl, String dbName, String userName, String password)

Here you can find the source of getConnection(String dbUrl, String dbName, String userName, String password)

Description

create a DB connection to mysql

License

Apache License

Parameter

Parameter Description
dbUrl a parameter
dbName a parameter
userName a parameter
password a parameter

Declaration

public static Connection getConnection(String dbUrl, String dbName, String userName, String password) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Main {
    private static Connection conn = null;
    private static int port = 3306;

    /**/*from   w ww. j  a  va 2 s .c  om*/
     * create a DB connection to mysql
     * @param dbUrl
     * @param dbName
     * @param userName
     * @param password
     * @return
     */
    public static Connection getConnection(String dbUrl, String dbName, String userName, String password) {

        if (conn == null) {
            try {
                String connString = "jdbc:mysql://" + dbUrl + ":" + port + "/" + dbName + "?autoReconnect=true";
                conn = DriverManager.getConnection(connString, userName, password);
            } catch (SQLException ex) {
                // handle any errors
                System.out.println("SQLException: " + ex.getMessage());
                System.out.println("SQLState: " + ex.getSQLState());
                System.out.println("VendorError: " + ex.getErrorCode());
            }
        }

        return conn;
    }
}

Related

  1. getConnection()
  2. getConnection()
  3. getConnection(final String host, final int port, final String schema, final String user, final String password)
  4. getConnection(String connectionType)
  5. getConnection(String dbFile)
  6. getConnection(String ip, String dataBaseName, String password, String username)
  7. getConnection(String ip, String db, String user, String passWord)
  8. getConnetion(String url, String username, String password)
  9. getcount()