简体   繁体   English

无法加载驱动程序 class:com.mysql.jdbc.Driver Spring

[英]Cannot load driver class: com.mysql.jdbc.Driver Spring

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: 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] 在 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] 在 sun.reflect.NativeMethod.voccessor0(mpl本机方法) ~[na:1.8.0_60] 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60] 在......

and it goes on它继续

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.xJava >= 8使用;

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

(Legacy) For MySQL <= 5.5 or Java <= 7 or JDBC < 4.2 use; (旧版)对于MySQL <= 5.5Java <= 7JDBC < 4.2使用;

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

More details on Connector/J versions 有关连接器/J 版本的更多详细信息

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

Just confirm the following things.只需确认以下事项。

  1. If you are under proxy make sure use VPN to connect to internal servers, iif you are accessing DEV/STG servers如果您在代理下,请确保使用 VPN 连接到内部服务器,如果您正在访问 DEV/STG 服务器

  2. 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>

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` 自动知道要获取哪个驱动程序。

If you are using IDEA and maven, you might forget reloading all maven projects, which causes the dependencies don't have the MySQL connector jar.如果您使用的是 IDEA 和 maven,您可能会忘记重新加载所有 maven 项目,这会导致依赖项没有 MySQL 连接器 jar。 And that's why when u run the application, there will be the error remind "com.mysql.cj.jdbc.Driver".这就是为什么当你运行应用程序时,会有错误提示“com.mysql.cj.jdbc.Driver”。

在此处输入图像描述

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.但是,如果您不想这样做并且正在使用 IntelliJ IDEA(尽管我很确定 Eclipse 有类似的东西),您也可以通过它添加依赖项。 Steps:脚步:

  1. Download desired jar from maven repositoryMaven 存储库下载所需的 jar
  2. Open your project in Intellij IDEA在 Intellij IDEA 中打开您的项目
  3. File -> Project Structure -> Libraries文件 -> 项目结构 -> 库
  4. Click New Project Library (green plus sign on the left), or press Alt + Insert keys单击新建项目库(左侧的绿色加号),或按 Alt + Insert 键
  5. Select Java选择 Java
  6. Select your jar选择你的罐子
  7. Press OK按确定

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 测试。

I will share more causes of this error:我将分享更多导致此错误的原因:

  • I already seen this error many times using Intellij IDEA and running some integration test.我已经多次使用 Intellij IDEA并运行一些集成测试看到此错误。 The test fail with the message: Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver .测试失败并显示以下消息: Caused by: java.lang.IllegalStateException: Cannot load driver class: org.h2.Driver After rebuild the project (Build > Rebuild Project) the error disappears.重建项目(Build > Rebuild Project)后,错误消失。
  • Another cause of this error is a corrupted JAR .此错误的另一个原因是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.一所大学收到此错误(也尝试运行集成测试),并且在删除.m2目录上的一些相关 jar 后,错误消失了。

build.gradle构建.gradle

 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

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>

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>

I know this might be a little late but maybe anyone from the future might find this answer helpful.我知道这可能有点晚了,但也许未来的任何人都可能会觉得这个答案很有帮助。 This problem arises from potentially three reasons这个问题可能源于三个原因

  • Incorrect maven or Gradle configuration不正确的 Maven 或 Gradle配置

  • Incorrect spring configuration in the application.properties file application.properties 文件中的 spring 配置不正确

  • Wrong class name on the application.properties file. application.properties 文件中的类名错误。

  • To troubleshoot if you are using maven.如果您使用的是 maven,请进行故障排除。 do not include the version name .不包括版本名称 This mostly is a recipe for problems, especially in huge applications.这主要是问题的根源,尤其是在大型应用程序中。 use maven autocompletes features to save you a little time.使用 maven 自动完成功能可以为您节省一点时间。

  • configure the data source correctly.正确配置数据源。 There are a lot of answers on it to this question.这个问题有很多答案。 make sure it's correct.确保它是正确的。 sometimes there is a cj that is missing or included where it's not supposed to be.有时会在不应该出现的地方丢失或包含一个cj Trial and error might help you discover the problem反复试验可能会帮助您发现问题

if neither of the above options works.如果上述选项都不起作用。 Remove spring.datasource.driver-class-name=com.mysql.jdbc.driver completely.完全删除spring.datasource.driver-class-name=com.mysql.jdbc.driver I don't know if it magic but this last one worked for me.我不知道它是否神奇,但最后一个对我有用。

GO NERDS!!!去书呆子!

This type of error basically generate for sql version.这种类型的错误基本上是针对sql版本生成的。 change your /or add sql version in your macen dependency更改您的/或在您的 macen 依赖项中添加 sql 版本

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

adding the following dependency solved my issue添加以下依赖项解决了我的问题

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

我的问题仅通过刷新项目就解决了,也许在您运行应用程序时依赖项没有加载

I had the same issue.我遇到过同样的问题。 It happened because I forgot to declare the MySQL connector dependency in my pom file.发生这种情况是因为我忘记在我的 pom 文件中声明 MySQL 连接器依赖项。 I added the dependency, then boom.我添加了依赖项,然后繁荣。 The error went away.错误消失了。

For People who uses Gradle 8.0 and beyond.对于使用 Gradle 8.0 及更高版本的人。 I had to use我不得不使用

implementation 'mysql:mysql-connector-java:8.0.26'

instead of代替

implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.26' , implementation group: 'mysql', name: 'mysql-connector-java', version: '8.0.26'

my application.yml looks like:我的 application.yml 看起来像:

spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/<Your DB name>?useSSL=false

remember to reload your gradle file记得重新加载你的 gradle 文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载驱动程序类:com.mysql.jdbc.Driver Spring Boot - Cannot load driver class: com.mysql.jdbc.Driver Spring Boot 无法加载 JDBC 驱动程序类 [com.mysql.jdbc.Driver] - Could not load JDBC driver class [com.mysql.jdbc.Driver] 无法加载驱动程序 class com.mysql.Z84BEFFD3A0D49636A58CE6080CAA87C7 - Failed to load driver class com.mysql.jdbc.Driver 无法加载JDBC驱动程序类&#39;com.mysql.jdbc.Driver&#39;Tomcat 8和Eclipse - Cannot load JDBC driver class 'com.mysql.jdbc.Driver' Tomcat 8 & Eclipse 无法运行Spring Boot应用程序原因无法加载驱动程序类com.mysql.jdbc.Driver - Cant run Spring Boot application reason Failed to load driver class com.mysql.jdbc.Driver 无法加载该类:com.mysql.jdbc.Driver - Can't load the class: com.mysql.jdbc.Driver 无法为连接URL&#39;jdbc:mysql // localhost:3306 /创建类&#39;com.mysql.jdbc.Driver&#39;的JDBC驱动程序 - Cannot create JDBC driver of class 'com.mysql.jdbc.Driver' for connect URL 'jdbc:mysql//localhost:3306/ 找不到类com.mysql.jdbc.Driver - Class not found com.mysql.jdbc.Driver 无法通过class.forName(“ com.mysql.jdbc.Driver”)加载com.mysql.jdbc.Driver类 - Couldn't load class com.mysql.jdbc.Driver by class.forName(“com.mysql.jdbc.Driver”) BIRT JDBCException“无法加载JDBC驱动程序类:com.mysql.jdbc.Driver”从2.6升级到3.7 - BIRT JDBCException “Cannot load JDBC Driver class: com.mysql.jdbc.Driver” while upgrading from 2.6 to 3.7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM