【问题标题】:JDBC connectivity issue while connecting to college server连接到大学服务器时出现 JDBC 连接问题
【发布时间】:2013-08-15 22:55:26
【问题描述】:

我尝试从工作场所连接到大学服务器并收到响应:

Connection Failed! Check output console 

我的代码:

public JDBCBasicExample() {
        // TODO Auto-generated constructor stub
    }
    public static void main(String[] argv) {

        System.out.println("-------- MySQL JDBC Connection Testing ------------");

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("Where is your MySQL JDBC Driver?");
            e.printStackTrace();
            return;
        }

        System.out.println("MySQL JDBC Driver Registered!");
        Connection connection = null;

        try {
            connection = DriverManager
            .getConnection("jdbc:mysql://collegeservername:portnumber/databasenameinserver",
              "username","password");

        } catch (SQLException e) {
            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;
        }

        if (connection != null) {
            System.out.println("You made it, take control your database now!");
        } else {
            System.out.println("Failed to make connection!");
        }
      }

我收到以下错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:675)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1086)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2486)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at testingdatabase.JDBCBasicExample.main(JDBCBasicExample.java:29)
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly l`enter code here`ost.
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:3119)
    at com.mysql.jdbc.MysqlIO.readPacket(MysqlIO.java:599)
    ... 16 more

有人可以帮我解决这个问题吗?我对此并不陌生,并尝试了许多其他方法,例如用 IP 地址替换学院服务器名称:端口号,尽管它不起作用。我也尝试过使用其他远程地址和端口名称,但没有成功。请给我你的建议。

【问题讨论】:

  • 您好,该错误表示它无法与数据库或数据库服务器通信。数据库服务器是否有任何防火墙只允许来自特定 IP 地址集的连接?您确定数据库正在运行吗?您确定您使用的是正确的端口吗?所有这三个问题都是收到该错误的常见原因。我将从执行代码的机器上进行远程登录。执行“telnet serverurl 端口号”。
  • 我可以连接到服务器,但端口有问题

标签: eclipse jdbc


【解决方案1】:

连接失败简单检查表:

  1. 您的网络工作正常。
  2. 您的大学服务器中是否有防火墙阻止或允许来自一组 IP 地址的连接?
  3. 数据库服务器的地址和端口是否正确??检查以确保外部 IP 在内部实际可用。
  4. mysql服务是否启动?? (查看系统服务列表和tcp监听列表,mysql是否监听连接??)
  5. 你能从你的工作空间通过 mysql shell 登录你的数据库吗? (打开终端,输入mysql -h your_database_host -u your_username -p登录)
  6. 数据库是否允许远程访问? (查看数据库服务器中的my.cnf文件,去掉bind-address=127.0.0.1这一行)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2010-10-11
    • 2016-10-05
    • 1970-01-01
    • 2012-10-25
    相关资源
    最近更新 更多