Failed to load driver class com.mysql.jdbc.Driver
Asked Answered
F

19

49

I am trying to run my Spring Boot backend with two profiles, one using H2 in memory database and the second one using MySQL. H2 database works just fine, but when I switch to MySQL I get

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

Description:

Failed to bind properties under '' to com.zaxxer.hikari.HikariDataSource:

    Property: driverclassname
    Value: com.mysql.jdbc.Driver;
    Origin: "driverClassName" from property source "source"
    Reason: Failed to load driver class com.mysql.jdbc.Driver; in either of HikariConfig class loader or Thread context classloader

Action:

Update your application's configuration

I have tried deleting .m2, reimporting, maven clean, compile, install and most of the things I could find on the internet, no success. The funny thing is that I have other project with MySQL database only, I had similar issue, but adding mysql-connector-java dependency solved it. I have no clue right now.

application.properties

spring.profiles.active=@profilename@

#H2 in memory database
domain.datasource.type=H2
domain.datasource.url=jdbc:h2:mem:store;MODE=MYSQL;
domain.datasource.driver-class=org.h2.Driver
domain.datasource.username=sa
domain.datasource.password=
domain.datasource.generate-dll=true

application-local_mysql.properties

spring.profiles.active=@profilename@

#MySQL local database
domain.datasource.type=MYSQL
domain.datasource.url=jdbc:mysql://localhost:3600/store;
domain.datasource.driver-class=com.mysql.jdbc.Driver;
domain.datasource.username=store
domain.datasource.password=store
domain.datasource.generate-dll=false

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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sk.personal</groupId>
    <artifactId>my-project</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>my-project</name>
    <description>My personal project.</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>

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

    </dependencies>

    <profiles>
        <profile>
            <id>local_h2</id>
            <properties>
                <profilename>local_h2</profilename>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>local_mysql</id>
            <properties>
                <profilename>local_mysql</profilename>
                <maven.test.skip>true</maven.test.skip>
            </properties>
        </profile>
    </profiles>

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

DatasourceConfig.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

import javax.sql.DataSource;

@Configuration
public class DatasourceConfig {

    @Value("${domain.datasource.url}")
    private String url;

    @Value("${domain.datasource.username}")
    private String username;

    @Value("${domain.datasource.password}")
    private String password;

    @Value("${domain.datasource.type}")
    private String type;

    @Value("${domain.datasource.driver-class}")
    private String driverClass;

    @Bean
    public DataSource dataSource() {
        if (type.equals("MYSQL")) {
            return DataSourceBuilder
                    .create()
                    .username(username)
                    .password(password)
                    .url(url)
                    .driverClassName(driverClass)
                    .build();
        } else {
            EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
            return builder
                    .setType(EmbeddedDatabaseType.H2)
                    .build();
        }
    }
}
Floccose answered 14/10, 2018 at 15:26 Comment(0)
F
17

The answer is so embarrassing. I appended the driver line of application.properties with a semicolon ... Obviously, it did't recognize that driver.

Floccose answered 15/10, 2018 at 20:13 Comment(1)
Embarrassing for me too :)Oldenburg
A
85

In my case the next dependency was missing:

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

In case of using IntelliJ and if you inherit from a <parent>, you can view your effective pom.xml by right clicking anywhere inside your pom.xml, then: enter image description here

and search for the mysql-connector-java artifact as mentioned.

Allot answered 14/3, 2019 at 8:18 Comment(3)
So in my case. The dependency was just missingStauffer
Gradle equivalent: runtimeOnly 'mysql:mysql-connector-java'Dunno
amigos :) this works of courseShavers
F
17

The answer is so embarrassing. I appended the driver line of application.properties with a semicolon ... Obviously, it did't recognize that driver.

Floccose answered 15/10, 2018 at 20:13 Comment(1)
Embarrassing for me too :)Oldenburg
C
15

I had a problem where I was using Spring Boot 2.2.0.RELEASE and needed to connect to an old Mysql DB (5.1.73), which required me to downgrade to mysql-connector-java version 5.1.38

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

Since Spring boot was expecting a newer mysql-java-connector, which has been renamed to com.mysql.cj.jdbc.Driver, I also had to add the spring datasource driver-class-name setting in my spring boot db config.

So my spring boot config ended up like this:

spring:
  datasource:
   url: 'localhost'
   password: password
   username: user
   driver-class-name: com.mysql.jdbc.Driver
Chatter answered 5/12, 2019 at 13:54 Comment(0)
V
11

just add mysql and jdbc dependencies like below

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
Viniferous answered 6/5, 2019 at 9:35 Comment(1)
After searching for a while, i got it working using "spring-boot-starter-data-jdbc". ThanksBosomed
C
4

