how to connect hibernate and DB2
Asked Answered
S

3

13

I am running an application that used struts and hibernate. I am currently using Derby database. Now i have to shift on DB2 database.

Please tell me

  • what Configuration I have to do in hibernate configuration file?
  • Do I have to set any classpath variable?
  • I know there are two jars for DB2 (db2jcc.jar & db2jcc_license_cu.jar). Is there any other jar I may need?

Thanks in advance.

Shevat answered 31/3, 2011 at 6:3 Comment(2)
I want hibernate to manage it. Not any container. I am using JBoss server.Shevat
Thanks this problem is over but there's another. Here is the link. https://mcmap.net/q/904913/-hibernate-and-db2-data-fetching/420613Shevat
D
22

It should work with db2jcc.jar

Add below properties to your hibernate.cfg.xml

<property name="hibernate.dialect">org.hibernate.dialect.DB2Dialect</property>

<property name="hibernate.connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>

<property name="connection.url">jdbc:db2://<host>:<port50000>/<dbname></property>

<property name="connection.username">dbusername</property>

<property name="connection.password">dbpassword</property>

Change last 3 properties according to your configuration

Dagostino answered 31/3, 2011 at 6:47 Comment(4)
please see my response in my last answer below the page.Shevat
Thanks this problem is over but there's another. Here is the link. https://mcmap.net/q/904913/-hibernate-and-db2-data-fetching/420613Shevat
You should mark the answer as correct which solved your problem.Dagostino
@ImranTariq it was your complete hibernate.cfg.xml ?Interlard
M
2

If your DB2 driver supports JDBC approach (and it does), you need to set connection properties. There are three ways of doing so: via xml, via hibernate.properties file and via programmatic configuration (more specifically, see Hibernate Reference Documentation, chapter 1 and 2. Here is an simple example, how to do this:

Programmatically:

SessionFactory sf = new Configuration()
.setProperty("hibernate.connection.driver_class", "com.ibm.db2.jcc.DB2Driver")
.setProperty("hibernate.connection.url", "jdbc:db2://yourDbServerUrl:port/databaseName")
.setProperty("hibernate.connection.username", "yourUsername")
.setProperty("hibernate.connection.password", "yourPassword")
.buildSessionFactory();

Via hibernate.properties:

hibernate.connection.driver_class = com.ibm.db2.jcc.DB2Driver
hibernate.connection.url = jdbc:db2://yourDbServerUrl:port/databaseName
hibernate.connection.username = yourUsername
hibernate.connection.password = yourPassword
Mediaeval answered 31/3, 2011 at 6:38 Comment(2)
please see my response in my last answer below the page.Shevat
Thanks this problem is over but there's another. Here is the link. https://mcmap.net/q/904913/-hibernate-and-db2-data-fetching/420613Shevat
P
0

You'd have to need the driver (I don't know if the jars you have are sufficient, but it might be the case) on the classpath and set the database dialect to org.hibernate.dialect.DB2Dialect in your persistence.xml.

In JBoss it's normally only necessary to either put the driver into the server's lib directory or into the application's lib dir.

Pieplant answered 31/3, 2011 at 6:31 Comment(2)
please see my response in my last answer below the page.Shevat
Thanks this problem is over but there's another. Here is the link. https://mcmap.net/q/904913/-hibernate-and-db2-data-fetching/420613Shevat

© 2022 - 2024 — McMap. All rights reserved.