2
votes

Environment:

  1. Google cloud App Engine standard environment
  2. Google Cloud sql database instance (mysql)

Installation Steps:

  1. Java web application war file is deployed using App Engine Java SDK 1.9.62 using appcfg update command
  2. Jar files installed as part of the deployment on google cloud: mysql-connector-java-5.1.38.jar mysql-socket-factory-1.0.4.jar mysql-socket-factory-cor-1.0.2.jar jdbc-socket-factory-core-1.0.5.jar

JDBC connection steps using Java:

try {            
//DriverManager.registerDriver(com.mysql.jdbc.Driver);  
String DBUrl = String.format("jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory", 
databaseName,instanceConnectionName);          
Connection  DbConn = DriverManager.getConnection(DBUrl,DBUser,DBPwd);
} catch(SQLException  ex)   {
System.out.println("Database Connection Error."+ex.getMessage());
}

Steps for error message:

Open application using project-id.appspot.com

Following Error Message: 16-0 Could not create connection to database server.

Steps followed for Database Verification on local server:

JDBC connection steps using Java:

try {            
// -- Use cloud sql remote ip address say x.x.x.x  
String DBUrl = String.format("jdbc:mysql://x.x.x.x:3306/%s",databaseName);          
Connection  DbConn = DriverManager.getConnection(DBUrl,DBUser,DBPwd);
} catch(SQLException  ex)   {
System.out.println("Database Connection Error."+ex.getMessage());
}
  1. JDBC remote connection to Gloud Cloud mysql database using above steps
  2. Run java web application on Windows
  3. Application is working fine.

I spent two days and tried various steps, the same error is displayed. Any ideas how to resolve the issue, or where is the problem.

1
Thank you for this! How did you came up with the answer? Documentation never stated that the string should have this format. - Dionisis Karakasiliotis

1 Answers

2
votes

This problem is resolved after making following changes to Url connection string.

Code Changes:

try {            
Class.forName("com.mysql.jdbc.GoogleDriver");  
String DBUrl = String.format("jdbc:google:mysql://%s/%s", 
instanceConnectionName, databaseName);          
Connection  DbConn = DriverManager.getConnection(DBUrl,DBUser,DBPwd);
} catch(SQLException  ex)   {
System.out.println("Database Connection Error."+ex.getMessage());
}