Unable to connect mysql to spring boot project

后端 未结 6 2020
庸人自扰
庸人自扰 2020-12-31 10:08

i am following this https://spring.io/guides/gs/accessing-data-mysql/ guide to connect mysql db to spring boot project

but getting following error when running the a

相关标签:
6条回答
  • 2020-12-31 10:11

    com.zaxxer.hikari.HikariDataSource requires spring-boot-starter-data-jdbc dependency, so add it to your pom.xml:

    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    
    0 讨论(0)
  • 2020-12-31 10:11

    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>
    
    0 讨论(0)
  • 2020-12-31 10:20

    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>
    
    0 讨论(0)
  • 2020-12-31 10:24

    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.

    0 讨论(0)
  • 2020-12-31 10:30

    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.

    0 讨论(0)
  • 2020-12-31 10:35

    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> `
    
    0 讨论(0)
提交回复
热议问题