1

I've got the following class in my Spring Boot app:

@Configuration
public class JDBCTokenConfig {

    ...

    @Value("${spring.datasource.driver-class-name}")
    private String dbDriverClassName;

    @Bean
    public DataSource dataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(dbDriverClassName);
        dataSource.setUrl(datasourceUrl);
        dataSource.setUsername(dbUsername);
        dataSource.setPassword(dbPassword);
        return dataSource;
    }

I've also got the following in my pom.xml:

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

In my application.properties I have:

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

Yet I'm seeing the following error:

Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.cj.jdbc.Driver]

Is this the correct the drive class path? Or anything else?

2
  • 2
    You don't need dataSource.setDriverClassName(dbDriverClassName). Just comment it out along with the @Value Commented Dec 28, 2019 at 2:03
  • Thanks. I tried removing setDriverClassName and it seems to work fine too. That's great, less code :) Commented Dec 28, 2019 at 14:43

1 Answer 1

4

Try using com.mysql.jdbc.Driver

Spring uses the following preconfigured versions for dependencies when a version is not explicitly provided in the pom.xml: https://docs.spring.io/platform/docs/current/reference/htmlsingle/#appendix-dependency-versions

Here the following version of mysql-connector-java will be used . 5.1.47

For mysql-connector-java v5.1.47 the correct driver class is com.mysql.jdbc.Driver

You could also provide the dependency with version to use the latest driver : com.mysql.cj.jdbc.Driver

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.18</version>
</dependency>
Sign up to request clarification or add additional context in comments.

3 Comments

I have added latest version of mysql connector in pom as you mention. However, I still gets java.lang.IllegalStateException: Could not load JDBC driver class [com.mysql.cj.jdbc.driver] error. Here is connection code: DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.driver"); dataSource.setUrl("jdbc:mysql://xx.x.x.x:x/xxx"); dataSource.setUsername("xxx"); dataSource.setPassword("xxx!"); JdbcTemplate template = new JdbcTemplate(dataSource); SqlRowSet sqlRowSet = template.queryForRowSet("SELECT * FROM dbo.xxx;")
is the Driver name a typo ? com.mysql.cj.jdbc.Driver , is the correct Driver class name
future readers : "com.mysql.cj.jdbc.Driver" seems to go hand in hand with this dialect "org.hibernate.dialect.MySQL8Dialect". at least for '8.0.21'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.