5

I have a really strange issue when using hibernate to connect to a MySQLDB and add data.

This is the error I get:

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

This is how my hibernate.cfg.xml looks like

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/fpa-webapp</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password"></property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
    </session-factory>
</hibernate-configuration>

I dont understand why I see a 500 Error when I navigate to the application; it says that the driver is not found.

HTTP ERROR 500

Problem accessing /fpa-webapp/. Reason:

Exception constructing service 'ValueEncoderSource': Error invoking

service builder method org.apache.tapestry5.services.TapestryModule.buildValueEncoderSource(Map, InvalidationEventHub) (at TapestryModule.java:2287) (for service 'ValueEncoderSource'): Error invoking service contribution method org.apache.tapestry5.hibernate.HibernateModule.contributeValueEncoderSource(MappedConfiguration, boolean, HibernateSessionSource, Session, TypeCoercer, PropertyAccess, LoggerSource): Exception constructing service 'HibernateSessionSource': Error invoking service builder method org.apache.tapestry5.hibernate.HibernateCoreModule.buildHibernateSessionSource(Logger, List, RegistryShutdownHub) (at HibernateCoreModule.java:123) (for service 'HibernateSessionSource'): JDBC Driver class not found: com.mysql.jdbc.Driver

I'm sure the driver is in the class path.

What it could be?

5
  • 1
    "Im sure the driver is in the class path." How sure are you? :) Commented Jun 16, 2011 at 20:32
  • 1
    Is it a web-app or standalone, and how did you provide the classpath? Commented Jun 16, 2011 at 20:38
  • It is a web app. I am sure the driver is there because i opened the classpath file of the project and i saw the .xml tag there with the name of the driver. Commented Jun 16, 2011 at 23:51
  • I'll believe you if you run this code successfully from a ServletContextInitializer implementation: try { class.forName("com.mysql.jdbc.Driver").newInstance(); System.out.println("you win") ; } catch (Throwable t) { System.out.println("you lied: "+t); } Commented Jun 17, 2011 at 0:51
  • Could you tell me what you mean by running from a servletcontextInitializer? Because with the current configuration i have(see above) i get an HTTP Error 500 and i cannot access the app. You mean to run it from somewhere else right? Commented Jun 17, 2011 at 8:54

2 Answers 2

1

Your driver is not on the classpath.

There are two ways to ensure it's on the classpath:

  1. Add it to the global lib directory. For Tomcat this is TOMCAT_HOME/lib.
  2. Include it in the war.

It depends on your requirements which you use.

If you're going to use Tomcat to manage the connection pool, you'll need to add it to the TOMCAT_HOME/lib and instead of defining your datasource directly in the hibernate configuration, you'll reference it via jndi.

Sign up to request clarification or add additional context in comments.

Comments

0

The only plausible explanation is that the Driver class is not on the CLASSPATH.

Check to make sure that the mysql-connector-java (or other relevant) jar is indeed in a place where it will get loaded. If you're 100% positive that it is, it might help to provide more information about how you know the class is being loaded, so that we can identify other possible causes.

Comments

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.