使用 Hibernate 时出现问题 - 未找到 JDBC 驱动程序类:com.mysql.jdbc.Driver

sfr*_*frj 5 java mysql hibernate 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>
Run Code Online (Sandbox Code Playgroud)

我不明白为什么当我导航到应用程序时会看到 500 错误;它说找不到驱动程序。

HTTP 错误 500

访问 /fpa-webapp/ 时出现问题。原因:

Exception constructing service 'ValueEncoderSource': Error invoking
Run Code Online (Sandbox Code Playgroud)

服务构建器方法 org.apache.tapestry5.services.TapestryModule.buildValueEncoderSource(Map, InvalidationEventHub) (位于 TapestryModule.java:2287)(对于服务“ValueEncoderSource”):调用服务贡献方法 org.apache.tapestry5.hibernate.HibernateModule 时出错。贡献ValueEncoderSource(MappedConfiguration,boolean,HibernateSessionSource,Session,TypeCoercer,PropertyAccess,LoggerSource):构造服务'HibernateSessionSource'时出现异常:调用服务构建器方法org.apache.tapestry5.hibernate.HibernateCoreModule.buildHibernateSessionSource(Logger,List,RegistryShutdownHub)时出错(位于HibernateCoreModule) .java:123)(对于服务“HibernateSessionSource”):找不到 JDBC 驱动程序类:com.mysql.jdbc.Driver

我确信驱动程序位于类路径中。

可能是什么?

Mat*_*ell 1

您的驱动程序不在类路径上。

有两种方法可以确保它位于类路径上:

  1. 将其添加到全局 lib 目录中。对于 Tomcat 来说,这是TOMCAT_HOME/lib.
  2. 将其纳入战争。

这取决于您使用的要求。

如果您要使用 Tomcat 来管理连接池,则需要将其添加到,而不是直接在 hibernate 配置中定义数据源,而是通过jndiTOMCAT_HOME/lib引用它。