我正在运行一个使用Struts和Hibernate的应用程序,目前正在使用Derby数据库。现在我必须转移到DB2数据库。
请告诉我:
- 在Hibernate配置文件中我需要做哪些配置?
- 我需要设置任何classpath变量吗?
- 我知道DB2有两个jar包(db2jcc.jar & db2jcc_license_cu.jar)。我还需要其他的jar包吗?
提前致谢。
我正在运行一个使用Struts和Hibernate的应用程序,目前正在使用Derby数据库。现在我必须转移到DB2数据库。
请告诉我:
提前致谢。
它应该能够与db2jcc.jar一起使用。
将以下属性添加到您的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>
根据您的配置更改最后3个属性
hibernate.properties
文件和通过编程配置(更具体地说,请参见Hibernate参考文档,第1章和第2章)。以下是一个简单的示例,演示如何完成此操作: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();
通过 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
您需要将驱动程序(我不知道您拥有的JAR文件是否足够,但可能是这种情况)放在类路径上,并在persistence.xml
中设置数据库方言为org.hibernate.dialect.DB2Dialect
。
在JBoss中,通常只需要将驱动程序放入服务器的lib目录或应用程序的lib目录中。