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

I am working on a Spring project. I have faced a problem; i cannot connect to mysql driver. Here is pom.xml :

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

And here is application.properties code:

 spring.datasource.url=jdbc:mysql://localhost:3306/stock_produit
 spring.datasource.username=root
 spring.datasource.password=
 spring.jpa.hibernate.ddl-auto=create
 spring.datasource.driver-class-name=com.mysql.cj.jdbc 

Logcat:

   Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
   2019-02-12 01:40:47.353 ERROR 10086 --- [           main] o.s.boot.SpringApplication               : Application run failed.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
  org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is 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 
           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 
        Caused by: java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc 
        at org.springframework.util.Assert.state(Assert.java:94) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
        at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:224) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.initializeDataSourceBuilder(DataSourceProperties.java:176) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:43) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:83) ~[spring-boot-autoconfigure-2.1.2.RELEASE.jar:2.1.2.RELEASE]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_191]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_191]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_191]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_191]
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
        ... 43 common frames omitted

Any help is appreciated.

which database you trying to connect with your application. Please put the drivers accordingly.. – Vikrant Kashyap Feb 12, 2019 at 6:02

In application.properties driver class name that you defined is not found in spring data dependency. So,

I think you should try this..

spring.datasource.url=jdbc:mysql://localhost:3306/stock_produit
spring.datasource.driverClassName=com.mysql.jdbc.Driver

instead of

spring.datasource.url=jdbc:mysql://localhost:3306/stock_produit
spring.datasource.driver-class-name=com.mysql.cj.jdbc 
  

Note: You trying to connect with mysql database, which requires com.mysql.jdbc.Driver and it is available in spring-boot-data dependency.

Hope this help you out..

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.