You don't specify version of MYSQL JDBC driver, so you're likely getting version 8.x, where the driver is named differently than in previous versions:

com.mysql.cj.jdbc.Driver

Chatter answered 14/10, 2018 at 15:32 Comment(2)
I've had connector of version 5.1.47 and it didn't work so I've specified dependency version to 8.0.12, but still, Failed to load driver class com.mysql.cj.jdbc.Driver; in either of HikariConfig class loader or Thread context classloaderFloccose
The version 8.0 of the driver still contains the com.mysql.jdbc.Driver for backwards compatibility.Aftonag
P
4

In my case error throws:

Property: driverclassname
Value: com.mysql.cj.jdbc.Driver
Origin: "driverClassName" from property source "source"

Reason: Failed to load driver class com.mysql.cj.jdbc.Driver in either of HikariConfig class loader or Thread context classloader

So I have just added mysql dependency:

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

Try upgrading your driver. Coz Mysql community has updated class name from com.mysql.jdbc.Driver to com.mysql.cj.jdbc. Check More Details

Pfennig answered 4/3, 2020 at 3:6 Comment(0)
C
2

Change the database driver dependency scope to 'runtime'

For example:

   <dependency>
        <groupId>com.{yourDatabaseGroupid}</groupId>
        <artifactId>{yourDatabaseArtifactId}</artifactId>
        <scope>runtime</scope>
    </dependency>
Carminacarminative answered 24/6, 2019 at 17:56 Comment(0)
D
2

You should add: spring.datasource.driver-class-name=com.mysql.jdbc.Driver to your application.properties file .

My **application.properties : **

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?useSSL=false&useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
Desired answered 16/2, 2021 at 20:36 Comment(0)
L
2

this issue for me was also caused by the version of mysql. all i had to do is add the version of mysql in pom.xml dependencies (in my case the verion is 8.0.25)

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

make sure to reload the maven dependecies before your run

Laniferous answered 15/6, 2021 at 9:21 Comment(0)
C
1

I cant believe it! In intellij: Build->rebuild project solved the issue for me!

enter image description here

For more information:

pom.xml: enter image description here

application.properties: enter image description here

Currency answered 18/5, 2021 at 13:20 Comment(0)
I
1

Spring boot 3.xx has updated its dependency

try this

runtimeOnly("com.mysql:mysql-connector-j")
<dependencies>
  <dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <scope>runtime</scope>
  </dependency>
</dependencies>
Indissoluble answered 2/4, 2023 at 2:54 Comment(0)
S
0

I had the same problem as you. For me, it was because of having h2 database dependency! I don't know how these two can affect each other, but all I did was removing this dependency and now it works just fine!

Schopenhauer answered 19/10, 2019 at 13:19 Comment(0)
B
0

Had to specify the jar path inside project->Properties->JPA-> connection Profile -> JAR List

configuration screen shot

Boffa answered 23/9, 2020 at 23:46 Comment(0)
M
0

I my case all configurations were correct but I still get this error.

In Intellij, go in the project structure (ctrl + alt + maj + S) in windows. Look if u get some problems.

If yes, go on maven in the sidebar: clic on "Generate sources and Update Folders for All projetct"

That resolve my error !

Machination answered 12/4, 2021 at 20:45 Comment(0)
S
0

I had the same issue and the root cause was [ spring.profile.active ] and the fix is [ spring.profiles.active ] where it is profiles plural and it was my mistake.

Sickener answered 29/8, 2022 at 1:24 Comment(0)
P
0

go to https://start.spring.io In that site: Dependencies > Search for Mysql drver > select it > Click explore button > copy the latest version of mysql connector > add it into your application.properties file

Pillow answered 26/10, 2022 at 16:33 Comment(0)
S
0

/I solved it just by placing these two lines./

implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
Slacken answered 25/7, 2023 at 17:51 Comment(0)
P
0

Can't comment, here's another updated alternative (to @Janac Meena's comment) for Gradle

implementation 'com.mysql:mysql-connector-j:8.1.0'

Check for more versions here. My mistake was spelling the above as mysql-connector-java:8.1.0 instead of mysql-connector-j:8.1.0.

Presurmise answered 14/9, 2023 at 7:3 Comment(0)
I
0

For me the issue was that:

  1. I failed to add the MySQL connector-j dependency:

    <dependency>
        <groupId>com.mysql</groupId>
        <artifactId>mysql-connector-j</artifactId>
        <scope>runtime</scope>
    </dependency>
    
  2. I didn't correctly specify my database URL and configuration in my application.properties file:

  3. While not necessary you can also add the driver-class-name in the application.properties file

    spring.datasource.url=jdbc:mysql://localhost:3306/database-name
    spring.datasource.username=database-User-name
    spring.datasource.password=your-database-password
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    
    
Impellent answered 4/2 at 9:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.