[英]how to connect hibernate and DB2
我正在運行一個使用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個屬性
如果您的DB2驅動程序支持JDBC方法(並且確實支持),則需要設置連接屬性。 這樣做的方式有三種:通過xml,通過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目錄中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.