0

So I'm using JDBC MySQL 8.0.32, with Java 11 and Spring Boot 2.7.8. When I run "mvn spring-boot:run" it says "Trying to download driver" and I guess (and I'm sure) that it's because of com.mysql.cj.jdbc.Driver. Here's the POM.xml file:

<?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.7.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>medica3</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>medica3</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.25</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.25</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.32</version>
        </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-starter-data-jpa</artifactId>
        </dependency>


    </dependencies>

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

</project>

Here's the application.properties file:

spring.datasource.url=jdbc:mysql://localhost:3306/med
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

And here's where I make the connection:

import java.sql.Connection;
import java.sql.DriverManager;

public class Database {
    protected Connection con;
    private String url;
    private String user;
    private String pass;

    public Database() throws Exception{
        con = null;
        this.url = "jdbc:mysql://localhost:3306/med";
        this.user = "root";
        this.pass = "";
        Class.forName("com.mysql.cj.jdbc.Driver");

        con = DriverManager.getConnection(url, user, pass);
    }
}

I've also tried refreshing the project with maven, make clean install and invalidate caches, nothing works, it's still the same. I've also checked the classpath and everything's okay.

  • 1
    Just to note (not a fix): You don't need to explicitly use `Class.forName(...)` in this specific example. You can remove that line. ([ref.](https://stackoverflow.com/questions/28220724/class-fornamejdbc-driver-no-longer-needed)) – andrewJames Feb 04 '23 at 20:19
  • You're making configuration in your Spring config, but then don't actually use it. I recommend you consult the Spring Boot documentation how to use JDBC with Spring Boot. You shouldn't use `DriverManager.getConnection` in Spring Boot. – Mark Rotteveel Feb 06 '23 at 11:00

1 Answers1

-1

try com.mysql.jdbc.Driver driver. Yo are using .cj.jdbc.Driver which is dependent on <artifactId>mysql-connector-j</artifactId> I think and this is why Maven is trying to download it

Pioter88
  • 133
  • 1
  • 6
  • 1
    Didn't work. I think using "com.mysql.jdbc.Driver" is for the 5.x version of the connector and "com.mysql.cj.jdbc.Driver" is for the 8 version (mine) – Luis Rosúa Feb 04 '23 at 19:59
  • Just to note: `com.mysql.jdbc.Driver` still exists - but only for backwards compatibility with older versions of MySQL. Under the covers, it actually delegates to `com.mysql.cj.jdbc.Driver`. ([Ref.](https://github.com/mysql/mysql-connector-j/blob/fa4912a849140828e48162a2c396c8df0091bed7/src/legacy/java/com/mysql/jdbc/Driver.java#L35)) – andrewJames Feb 04 '23 at 20:16