简体   繁体   English

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

[英]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.

相关问题 使用JDBC驱动程序与Azure数据库连接时发生错误 - Error occurred while connecting with azure database using JDBC driver 使用 JDBC 连接到托管在云中的 MongoDB 时出错 - Error connecting to MongoDB hosted in the cloud, using JDBC 使用 JDBC 连接到 Athena 时找不到合适的驱动程序 - No suitable driver found for while connecting to Athena using JDBC 使用 jdbc 驱动程序连接到 MongoDB - Connecting to MongoDB using jdbc driver java.sql.SQLException: 使用 jsp 连接时找不到适合 jdbc:hive2://localhost:10000/default 的驱动程序 - java.sql.SQLException: No suitable driver found for jdbc:hive2://localhost:10000/default while connecting using jsp 使用JDBC连接到SQL Server时出现jcifs / smb / NtlmPasswordAuthentication错误 - jcifs/smb/NtlmPasswordAuthentication error while connecting to SQL Server with JDBC 使用net.sourceforge.jtds.jdbc.Driver将SpringMVC与JDBC连接时出错 - Error in connecting SpringMVC with JDBC using net.sourceforge.jtds.jdbc.Driver 使用JDBC连接到Azure SQL数据仓库时出现SQLException - SQLException while connecting to Azure SQL Data Warehouse using JDBC 连接到SQLServer时发生JDBC错误 - JDBC error while connecting to SQLServer 使用JDBC驱动程序连接到Sybase时发生异常 - Exception when connecting to Sybase using JDBC Driver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM