如何在 Hibernate 中使用 DB2 JDBC 驱动程序

How to use DB2 JDBC driver with Hibernate

我不断得到

java.lang.ClassNotFoundException: Could not load requested class : com.ibm.db2.jcc.DB2Driver

尝试使用 Hibernate 连接到 DB2 数据库时。驱动程序 jar 被引用为外部库:

Image of Eclipse's "Referenced Library" folder

它也出现在class路径中:

classpathentry kind="lib" path="C:/Program Files (x86)/IBM/SQLLIB/java/db2jcc.jar"/>

我也可以通过在源代码中导入来访问 class。我的 persistence.xml 看起来如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
   <persistence-unit name="name" transaction-type="RESOURCE_LOCAL">
      <description>Persistence Unit</description>
      <provider>org.hibernate.ejb.HibernatePersistence</provider>

      <class>MyClass</class>

      <properties>
         <property name="javax.persistence.jdbc.driver" value="com.ibm.db2.jcc.DB2Driver" />
         <property name="javax.persistence.jdbc.url" value="jdbc:db2://url" />         
         <property name="javax.persistence.jdbc.user" value="user" />
         <property name="javax.persistence.jdbc.password" value="password" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.DB2Dialect" />

         <property name="hibernate.hbm2ddl.auto" value="create" />
         <property name="hibernate.show_sql" value="false" />
         <property name="hibernate.format_sql" value="true" />         
      </properties>
   </persistence-unit>
</persistence>

我怀疑这不起作用的原因是 class 包含在外部库中而不是作为 Maven 依赖项,因为当我用 net.ucanaccess.jdbc.UcanaccessDriver 替换 DB2 驱动程序时(包含在 Maven 包中),class 会很好地找到。

知道我做错了什么吗?

当您使用 com.ibm.db2.jcc.DB2Driver' 时,请确保 db2jcc.jardb2jcc_license_cu.jar 在您的 classpath.Please 中添加两个 jar类路径并试一试。

问题是我在 运行 主要 class 中使用 Maven exec 插件。 W/o 使用 Maven,它工作正常...