JDBC Driver class not found: com.mysql.jdbc.Driver
Asked Answered
G

7

13

I am developing a web application using maven spring and hibernate and I need to create schema using hibernate for which I had the following in my pom.xml to connect to MySQL 5.5 database.

    <!-- MySql 5.5 Connector -->   
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.15</version>
    </dependency>

This imported mysql-connector-java-5.1.15.jar in my Libraries under Maven Dependencies but when I try to connect to database it gives me Exception in thread "main" org.hibernate.HibernateException: JDBC Driver class not found: com.mysql.jdbc.Driver.

I have done this like gazillions of times, but I did it when I din't used to use Maven to manage my dependencies and build the project. I just used to have it the same jar file in the lib folder of a Dynamic Web Project in Eclipse IDE.

Could someone tell me what am I missing here and what else I need to have this jar in my build path?

Thanks.

Givens answered 8/1, 2012 at 17:32 Comment(0)
G
16

To start with, the jar that I need to connect to MySQL 5.5 should have been mysql-connector-java-5.1.15-bin.jar but not mysql-connector-java-5.1.15.jar. Secondly, this jar is not available in maven repository so I needed to manually add it to my local maven repository and then added it as a dependency in my pom.xml.

Adding mysql-connector-java-5.1.15-bin.jar to the local maven repository by

mvn install:install-file -Dfile=C:\Libraries\mysql-connector-java-5.1.15-bin\mysql-connector-java-5.1.15-bin.jar -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=5.1.15-bin -Dpackaging=jar

and then adding the following dependency to pom.xml of the project.

    <!-- MySql 5.5 Connector -->   
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.15-bin</version>
    </dependency> 
Givens answered 8/1, 2012 at 19:19 Comment(1)
JARs without the -bin suffix from the Maven repository work just fine, I'm using 5.1.20 myself. Perhaps is not the name of the JAR, which really doesn't matter, but the content that changed from the first one you used to the second. Clarifying this would probably make the answer a little bit better.Beekman
F
6

Thanks for the answers above - just a comment for NetBeans users: (names to be replaced with your versions)

  1. The mysql JAR can be downloaded here
  2. mvn executable can be found at c:\Program Files\NetBeans 7.2.1\java\maven\bin
  3. run set JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_10
  4. Then the above command would work: mvn install:install-file -Dfile=mysql-connector-java-5.1.15-bin.jar -DgroupId=mysql -DartifactId=mysql-connector-java -Dversion=5.1.15-bin -Dpackaging=jar
  5. installed package could be found in m2 repository in your home folder
Ferrocyanide answered 20/12, 2012 at 23:29 Comment(0)
D
3

My working solution

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

adding above dependency to pom.xml works for me...

Devastation answered 24/9, 2015 at 7:37 Comment(0)
J
2

Unzip your generated JAR file. There is a folder WEB-INF in. Then check if mysql-connector-java-5.1.15.jar exists in this WEB-INF/lib.

Jewel answered 8/1, 2012 at 18:2 Comment(5)
The application structure has been made by maven for java webapp, why should the maven dependency jars be in lib folder inside WEB-INF? You din't mean a WEB-INF folder in the unzipped mysql-connector-java-5.1.15.jar, did you? What am I missing?Givens
It gives me the same error even if I configure the build path manually by adding mysql-connector-java-5.1.15.jar to the build path, which I am not sure if I should have even done that with the maven webapp project as with maven I see the dependencies in the Java Resources.Givens
I am just generating the schema, I don't think lib folder in WEB-INF should have anything to do with it? I can't believe I am stuck at it.Givens
I mean the WEB-INF/lib folder in the unzipped web-app.Jewel
I was mistakenly using the wrong jar file. I've got it working now. Thanks :)Givens
R
0

Add belowcode to pom.xml file

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
    </dependency>
Refulgent answered 24/7, 2017 at 13:41 Comment(0)
S
0

Check the MySql version using SELECT version() and then check dependency of mysql version in pom.xml

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

application.properties files:

spring.datasource.url=jdbc:mysql://localhost:3306/db_name

spring.datasource.username=root
spring.datasource.password=

spring.datasource.testWhileIdle=true
spring.datasource.validationQuery = SELECT 1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true

spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.dialact=org.hibernate.dialact.MySQL5Dialact 
Squireen answered 27/1, 2019 at 12:7 Comment(0)
N
0

Put the .jar file in the folder LIB (/lib) if you are using TOMCAT. Don't forget to restart the server.

Nathan answered 4/6, 2020 at 6:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.