无法加载驱动程序类:com.mysql.jdbc.Driver Spring
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33123985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot load driver class: com.mysql.jdbc.Driver Spring
提问by voidcurser
spring.freemarker.cache:false
spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
im getting this error even with the driver-class-name difined
即使驱动程序类名称已定义,我也会收到此错误
java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.2.1.RELEASE.jar:4.2.1.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:153) ~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:119) ~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60] at ......
java.lang.IllegalStateException:无法加载驱动程序类:com.mysql.jdbc.Driver at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.2.1.RELEASE.jar:4.2 .1.RELEASE] 在 org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:153) ~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] 在 org .springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:119) ~[spring-boot-autoconfigure-1.3.0.M5.jar:1.3.0.M5] at sun.reflect.NativeMethodAccessorImpl。 invoke0(Native Method) ~[na:1.8.0_60] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60] at ......
and it goes on
它继续
回答by bekce
If you're using Maven, add this to your pom.xml
:
如果您使用的是 Maven,请将其添加到您的pom.xml
:
(Recommended) For MySQL 5.6, 5.7, 8.x
and Java >= 8
use;
(推荐)供MySQL 5.6, 5.7, 8.x
和Java >= 8
使用;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
(Legacy) For MySQL <= 5.5
or Java <= 7
or JDBC < 4.2
use;
(遗留)为了MySQL <= 5.5
或Java <= 7
或JDBC < 4.2
使用;
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
回答by Brijesh
It might be that you are using the deprecated driver class name. I solved it by updating the property in application.properties file.
可能是您使用了已弃用的驱动程序类名称。我通过更新 application.properties 文件中的属性解决了这个问题。
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
回答by HA S
Just confirm the following things.
只需确认以下事项。
If you are under proxy make sure use VPN to connect to internal servers, iif you are accessing DEV/STG servers
Make sure you add the following dependency inside correct location, like this
<dependencies> <--- inside this section <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> </dependencies>
如果您使用代理,请确保使用 VPN 连接到内部服务器,如果您正在访问 DEV/STG 服务器
确保在正确的位置添加以下依赖项,像这样
<dependencies> <--- inside this section <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> </dependencies>
and Not in
并且不在
`<build> <--- Not this section
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
` 3. And you don't need to use the following line
` 3. 并且您不需要使用以下行
`spring.datasource.driver-class-name=com.mysql.jdbc.Driver.
The spring.datasource.url=jdbc:mysql://xxxxx` automatically know which driver to fetch.
spring.datasource.url=jdbc:mysql://xxxxx` 自动知道要获取哪个驱动程序。
回答by Dherik
I will share more causes of this error:
我将分享这个错误的更多原因:
- I already seen this error many times using Intellij IDEAand running some integration test. The test fail with the message:
Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver
. After rebuild the project (Build > Rebuild Project) the error disappears. - Another cause of this error is a corrupted JAR. A college was receiving this error (also trying to run integration tests) and, after delete some related jars on
.m2
directory, the error was gone.
- 我已经在使用 Intellij IDEA并运行一些集成测试时多次看到此错误。测试失败并显示消息:
Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver
。重建项目(Build > Rebuild Project)后,错误消失。 - 此错误的另一个原因是损坏的 JAR。一所大学收到此错误(也尝试运行集成测试),并且在删除
.m2
目录中的一些相关 jar 后,错误消失了。
回答by Chaklader Asfak Arefe
I encountered the same problem now and solved it after providing the required dependency,
我现在遇到了同样的问题并在提供所需的依赖项后解决了它,
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
回答by cegas
The crux of the problem is that you're missing MySQL driver dependency.
问题的关键是您缺少 MySQL 驱动程序依赖项。
One of the ways, as outlined by other answers, is to specify it in your build tool's configuration. However, if you do not want to do it and are using IntelliJ IDEA (though I'm pretty sure Eclipse has something similar), you can also add the dependency via it. Steps:
正如其他答案所述,其中一种方法是在构建工具的配置中指定它。但是,如果您不想这样做并且正在使用 IntelliJ IDEA(尽管我很确定 Eclipse 有类似的东西),您也可以通过它添加依赖项。脚步:
- Download desired jar from maven repository
- Open your project in Intellij IDEA
- File -> Project Structure -> Libraries
- Click New Project Library (green plus sign on the left), or press Alt + Insert keys
- Select Java
- Select your jar
- Press OK
- 从Maven 存储库下载所需的 jar
- 在 Intellij IDEA 中打开您的项目
- 文件 -> 项目结构 -> 库
- 单击新建项目库(左侧绿色加号),或按 Alt + Insert 键
- 选择 Java
- 选择你的罐子
- 按确定
Should look something like this:
应该是这样的:
Now you should be able to use the dependency you have just added.
现在您应该可以使用刚刚添加的依赖项了。
Tested with IntelliJ IDEA 2017.3.4.
使用 IntelliJ IDEA 2017.3.4 进行测试。
回答by Markus G.
Probably someone still needs an answer (as me before) I solved it by adding the following dependency
可能有人仍然需要答案(就像我以前一样)我通过添加以下依赖项解决了它
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
回答by SatyaRajC
build.gradle
构建.gradle
runtimeOnly 'mysql:mysql-connector-java'
runtimeOnly 'mysql:mysql-connector-java'
application.yml
应用程序.yml
spring:
application:
name: apevent
version: 1.0
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/applicaiton