java.net.ConnectException [英] java.net.ConnectException

查看:55
本文介绍了java.net.ConnectException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于这个主题的帖子很少,但我仍然无法弄清楚出了什么问题.代码如下:

I've seen few threads about this topic but I still can't figure out what's wrong. Following is the code:

import java.sql.*;

public class SQL
{
    public static void main(String[] args) 
    {
        Connection conn = null;
        String url = "jdbc:mysql://mysql1.oyo.co.il:3306/";
        String dbName = "sdarot2_winner";
        String driver = "com.mysql.jdbc.Driver";
        String userName = ""; 
        String password = "";
        String table = "LEADER_CAM_PRODUCTS";

        try {
            Class.forName(driver).newInstance();
            conn = DriverManager.getConnection(url+dbName,userName,password);
            System.out.println("Connected to the database");
            conn.close();
            System.out.println("Disconnected from database");

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

这是我得到的错误:

com.mysql.jdbc.CommunicationsException:由于底层 >> 异常导致通信链接失败:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying > > exception:

** 开始嵌套异常**

** BEGIN NESTED EXCEPTION **

java.net.ConnectException消息:连接超时:连接

java.net.ConnectException MESSAGE: Connection timed out: connect

堆栈跟踪:

java.net.ConnectException:连接超时:连接在 java.net.PlainSocketImpl.socketConnect(Native Method)在 java.net.PlainSocketImpl.doConnect(来源不明)在 java.net.PlainSocketImpl.connectToAddress(Unknown Source)在 java.net.PlainSocketImpl.connect(Unknown Source)在 java.net.SocksSocketImpl.connect(来源不明)在 java.net.Socket.connect(来源不明)在 java.net.Socket.connect(来源不明)在 java.net.Socket.(来源不明)在 java.net.Socket.(来源不明)在 com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)在 com.mysql.jdbc.MysqlIO.(MysqlIO.java:271)在 com.mysql.jdbc.Connection.createNewIO(Connection.java:2744)在 com.mysql.jdbc.Connection.(Connection.java:1553)在 com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)在 java.sql.DriverManager.getConnection(Unknown Source)在 java.sql.DriverManager.getConnection(Unknown Source)在 SQL.main(SQL.java:17)

java.net.ConnectException: Connection timed out: connect at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(Unknown Source) at java.net.PlainSocketImpl.connectToAddress(Unknown Source) at java.net.PlainSocketImpl.connect(Unknown Source) at java.net.SocksSocketImpl.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.connect(Unknown Source) at java.net.Socket.(Unknown Source) at java.net.Socket.(Unknown Source) at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) at com.mysql.jdbc.Connection.createNewIO(Connection.java:2744) at com.mysql.jdbc.Connection.(Connection.java:1553) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at SQL.main(SQL.java:17)

** 结束嵌套异常 **

** END NESTED EXCEPTION **

最后一个发送到服务器的数据包是 1 毫秒前.

Last packet sent to the server was 1 ms ago.

以下是我发现的一些东西:

Here is few things I found around:

  1. IP/域或端口不正确
  2. IP/域或端口(即服务)已关闭
  3. IP/域的响应时间比您的默认超时要长
  4. 您的防火墙阻止了您使用的任何端口上的请求或响应
  5. 您的防火墙阻止了对该特定主机的请求
  6. 您的互联网访问已关闭

a) 这是正确的.b)它没有关闭,因为我的网站现在正在使用该数据库.c) 我该如何检查?我不认为这是问题所在,因为正如我所说,我在这个数据库上有一个正在运行的网站.d)现在的方式,因为我上面提到的原因相同.e) 与 d 相同

a) it is correct. b) it's not down since I got a website that is working right now with that database. c) how can I check that?I don't think that it's the problem since as I said I got a running website on this database. d) now way because the same reason I mentioned above. e) samem as d

那么我做错了什么?为什么 php 代码可以与 sql 一起使用,而 java 却给我错误?谢谢.

So what I'm doing wrong?how come a php code can work with sql but java gives me errors? Thank you.

推荐答案

作为实验,尝试使用系统的 telnet 命令连接到 mysql1.oyo.co.il 上的端口 3306.或者其他一些打开原始 TCP/IP 连接的实用程序(例如 ncnetcat).根据您的操作系统/发行版,您可能需要找到并安装合适的命令来帮助您排除故障.观察会发生什么.

As an experiment, try connecting to port 3306 on mysql1.oyo.co.il using your system's telnet command. Or some other utility (for example nc or netcat) that opens a raw TCP/IP connection. Depending on your OS / distro, you may have to locate and install a suitable command to help you with this trouble-shooting. Observe what happens.

重新诊断:

a) 这是正确的.

如果你这么说...

b) 它没有关闭,因为我有一个网站正在使用该数据库.

b) it's not down since I got a website that is working right now with that database.

充其量你已经证明主机在工作.80端口的web服务和3306端口的MySQL服务是不同的服务.(事实上​​,mysql1.oyo.co.il 可能正在做聪明的数据包路由技巧,以便不同端口上的流量被透明地路由到不同的主机或虚拟主机.)

At best you have demonstrated that the host is working. The web service on port 80 and the MySQL service on port 3306 are different services. (And in fact, it is possible that mysql1.oyo.co.il is doing clever packet routing tricks so that traffic on different ports is being tranparently routed to different hosts or virtual hosts.)

c) 我该如何检查?

尝试更改/增加超时时间.

Try changing / increasing the timeout.

我不认为这是问题所在,因为正如我所说,我在这个数据库上有一个正在运行的网站.

I don't think that it's the problem since as I said I got a running website on this database.

我猜这不是超时问题......但正如我上面所说的,你有一个网站的事实并没有说明 MySQL 服务是否正在运行.

My guess it is not a timeout issue ... but as I said above, the fact you got a website says NOTHING about whether the MySQL service is running.

d) 现在的方式,因为我上面提到的原因相同.

d) now way because the same reason I mentioned above.

您只是证明了您可以访问 80 端口......见上文

You've only demonstrated that you can get to port 80 ... see above

e) 与 d 相同

您只是证明了您可以访问 80 端口......见上文

You've only demonstrated that you can get to port 80 ... see above

基于以上,可以认为:

  • 您有本地防火墙问题,
  • 该端口上的 MySQL 服务当前未运行,
  • 服务端的网络路由因该端口上的流量而中断,
  • MySQL 服务已被永久关闭,或
  • 服务的防火墙已配置为不接受来自随机(或特定)位置的网络连接,包括您的位置.

您看到的是超时而不是连接被拒绝"的事实表明这更有可能是网络或防火墙问题,而不是 MySQL 服务端点本身的问题.

The fact that you are seeing a timeout rather than a "connection refused" suggests that this is more likely to be a networking or firewalling issue than a problem with the MySQL service endpoint itself.

我的猜测是他们"已经撤消了该服务……而且您不应该再使用它了.(提供公共 MySQL 数据库服务在我看来是一件奇怪的事情......而且从安全和管理的角度来看也是有问题的.)

My guess is that "they" have withdrawn the service ... and that you are not supposed to use it anymore. (Providing a public MySQL database service strikes me as an odd thing to do ... as well as being problematic from a security and administration standpoint.)

这篇关于java.net.ConnectException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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