Java JDBC MySQL Connection getMySQLConnection(String cloudSqlInstance, String dbName, String userName, String password)

Here you can find the source of getMySQLConnection(String cloudSqlInstance, String dbName, String userName, String password)

Description

get My SQL Connection

License

Open Source License

Declaration

public static Connection getMySQLConnection(String cloudSqlInstance, String dbName, String userName,
            String password) throws SQLException, ClassNotFoundException 

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 {
    public static Connection getMySQLConnection() throws ClassNotFoundException, SQLException {
        String cloudSqlInstance = "jblakz-hello-springboot:asia-east1:my-sql-instance-1";
        String dbName = "textdb";
        String userName = "root";
        String password = "1234";
        return getMySQLConnection(cloudSqlInstance, dbName, userName, password);
    }//  ww w  .  j av  a  2s.  c om

    public static Connection getMySQLConnection(String cloudSqlInstance, String dbName, String userName,
            String password) throws SQLException, ClassNotFoundException {
        hasDriver();
        String connectionURL = String.format("jdbc:mysql://google/%s?cloudSqlInstance=%s&"
                + "socketFactory=com.google.cloud.sql.mysql.SocketFactory", dbName, cloudSqlInstance);

        Connection conn = DriverManager.getConnection(connectionURL, userName, password);
        return conn;
    }

    protected static void hasDriver() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            throw new SQLException("Invalid Driver!!Please check this driver....");
        }
    }
}

Related

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