使用 Hibernate 时出现问题 - 未找到 JDBC 驱动程序类:com.mysql.jdbc.Driver
当使用 hibernate 连接到 MySQLDB 并添加数据时,我遇到了一个非常奇怪的问题。
这是我得到的错误:
未找到 JDBC 驱动程序类: 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 错误 500
访问 /fpa-webapp/ 时出现问题。 原因:
构造服务“ValueEncoderSource”时出现异常:调用时出错
服务构建器方法 org.apache.tapestry5.services.TapestryModule.buildValueEncoderSource(地图, InvalidationEventHub)(位于 TapestryModule.java:2287)(用于服务 'ValueEncoderSource'): 调用时出错 服务贡献法 org.apache.tapestry5.hibernate.HibernateModule.contributeValueEncoderSource(MappedConfiguration, 布尔值,HibernateSessionSource, 会话、类型强制、PropertyAccess、 LoggerSource): 构造异常 服务“HibernateSessionSource”: 调用服务构建器方法时出错 org.apache.tapestry5.hibernate.HibernateCoreModule.buildHibernateSessionSource(记录器, 列表,RegistryShutdownHub)(位于 HibernateCoreModule.java:123)(对于 服务“HibernateSessionSource”): 未找到 JDBC 驱动程序类: com.mysql.jdbc.Driver
我确定驱动程序位于类路径中。
可能是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的驱动程序不在类路径上。
有两种方法可以确保它位于类路径上:
TOMCAT_HOME/lib
。这取决于您使用的要求。
如果您要使用 Tomcat 来管理连接池,则需要将其添加到 TOMCAT_HOME/lib 中,并且您将引用它,而不是直接在 hibernate 配置中定义数据源通过 jndi。
Your driver is not on the classpath.
There are two ways to ensure it's on the classpath:
TOMCAT_HOME/lib
.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.唯一合理的解释是
Driver
类不在 CLASSPATH 中。检查以确保 mysql-connector-java (或其他相关)jar 确实位于将要加载的位置。如果您 100% 确信确实如此,那么提供有关如何知道该类正在加载的更多信息可能会有所帮助,以便我们可以识别其他可能的原因。
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.