Home > other >  "Trying to load driver" when I add "com.mysql.cj.jdbc.Driver"
"Trying to load driver" when I add "com.mysql.cj.jdbc.Driver"

Time:02-05

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.

CodePudding user response:

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

CodePudding user response:

Your dependency seems incorrect

Try this one note that group Id is changed

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.0.32</version>
</dependency>
  • Related