Java JDBC MySQL Connection getMySQLConnection(String hostname, String databaseName, String dbuser, String dbpasswort)

Here you can find the source of getMySQLConnection(String hostname, String databaseName, String dbuser, String dbpasswort)

Description

Gets the my sql connection.

License

Apache License

Parameter

Parameter Description
hostname the hostname
databaseName the database name
dbuser the dbuser
dbpasswort the dbpasswort

Exception

Parameter Description
ClassNotFoundException the class not found exception
SQLException the sQL exception

Return

the my sql connection

Declaration

public static Connection getMySQLConnection(String hostname, String databaseName, String dbuser,
        String dbpasswort) throws ClassNotFoundException, SQLException 

Method Source Code

//package com.java2s;
/**/*from  ww  w  .j a v  a2  s.  co  m*/
 * Copyright (C) 2007 Asterios Raptis
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

public class Main {
    /** Constant for the drivername from MySQL-database. */
    public static final String MYSQL_DRIVERNAME = "com.mysql.jdbc.Driver";
    /** Constant for the urlprefix from MySQL-database. */
    public static final String MYSQL_PREFIX_URL = "jdbc:mysql://";
    /** Constant for the default port where the MySQL-database listen. */
    public static final int MYSQL_PORT = 3306;

    /**
     * Gets the my sql connection.
     * 
     * @param hostname
     *            the hostname
     * @param databaseName
     *            the database name
     * @param dbuser
     *            the dbuser
     * @param dbpasswort
     *            the dbpasswort
     * @return the my sql connection
     * @throws ClassNotFoundException
     *             the class not found exception
     * @throws SQLException
     *             the sQL exception
     */
    public static Connection getMySQLConnection(String hostname, String databaseName, String dbuser,
            String dbpasswort) throws ClassNotFoundException, SQLException {
        return getMySQLConnection(hostname, MYSQL_PORT, databaseName, dbuser, dbpasswort);
    }

    /**
     * Gets the my sql connection.
     * 
     * @param hostname
     *            the hostname
     * @param portNumber
     *            the port number
     * @param databaseName
     *            the database name
     * @param dbuser
     *            the dbuser
     * @param dbpasswort
     *            the dbpasswort
     * @return the my sql connection
     * @throws ClassNotFoundException
     *             the class not found exception
     * @throws SQLException
     *             the sQL exception
     */
    public static Connection getMySQLConnection(String hostname, int portNumber, String databaseName, String dbuser,
            String dbpasswort) throws ClassNotFoundException, SQLException {
        String url = MYSQL_PREFIX_URL + hostname + ":" + portNumber + "/" + databaseName;
        Class.forName(MYSQL_DRIVERNAME);
        return DriverManager.getConnection(url, dbuser, dbpasswort);
    }
}

Related

  1. getMaxID()
  2. getMysqlCon(String ip, String port, String dbname, String username, String password)
  3. getMysqlConnection(final String url)
  4. getMySQLConnection(Properties props)
  5. getMySQLConnection(String cloudSqlInstance, String dbName, String userName, String password)
  6. getMySQLConnection(String hostName, String dbName, String userName, String password)
  7. getMysqlIndices(String conString)
  8. getNewDatabaseConnection(String dbUser, String dbPassword)
  9. getRssminerDB()