我一直试图在eclipse中使用JDBC连接到我的外部MySQL服务器位置http://mysql10.000webhost.com ,但是我一直收到错误消息。 这是我的代码:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;

    import com.mysql.jdbc.PreparedStatement;

    //import com.mysql.jdbc.Driver;

    public class Main {
        public static void main(String[] args) throws SQLException {
            try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn=DriverManager.getConnection("jdbc:mysql://mysql10.000webhost.com/database","username","password");
    java.sql.PreparedStatement statement = conn.prepareStatement("SELECT * FROM promoid");
    ResultSet result=statement.executeQuery();
    while(result.next())
    {
        System.out.println(result.getString(1));
    }
        }

        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    }

这是我得到的错误:

    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.<init>(MysqlIO.java:355)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2479)
        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2516)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2301)
        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:317)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at Main.main(Main.java:15)
    Caused by: 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.<init>(Unknown Source)
        at java.net.Socket.<init>(Unknown Source)
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:305)
        ... 15 more

JDBC不能连接到localhost之外的数据库吗?

===============>>#1 票数:0

Caused by: java.net.ConnectException: Connection timed out: connect.

您是否尝试过远程登录到主机mysql10.000webhost.com上的port 3306 (mysql默认)?

我敢打赌他们在防火墙上阻止了您。

  ask by user2230755 translate from so

未解决问题?本站智能推荐: