Unable to connect mysql to spring boot project

こ雲淡風輕ζ 提交于 2019-11-30 19:18:05

It seems like the application may not be running with the correct classpath as it seem to be having difficulty locating the mysql driver. Are you trying to run the application through the IDE?

Try running the application via maven using the command line from the project's directory:

./mvnw spring-boot:run

If this works then it's a problem with the IDE, for some reason it's not providing the correct classpath when running your application. I'm not familiar with STS but in IntelliJ there are options like "auto import pom" to keep the IDE's view of the project in sync with the pom.

Try to add mysql connector driver version and remove scope tag from:

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

FYI, I encountered a similar problem when I changed my Spring Boot app from H2 to mySQL.

I was using Gradle (instead of Maven). I, too, was using STS.

I simply forgot to run Refresh Gradle Project after I added this to my build.gradle file:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    ...
    // Use MySQL Connector-J
    compile 'mysql:mysql-connector-java'

The problem went away after doing a "Refresh Gradle Project" and rebuilding/restarting my app.

Maybe you missing MySQL connector dependency so add it into dependencies tag on pom.xml file and run the project again. It may fix your problem.

    `<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency> `
Rawal Santosh

Update the pom.xml file with DB version and the database:

<dependency> 
    <groupId>mysql</groupId> 
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.44</version>
</dependency>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!