连接到大学服务器时出现JDBC连接问题

时间:2013-08-15 22:55:27

标签: eclipse jdbc

我试图从我的工作场所连接到大学服务器并收到回复:

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

有人可以帮我解决这个问题吗?我是新手,并尝试过许多其他方法,如取代大学服务器名称:portnumber与IP地址虽然没有用。我也试过其他远程地址和端口名称,但没有成功。请给我你的建议。

1 个答案:

答案 0 :(得分:0)

连接失败简单检查表:

  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