3

I'm doing a simple spring boot project with a thread pool and MySQL in order to connect to MySQL whenever I'm adding spring-boot-starter-jdbc I'm getting below error.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.mysql.jdbc.Driver 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.s

Update 1:

    <dependencies>
            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

<dependency>
    <groupId>com.opencsv</groupId>
    <artifactId>opencsv</artifactId>
    <version>3.3</version>
</dependency>

    </dependencies>
2
  • 3
    please share your pom.xml you might be missing the mysql jdbc driver dependency Commented Dec 27, 2018 at 10:39
  • Please check updated post Commented Dec 27, 2018 at 10:46

4 Answers 4

2

It seems like you are missing mysql-connector dependency, Add these to your pom.

Maven:

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

or gradle:

compile "mysql:mysql-connector-java:*"
Sign up to request clarification or add additional context in comments.

1 Comment

I have added it
0

Looks like you forget to add dependency to MySQL (Spring Boot is using H2 database by default), you should add next lines to your pom.xml:

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

1 Comment

I have added mysql-connector-java
0

First include this in your pom file.

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

Second, clean install the maven project. If you are using eclipse check your JDK version and maven (Sometime jre is used instead of jdk, so, though in console everything looks fine but acutally the jar is not downloaded) in short .m2 repo should have this jar file when maven build is done.

3 Comments

at compile not I'm not getting error, I have used JdbcTemplate
sure , give me the link
0

Scope "runtime" is good for unit test and container as tomcat etc. when container provide jdbc driver. When run standalone apps (spring-boot) you should remove it or set to "compile".

5 Comments

'mvn dependency:tree'
I haven't install MAVEN locally
You can execute this goal in ide. Which one you use?
I'm using eclipse
I can see all required Jars are there

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.