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

时间:2014-02-04 21:14:35

标签: java mysql exception driver connect

我的代码:

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 消息:连接超时:连接

堆栈跟踪:

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值?

2 个答案:

答案 0 :(得分:1)

我对该地址进行了快速在线端口扫描,看起来服务器端没有侦听端口3306.也许防火墙配置?或者只是停止了服务器/错误的地址。

答案 1 :(得分:0)

看起来那个司机正在扔那个

reason can be that

service not listening
Connections are not free