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

xwbd5t1u  于 2023-01-24  发布在  Spring
关注(0)|答案(5)|浏览(227)

My Spring Boot Project trying connect to MYSQL database with driver mysql-connector-java . I have import newest mysql driver and spring-boot-starter-data-jpa

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

I have configured database connection in application.properties file

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=somethingfunny
spring.datasource.password=somethingfunny
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true

MYSQL version is 8.0.26

Spring boot version 2.6.2

When run project with Intellij I get error
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.14.jar:5.3.14] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.14.jar:5.3.14] ... 35 common frames omitted Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver at org.springframework.util.Assert.state(Assert.java:97) ~[spring-core-5.3.14.jar:5.3.14] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:241) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:193) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:48) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:90) ~[spring-boot-autoconfigure-2.6.2.jar:2.6.2] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na] at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.14.jar:5.3.14] ... 36 common frames omitted
I have refered the post about Cannot load driver class: com.mysql.jdbc.Driver (NOT com.mysql.cj.jdbc.Driver), I cannot apply for my project because my project get error when using com.mysql.cj.jdbc.Driver but not com.mysql.jdbc.Driver .
I also refered this post Cannot load driver class: com.mysql.cj.jdbc.Driver . But i can not find correct answer(the answer is marked corrected) for this error.
How to fix this error ?

w8rqjzmb

w8rqjzmb1#

1)在pom.xml中使用此依赖项

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

2)在www.example.com文件中使用此属性application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
lqfhib0f

lqfhib0f2#

我已经修复了这个问题。在Intellij中右键单击项目,选择maven,选择重新加载项目。现在Intellij将添加mysql驱动程序到项目中

55ooxyrt

55ooxyrt3#

与上面的答案https://stackoverflow.com/a/70514438/14111809不同,但使用Gradle而不是Maven(在IntelliJ IDE中),这为我解决了这个问题:

egmofgnx

egmofgnx4#

您是否检查过您使用的MySQL连接器版本?
因为您没有在pom.xml中指定版本,所以它有可能拉到版本5,现在它抱怨版本8所需的.cj。
这可能就是它没有.cj(com.mysql.jdbc.Driver)也能工作的原因,因为它拉了版本5。手动将版本添加到您的pom.xml中,并保持.cj不变。

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

之后,请确保您拉出您的依赖项。但请确保您签入您的项目,如果您确实只有版本8后,“mvn清洁安装”。
至于你分享的图片“mysql版本”,这意味着什么,例外是关于mysql-connector jar,它与工作台无关,你仍然可以有工作台版本5,并使用mysql-connector jar版本8,这将没有什么区别。
在任何版本的mysql workbench(5或8)中:
mysql-连接器8 jar =需要.cj
mysql-连接器5 jar =不需要.cj
您唯一需要做的事情就是在pom.xml中定义mysql-connector-java的版本

8yparm6h

8yparm6h5#

修复设置jdbc驱动程序类的行,如下所示:我不认为这有什么关系,但是driver-class-name更常见。并且检查您的构建文件是否是最新的。不妨尝试使用pom.xml重新构建
数据源驱动程序类名=com.mysql.cj.jdbc.驱动程序

相关问题