关于JDBC无法连接MYSQL,求大佬~

数据库服务和管理,MySQL/PostgreSQL/Oracle/Sybase 等
回复
q1234556
帖子: 1
注册时间: 2019-01-24 23:30
送出感谢: 0
接收感谢: 0

关于JDBC无法连接MYSQL,求大佬~

#1

帖子 q1234556 » 2019-01-24 23:40

不知道是什么个情况,在windows系统上能顺利使用JDBC,但是在linux上面JDBC就无法使用了,jar包什么的完全一致,有大佬能帮忙分析下原因吗,都好几天了实在不知道怎么办了~~~~数据库设置在linux系统上,在windows上面测试的时候是指向的linux地址,可以成功, :Cry

代码: 全选

public class ForTest {

    private static final String dbClassName = "com.mysql.jdbc.Driver";
    private static final String CONNECTION = "jdbc:mysql://localhost/maple";

    public static void main(String[] args) throws
            ClassNotFoundException, SQLException {
        {
            System.out.println(dbClassName);
            Class.forName(dbClassName);

            Properties p = new Properties();
            p.put("user", "root");
            p.put("password", "root");

            Connection c = DriverManager.getConnection(CONNECTION, p);
            System.out.println("It works !");
            ResultSet resultSet = c.prepareStatement("select * from test").executeQuery();
            while (resultSet.next()) {
                System.out.println(resultSet.toString());
            }
            c.close();
        }
    }
}
Exception in thread "main" 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(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2260)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:787)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:49)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:357)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at xpatch.ForTest.main(ForTest.java:24)
Caused by: 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(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1122)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:344)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2181)
... 12 more
Caused by: java.net.SocketException: 网络不可达
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at java.net.Socket.<init>(Socket.java:375)
at java.net.Socket.<init>(Socket.java:218)
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:293)
... 13 more
头像
astolia
论坛版主
帖子: 4093
注册时间: 2008-09-18 13:11
送出感谢: 1 次
接收感谢: 678 次

Re: 关于JDBC无法连接MYSQL,求大佬~

#2

帖子 astolia » 2019-01-26 21:11

运行一下nslookup localhost,看看localhost有没有被正确解析
回复

回到 “数据库管理”