Tim Tim - 1 year ago 169
Java Question

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


<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" />

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
(which is contained in a Maven package), the class will be found just fine.

Any idea what I am doing wrong here?

Tim Tim
Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download