在使用 hibernate 连接到 MySQLDB 并添加数据时,我遇到了一个非常奇怪的问题。
这是我得到的错误:
JDBC Driver class not found: com.mysql.jdbc.Driver
这就是我的 hibernate.cfg.xml 的样子
<!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>
我不明白为什么在导航到应用程序时会看到 500 错误;它说找不到驱动程序。
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
我确定驱动程序在类路径中。
它可能是什么?
最佳答案
您的驱动程序不在类路径中。
有两种方法可以确保它在类路径上:
- 将其添加到全局库目录。对于 Tomcat,这是
TOMCAT_HOME/lib
。 - 将其纳入 war 。
这取决于您使用的要求。
如果你打算使用 Tomcat 来管理连接池,你需要将它添加到 TOMCAT_HOME/lib
而不是直接在 hibernate 配置中定义你的数据源,你'我会通过 jndi 引用它.
关于java - 使用 Hibernate 的问题 - 未找到 JDBC 驱动程序类 : com. mysql.jdbc.Driver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6378165/