无法通过Android Studio使用JDBC连接到Cloud SQL数据库

编程入门 行业动态 更新时间:2024-10-25 20:28:45
本文介绍了无法通过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 .我猜您应该使用 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数据库

本文发布于:2023-10-23 23:05:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522263.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   数据库   Studio   Android   JDBC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!