gpt4 book ai didi

java - 找不到适合 jdbc :mysql://google/mysql? cloudSqlInstance 的驱动程序

转载 作者:行者123 更新时间:2023-11-29 07:35:56 25 4
gpt4 key购买 nike

我知道这是一个常见问题,我在 StackOverflow 上搜索了很多,大部分建议是阅读 connect to google cloud via jdbc .我按照示例代码进行操作,但仍然找不到适合 jdbc 的驱动程序。

package CSCI585HW2;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
* A sample app that connects to a Cloud SQL instance and lists all available tables in a database.
*/
public class ConnectGoogleCloud {
public static void main(String[] args) throws IOException, SQLException {
String instanceConnectionName = "csci585hw2-195403:us-central1:sql-db-1";

// The database from which to list tables.
String databaseName = "test";

String username = "zongtb";

// TODO: fill this in
// This is the password that was set via the Cloud Console or empty if never set
// (not recommended).
String password = "123ewq";

//[START doc-example]
String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s&"
+ "socketFactory=com.google.cloud.sql.mysql.SocketFactory",
databaseName,
instanceConnectionName);

Connection connection = DriverManager.getConnection(jdbcUrl, username, password);
//[END doc-example]

try (Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("SHOW TABLES");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}
}
}

ConnectGoogleCould是项目中唯一的类,除了Java 1.8我没有使用任何外部库。

我可以使用 MySQL Workbench 连接到 Google Cloud MySQL 实例,所以我认为这不是身份验证问题。

哪一步错了?任何建议都会有所帮助,在此先感谢您!

最佳答案

手动添加此库或添加到您的 POM:

<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.5</version>
</dependency>

对我来说,这解决了连接找不到合适的驱动程序的问题。

关于java - 找不到适合 jdbc :mysql://google/mysql? cloudSqlInstance 的驱动程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48883324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com