java 使用 Hibernate 的问题 - 找不到 JDBC 驱动程序类:com.mysql.jdbc.Driver
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6378165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problems using Hibernate - JDBC Driver class not found: com.mysql.jdbc.Driver
提问by sfrj
I have a really strange issue when using hibernate to connect to a MySQLDB and add data.
使用 hibernate 连接到 MySQLDB 并添加数据时,我遇到了一个非常奇怪的问题。
This is the error I get:
这是我得到的错误:
JDBC Driver class not found: com.mysql.jdbc.Driver
找不到 JDBC 驱动程序类:com.mysql.jdbc.Driver
This is how my hibernate.cfg.xml looks like
这是我的 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>
I dont understand why I see a 500 Error when I navigate to the application; it says that the driver is not found.
我不明白为什么导航到应用程序时会看到 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
HTTP 错误 500
访问 /fpa-webapp/ 时出现问题。原因:
Exception constructing service 'ValueEncoderSource': Error invoking
服务构建器方法 org.apache.tapestry5.services.TapestryModule.buildValueEncoderSource(Map, InvalidationEventHub)(在 TapestryModule.java:2287)(对于服务“ValueEncoderSource”):调用服务贡献方法 org.apache.tapestry5.hibernate.HibernateModule. 时出错贡献值EncoderSource(MappedConfiguration,boolean,HibernateSessionSource,Session,TypeCoercer,PropertyAccess,LoggerSource):异常构造服务'HibernateSessionSource':调用服务构建器方法org.apache.tapestry5.hibernate.HibernateCoreModule.buildHibernateSessionSource(Logger,List,RegistryShutiberdCore)时出错.java:123)(对于服务“HibernateSessionSource”):找不到 JDBC 驱动程序类:com.mysql.jdbc.Driver
I'm sure the driver is in the class path.
我确定驱动程序在类路径中。
What it could be?
它可能是什么?
采纳答案by Matthew Farwell
Your driver is not on the classpath.
您的驱动程序不在类路径上。
There are two ways to ensure it's on the classpath:
有两种方法可以确保它在类路径上:
- Add it to the global lib directory. For Tomcat this is
TOMCAT_HOME/lib
. - Include it in the war.
- 将其添加到全局 lib 目录中。对于 Tomcat,这是
TOMCAT_HOME/lib
. - 把它包括在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.
如果您打算使用 Tomcat 来管理连接池,您需要将它添加到TOMCAT_HOME/lib
并且不是直接在休眠配置中定义您的数据源,而是通过jndi引用它。
回答by Rob Hruska
The only plausible explanation is that the Driver
class is not on the CLASSPATH.
唯一合理的解释是Driver
该类不在 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.
检查以确保 mysql-connector-java(或其他相关)jar 确实位于将要加载的位置。如果您 100% 肯定是这样,那么提供有关您如何知道类正在加载的更多信息可能会有所帮助,以便我们可以确定其他可能的原因。