Java JDBC MySQL Connection getMysqlConnection(final String url)

Here you can find the source of getMysqlConnection(final String url)

Description

Get a mysql connection from a URL.

License

Open Source License

Parameter

Parameter Description
url a Mysql URL.

Return

a Connection to a Mysql database.

Declaration

public static Connection getMysqlConnection(final String url)
        throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.sql.SQLException;

public class Main {
    /** Mysql Driver. */
    public static final String MYSQL_DRIVER_CLASSNAME = "com.mysql.jdbc.Driver";

    /**//from  www . ja va 2  s . co m
     * Get a mysql connection from a URL.
     * 
     * Calls getConnection(MYSQL_DRIVER_CLASSNAME, url).
     * 
     * @param url
     *            a Mysql URL.
     * @return a Connection to a Mysql database.
     */
    public static Connection getMysqlConnection(final String url)
            throws SQLException, ClassNotFoundException, IllegalAccessException, InstantiationException {
        return getConnection(MYSQL_DRIVER_CLASSNAME, url);
    }

    /**
     * Create a new JDBC Connection.
     * 
     * @param driver
     *            driver class name.
     * @param url
     *            driver specific url.
     * @return Connection to database.
     * @throws ClassNotFoundException
     *             if driver class is not found.
     * @throws IllegalAccessException
     *             if driver empty constructor is not public.
     * @throws InstantiationException
     *             if an exception occurs while instantiating driver.
     * @throws SQLException
     *             if an error occurs while making connection.
     */
    public static Connection getConnection(final String driver, final String url)
            throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException {
        // create driver class, which registers with DriverManager
        Class.forName(driver).newInstance();

        // request connection from DriverManager
        return DriverManager.getConnection(url);
    }
}

Related

  1. getIonMassList()
  2. getJdbcConnection(String host, int port, String name, String user, String password)
  3. getJtdsConnection()
  4. getMaxID()
  5. getMysqlCon(String ip, String port, String dbname, String username, String password)
  6. getMySQLConnection(Properties props)
  7. getMySQLConnection(String cloudSqlInstance, String dbName, String userName, String password)
  8. getMySQLConnection(String hostname, String databaseName, String dbuser, String dbpasswort)
  9. getMySQLConnection(String hostName, String dbName, String userName, String password)