我正在努力让我的数据库与我的Java程序对话。

有人能给我一个使用JDBC的快速而简单的示例程序吗?

我得到了一个相当大的错误

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:207)
    at SqlTest.main(SqlTest.java:22)
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.ConnectException: Connection refused
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:432)
    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

测试文件内容:

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

public class SqlTest {

    public static void main(String [] args) throws Exception {
        // Class.forName( "com.mysql.jdbc.Driver" ); // do this in init
        // // edit the jdbc url 
        Connection conn = DriverManager.getConnection( 
            "jdbc:mysql://localhost:3306/projects?user=user1&password=123");
        // Statement st = conn.createStatement();
        // ResultSet rs = st.executeQuery( "select * from table" );

        System.out.println("Connected?");
    }
}

当前回答

我刚开始申请的时候也遇到过同样的问题,

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.

问题是在mysql文件/etc/mysql/mysql.conf.d/mysqld.cnf中,配置是这样的

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

所以我们可以看到,绑定地址是指向127.0.0.1,所以启动你的应用程序,一个简单的解决方案是,我们可以使用jdbc:mysql://127.0.0.1:3306/pizzeria,而不是jdbc:mysql://localhost:3306/pizzeria,将mysql连接到应用程序或另一个解决方案是,你可以检查和改变mysql配置为默认值。两种方案都可行。 我希望这对你们也有用。

其他回答

我刚开始申请的时候也遇到过同样的问题,

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.

问题是在mysql文件/etc/mysql/mysql.conf.d/mysqld.cnf中,配置是这样的

[mysqld]
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
# By default we only accept connections from localhost
bind-address    = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

所以我们可以看到,绑定地址是指向127.0.0.1,所以启动你的应用程序,一个简单的解决方案是,我们可以使用jdbc:mysql://127.0.0.1:3306/pizzeria,而不是jdbc:mysql://localhost:3306/pizzeria,将mysql连接到应用程序或另一个解决方案是,你可以检查和改变mysql配置为默认值。两种方案都可行。 我希望这对你们也有用。

我可能找错了对象,但您的异常似乎表明您的MySQL服务器不可用。

线程main中出现异常: 日志含义通信链路故障最后一个成功发送到服务器的报文为0 毫秒前。驱动未收到服务器的报文。处理步骤在…

如果你尝试(从终端)会发生什么?

mysql -u username -p

系统将提示您输入与用户名关联的密码。在你给出正确的密码后,mysql客户端连接吗?

如果没有,你可能需要从首选项启动MySQL。您还可以将其设置为在启动时运行。

在我的案例中,解决方案是将预期的TLS协议添加到连接字符串中,如下所示:

jdbc:mysql://localhost:3306/database_name?enabledTLSProtocols=TLSv1.2

出于测试/开发的目的(不推荐用于生产),您还可以尝试:

jdbc:mysql://localhost:3306/database_name?useSSL=false&allowPublicKeyRetrieval=true

尝试将localhost更改为127.0.0.1。

本地主机将被解析为::1。MySQL默认不支持IPv6连接。

下面是telnet localhost 3306的输出:

$ telnet localhost 3306
Trying ::1...

MySQL服务器没有响应。

当然,请确保您的MySQL服务器正在运行。

我解决了两件事: 1. 使用以下connads创建非root的新用户:

CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'newuser'@'localhost';
FLUSH PRIVILEGES;

2. 注释mysqld.conf中的IP地址行

然后用新的用户名和密码连接。它应该会起作用。