0

I need to add a MySQL database in my springboot application(using Maven)

However when I run the project in Eclipse I get:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

I already checked all these:

  • The database, specified in the application.properties, exists
  • Credentials/ url in the application.properties are correct
  • Mysql server is running
  • The dependency mysql-connector-j is included in the pom.xml
  • mysql-connector-j-8.031.jar is included in the Maven Dependencies

I am using:

  • java version "17.0.6" 2023-01-17 LTS
  • Apache Maven 3.6.3
  • Eclipse Version: 2022-12 (4.26.0)

src/main/recources/application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/database_name
spring.datasource.username=
spring.datasource.password=
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=create
spring.jpa.show-sql=true

logging.level.org.hibernate.type.descriptor.sql=TRACE
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.springframework=DEBUG


pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <!-- The basics -->
    <modelVersion>4.0.0</modelVersion>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <groupId>com.example</groupId>
    <artifactId>MyProjectApplication</artifactId>
    <version>1.0.0</version>

    <!-- Dependencies -->
    <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</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-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
                
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
        </dependency>

    </dependencies>

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

</project>

MyProjectApplication.java

package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication()
public class MyProjectApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyProjectApplication.class, "--debug");
    }

}

I also follow the default spring structure.

src/
├── main/
│   ├── java/
│   │   └── com/
│   │       └── example/
│   │           └── myproject/
│   │               ├── MyProjectApplication.java
│   │               └── controllers/
│   │                   └── MyController.java
|   |               └── dao/
                    ...
                    ...  
│   ├── resources/
│   │   ├── application.properties
target/
pom.xml
  • Hi, there is a related question: https://stackoverflow.com/questions/51221777/failed-to-configure-a-datasource-url-attribute-is-not-specified-and-no-embedd. Hope it helps you. – fer.trecool Apr 10 '23 at 13:26
  • Hello. Everything looks good but please remove dependency spring-boot-starter-jdbc. Starter for data-jpa is enough and it includes JDBC autoconfig. – Mar-Z Apr 10 '23 at 14:00
  • 1
    are you able to connect to mysql database from client tool. Also please add logs – techprat Apr 10 '23 at 15:28

0 Answers0