Java JDBC MySQL Connection getMySQLConnection(Properties props)

Here you can find the source of getMySQLConnection(Properties props)

Description

get My SQL Connection

License

Open Source License

Declaration

public static Connection getMySQLConnection(Properties props)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException 

Method Source Code

//package com.java2s;
/**//from   w w w.j a v  a2 s.  com
 * Project: bee-engine
 * 
 * File Created at 2012-9-11
 * 
 * Copyright 2012 dianping.com.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Dianping Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with dianping.com.
 */

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

import java.sql.SQLException;
import java.util.Properties;

public class Main {
    public static Connection getMySQLConnection(Properties props)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        if (props == null) {
            props = new Properties();
        }

        String url = props.getProperty("url") == null ? "jdbc:mysql://localhost:3306/" : props.getProperty("url");
        String db = props.getProperty("db") == null ? "cdcol" : props.getProperty("db");
        String driver = "com.mysql.jdbc.Driver";

        if (props.getProperty("user") == null) {
            props.setProperty("user", "root");
        }
        if (props.getProperty("password") == null) {
            props.setProperty("password", "");
        }
        if (props.getProperty("useServerPrepStmts") == null) {
            props.setProperty("useServerPrepStmts", "true");
        }
        if (props.getProperty("useUnicode") == null) {
            props.setProperty("useUnicode", "true");
        }

        Class.forName(driver).newInstance();
        DriverManager.setLoginTimeout(600);
        Connection conn = DriverManager.getConnection(url + db, props);
        return conn;
    }
}

Related

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