添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Have you checked which version of MySQL connector do you have?

Since you haven't specified the version in your pom.xml, there is a chance that it pulled version 5 and now it is complaining about that .cj which is required for version 8.

That might be the reason why is it working without .cj ( com.mysql.jdbc.Driver ), because it pulled the version 5. Manually add the version in your pom.xml and keep .cj as it is.

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

After that make sure that you pull your dependencies. But make sure that you check in your project if you do have ONLY version 8 after "mvn clean install".

As of the picture that you shared "mysql version", that means nothing, the exception is regarding mysql-connector jar, it has nothing to do with the workbench, you can still have workbench version 5, and use mysql-connector jar version 8, it will make no difference.

In any version of mysql workbench ( 5 or 8 ):

mysql-connector 8 jar = requires .cj

mysql-connector 5 jar = does not require .cj

The only thing that you need to do is define the version of mysql-connector-java in your pom.xml

Fix the line that sets jdbc driver class as below: I don't think it matters but driver-class-name is more common. And check if your build file is up to date. Might as well try re-building using pom.xml

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

she already said "my project get error when using com.mysql.cj.jdbc.Driver but not com.mysql.jdbc.Driver." I don't that that renaming the key to driver-class-name would be helpful. – zawarudo Dec 28, 2021 at 12:46 com.mysql.jdbc.Driver -> com.mysql.cj.jdbc.Driver is relevant to mysql version and I didn't say anything about that. And I did say I don't think it matters but try if it fixes it. And I did say build it again because often that fixes it. And yeah there you go. As you can see from OP's answer it was simple build-again-and-fixed kind of deal. – jeff pentagon Dec 29, 2021 at 2:43

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.