I am trying to connect this small Java program to my MySQL Database instance on GCP for educational purposes.
This code works well with the instance I have locally on localhost and port 3306. However, this is not very practical or useful.
Scenarios that work:
- Connecting Java client program to MySQL database served locally.
- Connecting MySQL Workbench client to MySQL database served locally.
- Connecting MySQL Workbench client to MySQL database served on Google Cloud Platform.
My objective:
- Connect very simple Java client program to MySQL database served on Google Cloud Platform.
Jar files used:
- mysql-connector-java-8.0.29.jar
- mysql-socket-factory-1.6.1-jar-with-dependencies.jar
IDE used:
- Eclipse
The code:
package test;
import java.sql.*;
public class Test
{
public static void main(String[] args)
{
String CREDENTIALS = "jdbc:mysql://google/emexdb?cloudSqlInstance=ethereal-shine-354205:us-central1:emexdb-mysql&socketFactory=google.cloud.sql.mysql.SocketFactory&useSSL=false&user=****&password=***********";
try
{
//Get a connection to database
Connection myConn = DriverManager.getConnection(CREDENTIALS);
//Create a statement
Statement myStmt = myConn.createStatement();
//Execute SQL query
ResultSet myRs = myStmt.executeQuery("select * from Test;");
//Process the result set
while(myRs.next())
{
System.out.println(myRs.getNString("id")+" "+myRs.getString("_name"));
}
myConn.close();
}
catch(Exception eSQL)
{
eSQL.printStackTrace();
}
}
}
This is the error message Eclipse is giving me:
java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on google:3,306.
Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:462)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:241)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:253)
at test.Test.main(Test.java:12)
Caused by: java.lang.NullPointerException: Cannot invoke "com.mysql.cj.protocol.a.NativeProtocol.getSocketConnection()" because the return value of "com.mysql.cj.NativeSession.getProtocol()" is null
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:972)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:818)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:448)
... 5 more
Any help would be much appreciated. Thank you!