Java 未能加载驱动程序类 com.mysql.jdbc.Driver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52804228/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:37:25  来源:igfitidea点击:

Failed to load driver class com.mysql.jdbc.Driver

javamysqlspring-bootjdbcdriver

提问by Tomas Lukac

I am trying to run my Spring Boot backend with two profiles, one using H2 in memory database and the second one using MySQL. H2 database works just fine, but when I switch to MySQL I get

我正在尝试使用两个配置文件运行我的 Spring Boot 后端,一个在内存数据库中使用 H2,第二个使用 MySQL。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. The funny thing is that I have other project with MySQL database only, I had similar issue, but adding mysql-connector-javadependency solved it. I have no clue right now.

我尝试删除 .m2、重新导入、maven 清理、编译、安装以及我可以在互联网上找到的大部分内容,但没有成功。有趣的是,我有其他只有 MySQL 数据库的项目,我有类似的问题,但是添加mysql-connector-java依赖项解决了它。我现在没有头绪。

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.affinity.tuke</groupId>
    <artifactId>online-superstore</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>online-superstore</name>
    <description>Online superstore project for AffinityAnalytics.</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

数据源配置文件

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();
        }
    }
}

采纳答案by Tomas Lukac

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 的驱动程序行......显然,它无法识别该驱动程序。

回答by Andreas

You don't specify version of MYSQL JDBC driver, so you're likely getting version 8.x, where the driver is named differently than in previous versions:

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

com.mysql.cj.jdbc.Driver

com.mysql.cj.jdbc.Driver

回答by IKo

In my case the next dependency was missing:

在我的情况下,缺少下一个依赖项:

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

回答by Omid Rostami

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>

回答by Nitish Patil

Change the database driver dependency scope to 'runtime'

将数据库驱动程序依赖范围更改为“运行时”

For example:

例如:

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

回答by Od Chan

I had the same problem as you. For me, it was because of having h2 database dependency! 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!

我和你有同样的问题。对我来说,这是因为有 h2 数据库依赖!我不知道这两者是如何相互影响的,但我所做的只是消除了这种依赖关系,现在它可以正常工作了!

回答by Andreas

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 数据源驱动程序类名称设置。

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

回答by Ganesh Giri

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.Check More Details

尝试升级您的驱动程序。因为 Mysql 社区已将类名从com.mysql.jdbc.Driver更新为 com.mysql.cj.jdbc。查看更多详情