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

cgvd09ve  于 2023-05-12  发布在  Java
关注(0)|答案(2)|浏览(109)

我的代码:

package staffrecords;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;  
import java.sql.ResultSet;
import java.sql.SQLException;

public class StaffRecords {

    public static void main(String[] args) {

        Connection con = null;
        Statement st = null;
        ResultSet rs = null;

        String databaseName = "SEGA";
        //String loginUrl = "jdbc:mysql://localhost:3307/" + databaseName;
        String loginUrl = "jdbc:mysql://dbprojects.eecs.qmul.ac.uk:3306/" + databaseName;
        String user = "SEGA";        
        String password = "*****";

        try 
        {                      
            con = (Connection) DriverManager.getConnection(loginUrl, user, password);
            st = (Statement) con.createStatement();
            String sql;
            sql = "SELECT StaffID FROM Staff";
            rs = st.executeQuery(sql);

            if (rs.next())
            {
                String staffID = rs.getString("StaffID");

                System.out.print("StaffID: " + staffID);
            }         
            rs.close();
            st.close();
            con.close();
        } 
        catch (SQLException ex) 
        {
            System.out.println(ex);
        }      
    }
}

错误是:
com.mysql.jdbc.CommunicationsException:通信链路因基础异常而失败:

开始嵌套例外

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

java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:271)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1555)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
    at java.sql.DriverManager.getConnection(DriverManager.java:579)
    at java.sql.DriverManager.getConnection(DriverManager.java:221)
    at staffrecords.StaffRecords.main(StaffRecords.java:28)

结束嵌套异常

发送到服务器的最后一个数据包是在2毫秒之前。构建成功(总时间:24秒)
是否有一个简单的解决方案,让代码打印出表Staff中的所有StaffID值?

n9vozmp4

n9vozmp41#

我做了一个快速的在线端口扫描该地址,它看起来像服务器端没有监听端口3306。也许是防火墙配置?或者只是停止服务器/错误的地址。

dnph8jn4

dnph8jn42#

看起来那个司机

reason can be that

service not listening
Connections are not free

相关问题