How to connect to sleep and DB2

I am running an application using struts and hibernate. I am currently using the Derby database. Now I need to go to the DB2 database.

Tell me please,

  • What configuration do I need to do in hibernate configuration file?
  • Do I need to set any classpath variable?
  • I know that there are two banks for DB2 ( db2jcc.jar and db2jcc_license_cu.jar ). Is there any other jar I may need?

Thanks in advance.

+7
source share
3 answers

It should work with db2jcc.jar

Add the properties below to 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 the last 3 properties to suit your configuration

+15
source

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

Program:

 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 
+2
source

You will need a driver (I don’t know if you have enough jars, but it may be so) in the class path and set the database dialogs to org.hibernate.dialect.DB2Dialect in persistence.xml .

In JBoss, you usually need to either place the driver in the lib server directory or in the application lib directory.

0
source

All Articles