gpt4 book ai didi

java - 使用 Connector/J 连接到 MYSQL

转载 作者:行者123 更新时间:2023-11-29 16:07:00 24 4
gpt4 key购买 nike

我已经安装了 MySQL Workbench 8.0,并尝试通过 Connector/J 版本 5.1.39 通过 Netbeans 连接到我的数据库。

这个设置在我的 Windows Surface 上运行得很好。我试图在我的家用电脑上复制设置,但遇到了问题。

代码:

String driver       = "jdbc:mysql:";
String hostAndPort = "//localhost:3306/";
String database = "bills";
String options = "?autoReconnect=true&useSSL=false";

final String userName = "root";
final String password = "pass";

String url = driver + hostAndPort + database + options;

try(Connection con = DriverManager.getConnection(url, userName, password);
Statement st = con.createStatement())
{

// Do nothing so far

} catch (SQLException ex) {
System.out.println("SQLException:\t" + ex.getMessage());
System.out.println("SQLState:\t" + ex.getSQLState());
System.out.println("VendorError:\t" + ex.getErrorCode());
}

当我尝试运行应用程序时,它捕获异常并打印:

SQLException:   Could not create connection to database server. Attempted reconnect 3 times. Giving up.
SQLState: 08001
VendorError: 0

值得一提的是,数据库“bills”存在,用户名和密码正确。我可以使用它们进入 MySQL Workbench 并编辑帐单数据库。

我尝试过的事情:

  • 我将 Connector/J 目录路径添加到类路径中。
  • 我将 Connector/J jar 文件直接添加到项目中。
  • 我将“MySQL JDBC Driver”库添加到项目中。
  • 我验证了数据库已设置为使用端口 3306 和“标准 (TCP/IP)”连接方法。 (我对 root 用户也没有“帐户限制”)

enter image description here

  • 我已验证 Windows Defender 防火墙没有阻止 MySQL。 (我没有其他防火墙)

enter image description here

  • 我验证了 MySQL 服务正在运行

enter image description here

同样,此设置在我的 Surface 上运行良好。我尝试过较新版本的 Connector/J,但遇到了与 MySQL Workbench v.8 的兼容性问题,因此我决定使用 v.5.1.39(Connector/J),因为它可以在我的 Surface 上运行。

我觉得我能了解到正在发生的事情的唯一线索是当我检查家用电脑上的端口 Activity 并将其与我的表面上的端口 Activity 进行比较时:

在尝试连接数据库期间观察端口时,我得到以下信息:

enter image description here

为什么我的家用电脑上没有建立端口连接?这是问题的根源吗?我该如何解决它?

如果这不是问题的根源,那么为什么我无法连接?

最佳答案

您的JDBC驱动程序版本可能与您的MySQL数据库服务器不兼容。在这种情况下,您可以尝试将驱动程序更新到您的数据库版本>连接器/J 版本 8.0+

关于java - 使用 Connector/J 连接到 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55643928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com