1

I created a db named: books_management_system and then with spring book app should be generate table with four columns, butI can not connect MySQL with Spring Boot App, here is applicaton.properties file and pom.xml, I think xml file is okay but maybe the problem is in app.properties file, please give help me

spring.datasource.url=jdbc:mysql://localhost:3306/books_management_system
spring.datasource.username=root
spring.datasource.password=ivana12345
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = update
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.books</groupId>
    <artifactId>springboot-books-management-system</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-books-management-system</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>16</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
Ivana
  • 35
  • 3

3 Answers3

1

In your new post now you don't have any driver class name. I have created a example which is working fine.-->

#database properties
spring.datasource.url=jdbc:mysql://localhost:3306/smartcontactmanager
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update

I have used mysql version 8.0.21

Sharman Jeurkar
  • 136
  • 1
  • 4
  • 13
0

Use this

spring.database.driverClassName= com.mysql.jdbc.Driver

Instead of

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

And also add this dependency

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

Instead of

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

I would recommend you to first state the version of of your MySQL dependency. This is because the driver package name changes according to the version. After 8.0.14 the package name becomes com.mysql.cj.jdbc.Driver and below it it is com.mysql.jdbc.Driver. So in your pom you should add the version. As the version may not come from parent. And also to be safe try and add versions to all your dependancies.

Sharman Jeurkar
  • 136
  • 1
  • 4
  • 13