ClassNotFoundException 与 com.mysql.cj.jdbc.Driver、MySQL 连接器和 IntelliJ IDEA [英] ClassNotFoundException with com.mysql.cj.jdbc.Driver, MySQL Connector and IntelliJ IDEA
问题描述
我正在构建一个 Maven Java 1.8 项目,其中我将 MySQL 连接器作为依赖项包含在内:
I'm building up a Maven Java 1.8 project in which I've included the MySQL Connector as a dependency:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
在我的应用程序中,我有一个保存 MySQL 连接的单例,因此当应用程序启动时,会触发 MySQL 连接器,但我正在使用的驱动程序出现 ClassNotFoundException:com.mysql.cj.jdbc.Driver.
In my application I've a singleton that holds the MySQL connection so when the application starts, the MySQL connector is triggered but I got a ClassNotFoundException for the driver that I'm using: com.mysql.cj.jdbc.Driver.
我使用的 JDBC URL 是:
The JDBC URL that I'm using is:
jdbc:mysql://localhost:3306/mydb?autoReconnect=true&useSSL=false
我使用的是 IntelliJ IDEA (2017.2) IDE.
I'm using IntelliJ IDEA (2017.2) IDE.
我已经检查了文件 -> 项目结构 -> 库 -> 我看到Maven: mysql:mysql-connector-java:5.1.392"作为一个库.
I've checked in File -> Project Structure -> Libraries -> I see "Maven: mysql:mysql-connector-java:5.1.392" as a Library.
我还检查了文件 -> 项目结构 -> 工件 -> 和 myapp.war 下爆炸 -> WEB-INF -> lib -> Maven: mysql:mysql-connector-java:5.1.39 is present.
I've also checked under File -> Project Structure -> Artifacts -> and under myapp.war exploded -> WEB-INF -> lib -> Maven: mysql:mysql-connector-java:5.1.39 is present.
从视图 -> 工具窗口 -> 数据库,我已经成功地建立了一个 MySQL 连接,下载了建议的驱动程序(MySQL 连接器 5.1.35).
From View -> Tool Windows -> Database, I've succesfully made a MySQL connection dowloading the suggested driver (MySQL Connector 5.1.35).
如何告诉应用程序它必须在运行时加载 MySQL 驱动程序?
How can I tell the application that it has to load the MySQL driver at runtime?
提前致谢
推荐答案
尝试升级您的驱动程序.因为 Mysql 社区已将类名从 com.mysql.jdbc.Driver
更新为 com.mysql.cj.jdbc.Driver
查看 MySql 社区的更多信息
Try upgrading your driver. Coz Mysql community has updated class name from com.mysql.jdbc.Driver
to com.mysql.cj.jdbc.Driver
Check out more on MySql Community
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
或者直接从这里下载 jar 文件:
Or Download jar file direct from here :
mysql-connector-java-8.0.11.jar
这篇关于ClassNotFoundException 与 com.mysql.cj.jdbc.Driver、MySQL 连接器和 IntelliJ IDEA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!