pulse
pulse

Reputation: 83

Specified JDBC Driver com.mysql.jdbc.Driver class not found

I am using eclipse juno and hibernate 4.1.6, mysql connector 5.1.24, jboss 7.1.1.

I tried this and this(as a module).

I have the connector JAR in WEB-INF/lib folder, and I also tried once without it. Everything failed. I don't know anymore how I can solve this problem. The whole stack trace:

18:35:44,284 ERROR [stderr] (http-localhost-127.0.0.1-8080-1) Error creating Session: org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver com.mysql.jdbc.Driver class not found

The connector is an the class path, it's in the Maven Dependencies Library. I put it in the lib folder and in the System Library...nothing works.

public class HibernateUtil {

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

static
{
    try
    {
        Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
        serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    }
    catch (HibernateException he)
    {
        System.err.println("Error creating Session: " + he);
        throw new ExceptionInInitializerError(he);
    }
}

Upvotes: 0

Views: 9279

Answers (4)

Navin Ketu
Navin Ketu

Reputation: 11

Please check your mysql connector version is compatible with mysql db installed on your system. e.g. mysql connector version 5.1.26 is compatible with mysql 5.6.25 while 5.1.34 is not.

Upvotes: 1

hd1
hd1

Reputation: 34677

Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties());
sessionFactory = configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry());
// end of static block

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}

Upvotes: 0

Raul Rene
Raul Rene

Reputation: 10280

Check in your hibernate.cfg.xml file that you have correctly specified the driver class and the dialect:

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

(these are properties of the <session-factory>)

Then, simply build your Session Factory:

sessionFactory = new Configuration().configure().buildSessionFactory();

If the application still does not see your Maven dependency for the connector, try doing a clean install, and after it check in the list of libraries for your project that the MySQL Connector appears there.

Upvotes: 3

roger_that
roger_that

Reputation: 9791

Try to add the connector to build path as external jar OR , create a lib folder and put connector jar in it and then Add Library to your build path and give that connector lib folder path.

Upvotes: 0

Related Questions