我正在努力让我的数据库与我的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?");
    }
}

当前回答

在我的例子中,当我试图从同一个主机连接到运行在Ubuntu主机虚拟机容器中的mysql-server时,我遇到了这个错误。

例子: 如果我的虚拟机名称是abc.company.com,下面的jdbc URL将不起作用:

jdbc: mysql: / / abc.company.com: 3306 / dbname

上面的jdbc url可以在其他机器上正常工作,如xyz.company.com,但不是abc.company.com。

下面的jdbc URL可以在abc.company.com的机器上正常工作:

jdbc: mysql: / / localhost: 3306 / dbname

这让我检查了/etc/hosts文件。

添加以下行到/etc/hosts可以修复此问题:

127.0.1.1 ABC

这似乎是一个操作系统错误,需要我们在一些Ubuntu版本上添加这些。 参考:https://www.debian.org/doc/manuals/debian-reference/ch05.en.html # _the_hostname_resolution

在尝试这个之前,我已经尝试了所有其他的解决方案,如GRANT all ..,修改mysql.cnf中的绑定地址行。在这件事上他们都没帮上忙。

其他回答

在我的例子中,当我试图从同一个主机连接到运行在Ubuntu主机虚拟机容器中的mysql-server时,我遇到了这个错误。

例子: 如果我的虚拟机名称是abc.company.com,下面的jdbc URL将不起作用:

jdbc: mysql: / / abc.company.com: 3306 / dbname

上面的jdbc url可以在其他机器上正常工作,如xyz.company.com,但不是abc.company.com。

下面的jdbc URL可以在abc.company.com的机器上正常工作:

jdbc: mysql: / / localhost: 3306 / dbname

这让我检查了/etc/hosts文件。

添加以下行到/etc/hosts可以修复此问题:

127.0.1.1 ABC

这似乎是一个操作系统错误,需要我们在一些Ubuntu版本上添加这些。 参考:https://www.debian.org/doc/manuals/debian-reference/ch05.en.html # _the_hostname_resolution

在尝试这个之前,我已经尝试了所有其他的解决方案,如GRANT all ..,修改mysql.cnf中的绑定地址行。在这件事上他们都没帮上忙。

这可能是一个简单的罐子问题。可能您正在使用旧的mysql-connector-java- xxx -bin.jar,它不支持您当前的mysql版本。我使用mysql-connector-java-5.1.18-bin.jar,因为我使用mysql 5.5,这个问题为我解决了。

你有一个

com.mysql.jdbc.exceptions.jdbc4。CommunicationsException:通信链路故障 connectexception:拒绝连接

我引用了这个答案,其中还包含一个循序渐进的MySQL+JDBC教程:

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific CommunicationsException: Communications link failure, then it means that the DB isn't reachable at all. This can have one or more of the following causes: IP address or hostname in JDBC URL is wrong. Hostname in JDBC URL is not recognized by local DNS server. Port number is missing or wrong in JDBC URL. DB server is down. DB server doesn't accept TCP/IP connections. DB server has run out of connections. Something in between Java and DB is blocking connections, e.g. a firewall or proxy. To solve the one or the other, follow the following advices: Verify and test them with ping. Refresh DNS or use IP address in JDBC URL instead. Verify it based on my.cnf of MySQL DB. Start the DB. Verify if mysqld is started without the --skip-networking option. Restart the DB and fix your code accordingly that it closes connections in finally. Disable firewall and/or configure firewall/proxy to allow/forward the port.

参见:

我应该如何连接到JDBC数据库/数据源在一个基于servlet的应用程序? 在多线程系统中使用静态java.sql.Connection实例安全吗?

这是试图连接到旧版本的MySQL ('version', '5.1.73') ); 当您使用较新的驱动程序版本时,您会得到一个错误,告诉您使用“com.mysql. jdbc. jdbc”。甚至你不需要指定你使用的是哪个驱动程序:

加载类com.mysql.jdbc.Driver'。这是不赞成的。新 驱动类iscom.mysql.cj.jdbc.Driver'。司机是 通过SPI自动注册和手动加载驱动程序 类通常是不必要的。

我改变了声明,使用5.1.38版本的mysql-connector-java,在代码中,我保留了com.mysql.jdbc.Driver。

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.38</version>
</dependency>

一切都始于我看到安基特耆那教的答案

在我的情况下,我从intellij IDEA运行时得到这个错误,而从终端运行时。/gradlew bootRun工作正常。然后我发现Intellij指向的是我下载的一些liberica java。我把它改成了OpenJdk one,它开始工作得很好。