【问题标题】:Cannot load driver class: com.mysql.cj.jdbc Spring boot无法加载驱动类:com.mysql.cj.jdbc Spring boot
【发布时间】:2019-02-12 14:05:24
【问题描述】:

我正在做一个 Spring 项目。我遇到了一个问题;我无法连接到 mysql 驱动程序。 这是 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>

这里是 application.properties 代码:

 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

感谢任何帮助。

【问题讨论】:

  • 您尝试将哪个数据库与您的应用程序连接。请相应地放置驱动程序..

标签: spring-boot


【解决方案1】:

application.properties 中,您定义的驱动程序类名在 spring 数据依赖项中找不到。所以,

我觉得你应该试试这个..

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

而不是

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

注意:您尝试连接 mysql 数据库,这需要 com.mysql.jdbc.Driver 并且它在 spring-boot-data 依赖项中可用。

希望对你有所帮助..

【讨论】:

    【解决方案2】:

    如果您使用 MySQL 服务器 8 和连接器 j8.0,则使用此类名称 - com.mysql.cj.jdbc.Driver

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 2018-01-31
      • 2014-10-14
      • 2020-01-29
      • 2020-09-17
      • 1970-01-01
      • 2023-03-23
      • 2015-03-01
      相关资源
      最近更新 更多