使用Connector / J连接到MYSQL

时间:2019-04-12 03:08:10

标签: java mysql netbeans connector-j

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

这是一个可以在Windows Surface上正常运行的设置。 我正在尝试复制家用PC上的设置,但是遇到了问题。

代码:

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

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

我尝试过的事情:

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

enter image description here

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

enter image description here

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

enter image description here

同样,此设置在我的Surface上也能正常工作。 我已经尝试使用更新版本的Connector / J,但是遇到了MySQL Workbench v.8的兼容性问题,所以我决定使用(Connector / J的)5.1.39版,因为它可以在我的Surface上使用。

我唯一想知道发生了什么的地方是当我检查家用PC上的ports活动并将其与我的表面的活动进行比较时:

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

enter image description here

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

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

1 个答案:

答案 0 :(得分:0)

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