[英]Error while connecting to Cloud SQL using jdbc driver
I have written a basic java code to connect to my Cloud SQL instance.我编写了一个基本的 java 代码来连接到我的 Cloud SQL 实例。 I have added the mysql jar connector in this project.
我在这个项目中添加了 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);
}
}
}
I am unable to connect and receiving this exception我无法连接并收到此异常
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Cannot load connection class because of underlying exception: 'java.lang.NumberFormatException: For input string: "us-central1:test-instance"'.
Any help is highly appreciated.任何帮助都受到高度赞赏。 Thanks!
谢谢!
Thanks nbk for your comment.感谢 nbk 的评论。 I was able to solve the issue by modifying the connection string and including this dependency in my maven project
我能够通过修改连接字符串并在我的 maven 项目中包含这个依赖项来解决这个问题
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory</artifactId>
<version>1.0.15</version>
</dependency>
Updated connection string更新的连接字符串
"jdbc:mysql:///testing?cloudSqlInstance=project-name:us-central1:test-instance&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=username&password=password"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.