简体   繁体   中英

How to use DB2 JDBC driver with Hibernate

I keep getting

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

when trying to connect to a DB2 database using Hibernate. The driver jar is referenced as an external library:

Image of Eclipse's "Referenced Library" folder

It also shows up in the classpath:

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

I can also access the class by importing it in the source code. My persistence.xml looks as follows:

<?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>

My suspicion why this does not work is that the class is contained in an external library and not as a maven dependency, because when I replace the DB2 driver by net.ucanaccess.jdbc.UcanaccessDriver (which is contained in a Maven package), the class will be found just fine.

Any idea what I am doing wrong here?

当您使用com.ibm.db2.jcc.DB2Driver'时,请确保db2jcc.jardb2jcc_license_cu.jar位于类路径中。请将两个jar都添加到类路径中,然后尝试一下。

The problem was that I was using the Maven exec plugin to run the main class. W/o using Maven, it works fine...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM