简体   繁体   English

无法加载驱动程序 class com.mysql.Z84BEFFD3A0D49636A58CE6080CAA87C7

[英]Failed to load driver class com.mysql.jdbc.Driver

I am trying to run my Spring Boot backend with two profiles, one using H2 in memory database and the second one using MySQL.我正在尝试使用两个配置文件运行我的 Spring 引导后端,一个在 memory 数据库中使用 H2,第二个使用 MySQL。 H2 database works just fine, but when I switch to MySQL I get H2 数据库工作得很好,但是当我切换到 MySQL 时,我得到了

APPLICATION FAILED TO START
***************************

Description:

Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:

    Property: driverclassname
    Value: com.mysql.jdbc.Driver;
    Origin: "driverClassName" from property source "source"
    Reason: Failed to load driver class com.mysql.jdbc.Driver; in either of HikariConfig class loader or Thread context classloader

Action:

Update your application's configuration

I have tried deleting.m2, reimporting, maven clean, compile, install and most of the things I could find on the internet, no success.我试过删除.m2,重新导入,maven 清理,编译,安装和我在互联网上可以找到的大部分东西,没有成功。 The funny thing is that I have other project with MySQL database only, I had similar issue, but adding mysql-connector-java dependency solved it.有趣的是,我有其他项目仅使用 MySQL 数据库,我有类似的问题,但添加mysql-connector-java依赖项解决了它。 I have no clue right now.我现在没有头绪。

application.properties应用程序属性

spring.profiles.active=@profilename@

#H2 in memory database
domain.datasource.type=H2
domain.datasource.url=jdbc:h2:mem:store;MODE=MYSQL;
domain.datasource.driver-class=org.h2.Driver
domain.datasource.username=sa
domain.datasource.password=
domain.datasource.generate-dll=true

application-local_mysql.properties应用程序-local_mysql.properties

spring.profiles.active=@profilename@

#MySQL local database
domain.datasource.type=MYSQL
domain.datasource.url=jdbc:mysql://localhost:3600/store;
domain.datasource.driver-class=com.mysql.jdbc.Driver;
domain.datasource.username=store
domain.datasource.password=store
domain.datasource.generate-dll=false

pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sk.personal</groupId>
    <artifactId>my-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>my-project</name>
    <description>My personal project.</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    </dependencies>

    <profiles>
        <profile>
            <id>local_h2</id>
            <properties>
                <profilename>local_h2</profilename>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>local_mysql</id>
            <properties>
                <profilename>local_mysql</profilename>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

DatasourceConfig.java数据源配置.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;

@Configuration
public class DatasourceConfig {

    @Value("${domain.datasource.url}")
    private String url;

    @Value("${domain.datasource.username}")
    private String username;

    @Value("${domain.datasource.password}")
    private String password;

    @Value("${domain.datasource.type}")
    private String type;

    @Value("${domain.datasource.driver-class}")
    private String driverClass;

    @Bean
    public DataSource dataSource() {
        if (type.equals("MYSQL")) {
            return DataSourceBuilder
                    .create()
                    .username(username)
                    .password(password)
                    .url(url)
                    .driverClassName(driverClass)
                    .build();
        } else {
            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
            return builder
                    .setType(EmbeddedDatabaseType.H2)
                    .build();
        }
    }
}

In my case the next dependency was missing:在我的情况下,下一个依赖项丢失了:

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

In case of using IntelliJ and if you inherit from a <parent> , you can view your effective pom.xml by right clicking anywhere inside your pom.xml , then:如果使用 IntelliJ,并且如果您从<parent>继承,则可以通过右键单击pom.xml中的任意位置来查看有效的pom.xml ,然后: 在此处输入图像描述

and search for the mysql-connector-java artifact as mentioned.并搜索提到的mysql-connector-java工件。

The answer is so embarrassing.答案太尴尬了。 I appended the driver line of application.properties with a semicolon ... Obviously, it did't recognize that driver.我在 application.properties 的驱动程序行后面加上了一个分号……显然,它没有识别出那个驱动程序。

just add mysql and jdbc dependencies like below只需添加 mysql 和 jdbc 依赖项,如下所示

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>

I had a problem where I was using Spring Boot 2.2.0.RELEASE and needed to connect to an old Mysql DB (5.1.73), which required me to downgrade to mysql-connector-java version 5.1.38我在使用 Spring Boot 2.2.0.RELEASE 时遇到问题,需要连接到旧的 Mysql DB (5.1.73),这需要我降级到 mysql-connector-java 版本 5.1.38

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

Since Spring boot was expecting a newer mysql-java-connector, which has been renamed to com.mysql.cj.jdbc.Driver, I also had to add the spring datasource driver-class-name setting in my spring boot db config.由于 Spring boot 需要更新的 mysql-java-connector,它已重命名为 com.mysql.cj.jdbc.Driver,因此我还必须在我的 spring boot db 配置中添加 spring datasource driver-class-name 设置。

