使用jdbc驱动程序连接到云SQL时出错



我已经编写了一个基本的java代码来连接到我的Cloud SQL实例。我在这个项目中添加了mysql-jar连接器。


import java.sql.*;
class MysqlCon {
public static void main(String args[]) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://project-name:us-central1:test-instance", "username", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from testing");
while (rs.next())
System.out.println(rs.toString());
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

我无法连接并接收此异常

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "us-central1:test-instance"'.

非常感谢您的帮助。谢谢

感谢nbk的评论。我能够通过修改连接字符串并将此依赖项包含在我的maven项目中来解决这个问题

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

更新的连接字符串

"jdbc:mysql:///testing?cloudSqlInstance=project-name:us-central1:test-instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=username&password=password"

相关内容

  • 没有找到相关文章

最新更新