Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - ClassNotFoundException: com.mysql.jdbc.Driver

Tags:

I'm trying to retrieve data from a MySQL database through Hibernate, but I'm stuck with this error:

Failed to create sessionFactory object.org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver could not be loaded

java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver
[...]

I use a class called DAOFactory to get the hibernate session:

public class DAOFactory {

    private static boolean isInstance = false;  
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry; 
    private static Session session;

    private DAOFactory() throws ExceptionInInitializerError{        
        if( !isInstance ) {
            try {               
                Configuration cfg   = new Configuration().configure();              
                serviceRegistry     = new ServiceRegistryBuilder().applySettings(cfg.getProperties())
                                                .buildServiceRegistry();
                sessionFactory      = cfg.buildSessionFactory(serviceRegistry);
            } catch (Throwable ex) {
                System.err.println("Failed to create sessionFactory object."+ ex);
                throw new ExceptionInInitializerError(ex);
            }
            session = sessionFactory.openSession();         
            isInstance = true ;
        }               
    }

    public static DAOFactory getInstance() {        
        return new DAOFactory() ;
    }

    public Session getSession() {
        return session ;
    }
}

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="">
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/enigma</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.pool_size">1</property>
        <property name="current_session_context_class">thread</property>
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
    </session-factory>
</hibernate-configuration>

And mysql-connector-java-5.1.26-bin.jar is already in the classpath:

classpath

Does anyone see what I'm missing ?

like image 994
Arthur Avatar asked Oct 11 '13 13:10

Arthur


People also ask

How do I add MySQL connector jar to classpath?

You need to add the downloaded Java MySQL Connector JAR in client project's classpath . To do this, right click on your Java Project (JDBCMySQLSample) -> Properties -> Buildpath -> Libraries -> Add External JAR and select “mysql-connector-java-5.1. 14-bin. jar” JAR file.

What is COM MySQL JDBC driver?

JDBC (Java Database Connectivity) is a programming interface that lets Java applications access a relational database. SuperCHANNEL needs a JDBC driver so that it can access the relational database system (e.g. SQL Server, Oracle, etc) where your source data is stored.


2 Answers

Thanks to Reimeus for the answer. mysql-connector-java-5.1.26-bin.jar needs to be in the runtime classpath.

Run -> Run Configurations... -> Classpath -> Add external JAR.

Clean everything, try again, and the Exception is gone.

like image 190
Arthur Avatar answered Oct 21 '22 05:10

Arthur


For those who use Maven: add the following dependency in pom.xml.

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

or choose another version from here.

Then you can get the artifact using:

mvn dependency:resolve

(if you don't use the IDE).

like image 22
ROMANIA_engineer Avatar answered Oct 21 '22 05:10

ROMANIA_engineer