【问题标题】:Connecting to Google cloud sql from Java stand alone application从 Java 独立应用程序连接到 Google Cloud sql
【发布时间】:2015-02-25 19:01:27
【问题描述】:

我想编写一个 java 独立应用程序来连接到 google cloud sql - MySql 数据库。我可以找到应用程序客户端的示例,但不能找到独立的 Java 应用程序。

当我尝试这样做时,我收到以下错误。

Class.forName("com.mysql.jdbc.Driver");

Connection con = DriverManager.getConnection("jdbc:google:mysql://IP:Instance_name?user=user_name");
Statement st = con.createStatement();
ResultSet rs = con.createStatement().executeQuery("SELECT 1 + 1");

失败并出现错误:

找不到适合jdbc:google:mysql:..的驱动程序

我看到一些他们要求启用 mysql 连接器的答案。但应用引擎项目就是这种情况。如何使用独立应用程序做到这一点?

【问题讨论】:

  • 您能否分享您的代码,我也在尝试从独立 java 程序连接到 Cloud SQL 2nd Gen,但它无法正常工作,您的代码肯定会帮助我们,谢谢。

标签: google-api google-cloud-sql


【解决方案1】:

您可以只使用标准的mysql 连接器连接到云sql 实例:例如:

DriverManager.getConnection("jdbc:mysql://IP:Instance_name?user=user_name");

您可以查看此链接https://cloud.google.com/sql/docs/external 了解更多信息。

【讨论】:

  • 感谢您的帮助。我错过了添加我的 IP 地址以进行身份​​验证。现在可以了。
  • 数据库名称在这个字符串中的位置?
【解决方案2】:

我们可以简单地使用标准的 mysql java 连接器来连接云 sql,形成一个独立的 java 应用程序。

按照下面的代码 sn-p。这应该可以帮助您无缝连接到数据库。

class.forName(com.mysql.jdbc.Driver);
String connectionUrl = "jdbc:mysql//<public-ip>/<database>?useSSL=false";
Connection conn = DriverManager.getConnection(connectionUrl, user, password);

在您执行此操作之前,请确保您从 Internet 获取您的 IP 地址并在路径下添加相同的地址:google cloud -> sql -> sqlinstance -> 连接 -> 网络 -> 授权网络。

Click here to see the google cloud mysql configuration

【讨论】:

    【解决方案3】:

    在运行代码之前在 内部编辑

    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.SQLNonTransientConnectionException;
    import java.sql.Statement;
    
    /**
    * A sample app that connects to a Cloud SQL instance and lists all available tables 
    in a database.
    */
    
    public class Cloud_sql {
    public static void main(String[] args) throws SQLNonTransientConnectionException 
    ,IOException, SQLException {
    
    String instanceConnectionName = <Your instanceConnectionName>;
    String databaseName = <Database name from which u want to list tables>;
    
    
    String IP_of_instance = <IP address of the instance>;
    String username = <mysql username>;
    String password = <mysql 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);
    
    try (Statement statement = connection.createStatement()) {
      ResultSet resultSet = statement.executeQuery("SHOW TABLES");
      while (resultSet.next()) {
        System.out.println(resultSet.getString(1));
      }
    }catch(Exception e){
      e.printStackTrace();
    }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-05
      • 2023-02-07
      • 2020-07-08
      • 2019-05-10
      • 2013-09-22
      相关资源
      最近更新 更多