Springboot + MySQL + Driver Class Not Found

13 浏览
0 Comments

Springboot + MySQL + Driver Class Not Found

我正在进行一个简单的Spring Boot项目,其中包含一个线程池和MySQL,以便在添加spring-boot-starter-jdbc时,每当我连接到MySQL时,我都会遇到以下错误。

org.springframework.beans.factory.BeanCreationException: 在类路径资源[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]中定义的名为'dataSource'的bean创建失败:通过工厂方法实例化失败;内嵌异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.apache.tomcat.jdbc.pool.DataSource]:工厂方法'dataSource'抛出异常;内嵌异常是java.lang.IllegalStateException:无法加载驱动程序类:com.mysql.jdbc.Driver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.s

更新1:

    
            
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            mysql
            mysql-connector-java
            runtime
        

    com.opencsv
    opencsv
    3.3

    

0
0 Comments

原因:缺少mysql-connector依赖

解决方法:在pom.xml(Maven项目)或build.gradle(Gradle项目)中添加以下依赖:

Maven依赖:

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

Gradle依赖:

compile "mysql:mysql-connector-java:*"

0
0 Comments

在使用Spring Boot连接MySQL数据库时,可能会遇到"Driver Class Not Found"的错误。这个问题的出现原因是缺少MySQL驱动程序依赖。解决方法是在项目的pom文件中添加mysql-connector-java的依赖,并执行clean install命令来安装maven项目。如果使用的是Eclipse,还需要检查JDK版本和maven配置,有时候可能会将JRE误用为JDK,导致在控制台上看起来一切正常,但实际上未下载相应的jar文件。在编译时不会出现错误,是因为使用了JdbcTemplate,但在运行时会出现"Driver Class Not Found"的错误。

以下是解决该问题的一些有用链接:

- [stackoverflow.com/a/36079859/5086633](https://stackoverflow.com/a/36079859/5086633)

- [stackoverflow.com/questions/28821521](https://stackoverflow.com/questions/28821521)

- [stackoverflow.com/questions/28042426](https://stackoverflow.com/questions/28042426)

0
0 Comments

to my pom.xml in the dependencies section, but I still get the error "Driver class not found". What could be the issue?

0