无法使用 JDBC 通过 Android Studio 连接到云 SQL 数据库
Can't connect to Cloud SQL Database using JDBC through Android Studio
我创建了一个 运行 的 Cloud Sql 实例。我现在一直在尝试通过 Android Studio 和 Eclipse 连接到它,但在这两个地方都失败了。
package com.example.testapplication3;
import java.sql.*;
public class ConnectToSql {
public String run()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String instanceConnectionName = "TheActualInstanceName";
String databaseName = "BudgetApp";
String IP_of_instance = "35.******";
String username = "root";
String password = "********";
String jdbcUrl = String.format("jdbc:mysql://%s/%s?cloudSqlInstance=%s" +
"&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
IP_of_instance, databaseName, instanceConnectionName);
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
Statement stat = connection.createStatement();
ResultSet res = stat.executeQuery("Select * from users;");
String result = "";
while (res.next())
{
result += (res.getString(1) + " " + res.getString(2) + " " + res.getString(3));
}
connection.close();
return result;
}
catch(Exception e)
{
System.out.println(e);
return null;
}
}
}
我不断收到此错误消息:
java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on 35.*******: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.
我做了什么:
- 确保我的 IP 在“实例连接”选项卡下列入白名单
- url 连接的不同格式
- 通过 ping 确保服务器已启动
- 仅通过云连接Shell
- 已确认云端端口开放
您应该遵循 connecting to Cloud SQL from external applications. You should use the Cloud SQL jdbc socket factory 的指南,因为我猜您已经在使用了。
从您的代码中我可以看出 jdbc url 看起来有点奇怪。请尝试将其更改为完全相同的格式:
jdbc:mysql://google/<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>
您缺少 "google" 部分。希望这会有所帮助!如果这不起作用,可能值得尝试通过云 SQL 代理进行连接,正如您在我在此处附加的第一个 link 中看到的那样。
我创建了一个 运行 的 Cloud Sql 实例。我现在一直在尝试通过 Android Studio 和 Eclipse 连接到它,但在这两个地方都失败了。
package com.example.testapplication3;
import java.sql.*;
public class ConnectToSql {
public String run()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String instanceConnectionName = "TheActualInstanceName";
String databaseName = "BudgetApp";
String IP_of_instance = "35.******";
String username = "root";
String password = "********";
String jdbcUrl = String.format("jdbc:mysql://%s/%s?cloudSqlInstance=%s" +
"&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
IP_of_instance, databaseName, instanceConnectionName);
Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
Statement stat = connection.createStatement();
ResultSet res = stat.executeQuery("Select * from users;");
String result = "";
while (res.next())
{
result += (res.getString(1) + " " + res.getString(2) + " " + res.getString(3));
}
connection.close();
return result;
}
catch(Exception e)
{
System.out.println(e);
return null;
}
}
}
我不断收到此错误消息:
java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on 35.*******: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.
我做了什么:
- 确保我的 IP 在“实例连接”选项卡下列入白名单
- url 连接的不同格式
- 通过 ping 确保服务器已启动
- 仅通过云连接Shell
- 已确认云端端口开放
您应该遵循 connecting to Cloud SQL from external applications. You should use the Cloud SQL jdbc socket factory 的指南,因为我猜您已经在使用了。
从您的代码中我可以看出 jdbc url 看起来有点奇怪。请尝试将其更改为完全相同的格式:
jdbc:mysql://google/<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>
您缺少 "google" 部分。希望这会有所帮助!如果这不起作用,可能值得尝试通过云 SQL 代理进行连接,正如您在我在此处附加的第一个 link 中看到的那样。