Here you can find the source of getMySQLConnection(String cloudSqlInstance, String dbName, String userName, String password)
public static Connection getMySQLConnection(String cloudSqlInstance, String dbName, String userName, String password) throws SQLException, ClassNotFoundException
//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...."); } } }