Can't connect to Cloud SQL Database using JDBC through Android Studio

百般思念 提交于 2021-01-29 11:50:24

问题


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.

What I've done:

  • Made sure my IP is whitelisted under the Instance Connections tab
  • Different formatting for the url connection
  • Made sure the server is up by pinging
  • Connected by Cloud Shell ONLY
  • Confirmed port is open on Cloud end

回答1:


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.

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>

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.



来源:https://stackoverflow.com/questions/58495312/cant-connect-to-cloud-sql-database-using-jdbc-through-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!