[英]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.