无法通过Android Studio使用JDBC连接到Cloud SQL数据库 [英] Can't connect to Cloud SQL Database using JDBC through Android Studio

查看:89
本文介绍了无法通过Android Studio使用JDBC连接到Cloud SQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个正在运行的Cloud Sql实例.我一直在尝试通过Android Studio和Eclipse连接到它,但在两个地方都失败了.

I have a Cloud Sql instance created that is running. I have been trying for a bit now to connect to it through Android Studio and Eclipse and have failed in both places.

package com.example.testapplication3;

import java.sql.*;

public class ConnectToSql {

    public String run()
    {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            String instanceConnectionName = "TheActualInstanceName"; 
            String databaseName = "BudgetApp";

            String IP_of_instance = "35.******";
            String username = "root";
            String 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);

            Statement stat = connection.createStatement();
            ResultSet res = stat.executeQuery("Select * from users;");
            String result = "";
            while (res.next()) 
            {
                result += (res.getString(1) + " " + res.getString(2) + " " + res.getString(3));
            }

            connection.close();

            return result;
        }
        catch(Exception e)
        {
            System.out.println(e);
            return null;
        }

    }
}

我不断从中得到这个错误:

I am continuously getting this error from it:

java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on 35.*******:3,306.
    Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag.

我所做的:

  • 确保在实例连接"标签下将我的IP列入白名单
  • URL连接的格式不同
  • 通过ping确认服务器已启动
  • 仅通过Cloud Shell连接
  • 已确认端口在云端已打开

推荐答案

您应遵循 Cloud SQL jdbc套接字工厂已经在使用.

You should follow the guide of connecting to Cloud SQL from external applications. You should use the Cloud SQL jdbc socket factory, as I guess you are already using.

从您的代码中,我只能看到jdbc url看起来有些奇怪.尝试将其更改为这种确切格式,请:

From your code I can see just the jdbc url looks a bit strange. Try to change it into this exact format, please :

jdbc:mysql://google/<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>

您缺少"google"部分.希望这会有所帮助!如果这不起作用,那么值得尝试通过Cloud SQL代理进行连接,如您在我在此处附加的第一个链接中所见.

You are missing the "google" part. Hope this will help ! If this does not work it may worth trying to connect through the Cloud SQL proxy as you can see in the first link that I have attached here.

这篇关于无法通过Android Studio使用JDBC连接到Cloud SQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