java – jdbc:无法创建与数据库服务器的连接

前端之家收集整理的这篇文章主要介绍了java – jdbc:无法创建与数据库服务器的连接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

参见英文答案 > com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure                                    32个
我正在尝试使用jdbc连接MysqL数据库.
这是我的数据库连接类:

public class Database {
    String userName = "myUserName";
    String password = "myPass";

    String url = "jdbc:MysqL://xxx.xxx.xxx.xxx:3306/database_Name?autoReconnect=true&useSSL=false";

    public void connect()
    {
        try {
            Class.forName("com.MysqL.jdbc.Driver").newInstance();

            Connection conn = DriverManager.getConnection(url,userName,password);

            Statement statement = conn.createStatement();
            String queryString = "select * from users";
            ResultSet rs = statement.executeQuery(queryString);
            while (rs.next()) {
                System.out.println(rs.getString(2));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

现在,您可以假设我调用方法如下:

new Database().connect();

我得到这个例外:

com.MysqL.jdbc.exceptions.jdbc4.MysqLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.MysqL.jdbc.Util.handleNewInstance(Util.java:425)
    at com.MysqL.jdbc.Util.getInstance(Util.java:408)
    at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:918)
    at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:897)
    at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:886)
    at com.MysqL.jdbc.sqlError.createsqlException(sqlError.java:860)
    at com.MysqL.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2163)
    at com.MysqL.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2088)
    at com.MysqL.jdbc.ConnectionImpl.MysqL.jdbc.JDBC4Connection.MysqL.jdbc.Util.handleNewInstance(Util.java:425)
    at com.MysqL.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
    at com.MysqL.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at Database.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(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.MysqL.jdbc.Util.handleNewInstance(Util.java:425)
    at com.MysqL.jdbc.sqlError.createCommunicationsException(sqlError.java:989)
    at com.MysqL.jdbc.MysqLIO.MysqLIO.java:341)
    at com.MysqL.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
    at com.MysqL.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2104)
    ... 19 more
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.MysqL.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
    at com.MysqL.jdbc.MysqLIO.MysqLIO.java:300)
    ... 21 more

*我添加%来从远程访问数据库.

* Java部分为localhost工作但是当我尝试这个远程数据库时,它失败了.

*创建了数据库和表.(没有问题.)

那问题出在哪里?

最佳答案

Java part working for localhost but when I try this remote database,it fails

这意味着,您的MysqL服务器绑定到localhost,并且在外部不可见.
请将MysqL conf文件中的bind-address属性设置为主机的实际IP地址.

例如:bind-address = 192.168.1.26

根据实际IP地址,我的意思是您的客户端可以看到运行Java代码的位置.这可以是内联网地址或公共IP.如果要公开您的公共IP,则可能必须使用某些防火墙.

原文链接:https://www.f2er.com/mysql/432876.html

猜你在找的MySQL相关文章