So my spring boot config ended up like this:所以我的 Spring Boot 配置最终是这样的:

spring:
  datasource:
   url: 'localhost'
   password: password
   username: user
   driver-class-name: com.mysql.jdbc.Driver

您没有指定 MYSQL JDBC 驱动程序的版本,因此您可能会获得版本8.x ,其中驱动程序的名称与以前的版本不同:

com.mysql.cj.jdbc.Driver

In my case error throws:在我的情况下,错误抛出:

Property: driverclassname
Value: com.mysql.cj.jdbc.Driver
Origin: "driverClassName" from property source "source"

Reason: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader原因:在 HikariConfig 类加载器或线程上下文类加载器中加载驱动程序类 com.mysql.cj.jdbc.Driver 失败

So I have just added mysql dependency:所以我刚刚添加了mysql依赖:

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

Try upgrading your driver.尝试升级您的驱动程序。 Coz Mysql community has updated class name from com.mysql.jdbc.Driver to com.mysql.cj.jdbc. Coz Mysql 社区已将类名从com.mysql.jdbc.Driver 更新为 com.mysql.cj.jdbc。 Check More Details 查看更多详情

Change the database driver dependency scope to 'runtime'将数据库驱动程序依赖范围更改为“运行时”

For example:例如:

   <dependency>
        <groupId>com.{yourDatabaseGroupid}</groupId>
        <artifactId>{yourDatabaseArtifactId}</artifactId>
        <scope>runtime</scope>
    </dependency>

You should add: spring.datasource.driver-class-name=com.mysql.jdbc.Driver to your application.properties file .您应该将: spring.datasource.driver-class-name=com.mysql.jdbc.Driver到您的application.properties文件中。

My **application.properties : **我的 **application.properties :**

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root

I cant believe it!我不敢相信! In intellij: Build->rebuild project solved the issue for me!在 intellij 中:Build- >rebuild project为我解决了这个问题!

在此处输入图像描述

For more information:了解更多信息:

pom.xml : pom.xml 在此处输入图像描述

application.properties:应用程序属性: 在此处输入图像描述

I had the same problem as you.我和你有同样的问题。 For me, it was because of having h2 database dependency!对我来说,这是因为有 h2 数据库依赖! I don't know how these two can affect each other, but all I did was removing this dependency and now it works just fine!我不知道这两者如何相互影响,但我所做的只是删除了这种依赖关系,现在它工作得很好!

Had to specify the jar path inside project->Properties->JPA-> connection Profile -> JAR List必须在 project->Properties->JPA-> connection Profile -> JAR List 中指定 jar 路径

配置屏幕截图

I my case all configurations were correct but I still get this error.我的情况是所有配置都是正确的,但我仍然收到此错误。

In Intellij, go in the project structure ( ctrl + alt + maj + S ) in windows.在 Intellij 中,进入 windows 中的项目结构( ctrl + alt + maj + S )。 Look if u get some problems.看看你有没有问题。

If yes, go on maven in the sidebar: clic on "Generate sources and Update Folders for All projetct"如果是,请在侧边栏中继续使用maven :单击“为所有项目生成源和更新文件夹”

That resolve my error !这解决了我的错误!

this issue for me was also caused by the version of mysql.对我来说这个问题也是由mysql的版本引起的。 all i had to do is add the version of mysql in pom.xml dependencies (in my case the verion is 8.0.25)我所要做的就是在 pom.xml 依赖项中添加 mysql 的版本(在我的情况下,版本是 8.0.25)

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

make sure to reload the maven dependecies before your run确保在运行之前重新加载 maven 依赖项

I had the same issue and the root cause was [ spring.我有同样的问题,根本原因是[spring。 profile .active ] and the fix is [ spring. profile .active ],修复是 [spring。 profiles .active ] where it is profiles plural and it was my mistake. profile .active ] 其中是profile复数,这是我的错误。

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

相关问题 无法加载 JDBC 驱动程序类 [com.mysql.jdbc.Driver] - Could not load JDBC driver class [com.mysql.jdbc.Driver] 无法加载驱动程序 class:com.mysql.jdbc.Driver Spring - Cannot load driver class: com.mysql.jdbc.Driver Spring 无法运行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 找不到类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”) 未找到指定的 JDBC 驱动程序 com.mysql.jdbc.Driver 类 - Specified JDBC Driver com.mysql.jdbc.Driver class not found 无法加载JDBC驱动程序类&#39;com.mysql.jdbc.Driver&#39;Tomcat 8和Eclipse - Cannot load JDBC driver class 'com.mysql.jdbc.Driver' Tomcat 8 & Eclipse java.lang.IllegalStateException: 无法加载 JDBC 驱动程序类 [com.mysql.jdbc.Driver] - java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.jdbc.Driver] Java如何查找类? (尝试加载驱动程序:com.mysql.jdbc.Driver) - Java How To Find Class? (Attempting to load the driver: com.mysql.jdbc.Driver)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM