无法加载驱动程序类com.mysql.jdbc.Driver

时间:2018-10-14 15:26:45

标签: java mysql spring-boot jdbc driver

我试图用两个配置文件运行我的Spring Boot后端,一个配置文件在内存数据库中使用H2,第二个配置文件使用MySQL。 H2数据库工作正常,但是当我切换到MySQL时,我得到了

double

我曾尝试删除.m2,重新导入,进行maven清理,编译,安装以及大多数我可以在Internet上找到的东西,但都没有成功。有趣的是,我只有MySQL数据库的其他项目,我有类似的问题,但是添加 mysql-connector-java 依赖性解决了它。我现在不知道了。

application.properties

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

application-local_mysql.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

pom.xml

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

DatasourceConfig.java

<?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>

13 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:3)

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

com.mysql.cj.jdbc.Driver

答案 2 :(得分:2)

在我看来,错误抛出:

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

原因:无法在HikariConfig类加载器或线程上下文类加载器中加载驱动程序类com.mysql.cj.jdbc.Driver

所以我刚刚添加了mysql依赖项:

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

尝试升级驱动程序。 Coz Mysql社区已将类名称从 com.mysql.jdbc.Driver更新为com.mysql.cj.jdbc。 Check More Details

答案 3 :(得分:1)

答案是如此尴尬。我在驱动器行的application.properties后面加上了分号... 显然,它无法识别该驱动程序。

答案 4 :(得分:1)

简直不敢相信! 在 intellij:Build->rebuild project 为我解决了这个问题!

enter image description here

更多信息:

pom.xmlenter image description here

application.properties: enter image description here

答案 5 :(得分:0)

只需添加如下所示的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>

答案 6 :(得分:0)

change database driver dependency scope to 'runtime'

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

答案 7 :(得分:0)

我和你有同样的问题。对我而言,这是因为具有h2数据库依赖性! 我不知道这两个如何相互影响,但是我所做的只是删除了此依赖关系,现在一切正常!

答案 8 :(得分:0)

我在使用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>

由于Spring Boot期望使用更新的mysql-java-connector(已重命名为com.mysql.cj.jdbc.Driver),因此我还必须在spring boot db中添加spring数据源驱动程序类名称设置配置。

所以我的spring boot配置最终如下:

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

答案 9 :(得分:0)

必须在项目->属性-> JPA->连接配置文件-> JAR列表中指定jar路径

see screen shot

答案 10 :(得分:0)

您应该将:spring.datasource.driver-class-name=com.mysql.jdbc.Driver 添加到您的 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

答案 11 :(得分:0)

我的情况下所有配置都正确,但我仍然收到此错误。

在 Intellij 中,进入 windows 中的项目结构 (ctrl + alt + maj + S)。 看看你有没有遇到什么问题。

如果是,请继续在侧栏中的 ma​​ven:点击“为所有项目生成源和更新文件夹”

那解决了我的错误!

答案 12 :(得分:0)

对我来说这个问题也是由mysql的版本引起的。 我所要做的就是在 pom.xml 依赖项中添加 mysql 的版本 (在我的情况下,版本是 8.0.25)

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

确保在运行前重新加载 Maven 依赖

相关问题