環境:
開發的IDE:JBuilderX
使用的數據庫:MS SQL Server 2000
使用的數據庫驅動:JSQL Driver(JDBC 3.0)
說明:
1、hibernate在配置文件中明確說明“Microsoft Driver (not recommended!)”,因此先使用JSQL Driver。
2、JSQL Driver可以到http://www.jnetdirect.com中得到,需要先注冊個用戶,才能下載到試用的版本。
3、JDBC3.0只能在JDK1.4及以上版本中使用,JBuilderX默認的是JDK1.4
預備工作:
1、下載Hibernate,目前最高版本是2.1.2
2、在JBuilder中創建一個lib,起名為hibernate_full,將hibernate/lib下的所有jar通通放進去,并將hibernate/hibernate2.jar也放進去
3、在JBuilder中創建一個lib,起名為JSQL3,將JSQL Driver下的JNetDirect/JSQLConnect/JDBC_3.0_Driver/JSQLConnect.jar放進去
開始進行例子:
1、創建一個PRoject,命名為testhibernate
2、在屬性里的Required Libraries里加入hibernate_full和JSQL3
3、在菜單Project --> Project Properties --> Build --> Resource 里選中xml文件,選擇“Copy” --在編譯該項目的時候,會自動將src文件夾里的xml文件拷貝到classes文件夾里的相應目錄下
4、在testhibernate項目中創建一個src目錄
5、將hibernate源文件里的hibernate/src/hibernate.properties 和 log4j.properties拷貝到testhibernate項目中的src目錄下
6、修改hibernate.properties中關于MS Sql Server 2000驅動方面的配置
找到
## HypersonicSQL
hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
hibernate.connection.driver_class org.hsqldb.jdbcDriver
hibernate.connection.username sa
hibernate.connection.passWord
hibernate.connection.url jdbc:hsqldb:hsql://localhost
hibernate.connection.url jdbc:hsqldb:test
hibernate.connection.url jdbc:hsqldb:.
這段,這里是說默認的是使用HypersonicSQL,我們使用的是MS Sql Server,因此將整段注釋掉
## HypersonicSQL
#hibernate.dialect net.sf.hibernate.dialect.HSQLDialect
#hibernate.connection.driver_class org.hsqldb.jdbcDriver
#hibernate.connection.username sa
#hibernate.connection.password
#hibernate.connection.url jdbc:hsqldb:hsql://localhost
#hibernate.connection.url jdbc:hsqldb:test
#hibernate.connection.url jdbc:hsqldb:.
并且,找到
## MS SQL Server
#hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
#hibernate.connection.username sa
#hibernate.connection.password sa
## JSQL Driver
#hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
#hibernate.connection.url jdbc:JSQLConnect://1E1/test
這段,比如我們使用的數據庫服務器機器名為yuj,數據庫名為testhi,連接到數據庫上去的用戶名為sa,密碼為sa,則修改后這段成為
## MS SQL Server
hibernate.dialect net.sf.hibernate.dialect.SQLServerDialect
hibernate.connection.username sa
hibernate.connection.password sa
## JSQL Driver
hibernate.connection.driver_class com.jnetdirect.jsql.JSQLDriver
hibernate.connection.url jdbc:JSQLConnect://yuj/testhi
7、創建一個類testhibernate.Person,這是個標準的javaBean,只有3個屬性和相應的get/set方法
package testhibernate;
public class Person
新聞熱點
疑難解答