JBuilderX+SQL Server開(kāi)發(fā)hibernate
更新時(shí)間:2006年10月13日 00:00:00 作者:
環(huán)境:
開(kāi)發(fā)的IDE:JBuilderX
使用的數(shù)據(jù)庫(kù):MS Sql Server 2000
使用的數(shù)據(jù)庫(kù)驅(qū)動(dòng):JSQL Driver(JDBC 3.0)
說(shuō)明:
1、hibernate在配置文件中明確說(shuō)明“Microsoft Driver (not recommended!)”,因此先使用JSQL Driver。
2、JSQL Driver可以到http://www.jnetdirect.com中得到,需要先注冊(cè)個(gè)用戶,才能下載到試用的版本。
3、JDBC3.0只能在JDK1.4及以上版本中使用,JBuilderX默認(rèn)的是JDK1.4
準(zhǔn)備工作:
1、下載Hibernate,目前最高版本是2.1.2
2、在JBuilder中創(chuàng)建一個(gè)lib,起名為hibernate_full,將hibernatelib下的所有jar通通放進(jìn)去,并將hibernatehibernate2.jar也放進(jìn)去
3、在JBuilder中創(chuàng)建一個(gè)lib,起名為JSQL3,將JSQL Driver下的JNetDirectJSQLConnectJDBC_3.0_DriverJSQLConnect.jar放進(jìn)去
開(kāi)始進(jìn)行例子:
1、創(chuàng)建一個(gè)project,命名為testhibernate
2、在屬性里的Required Libraries里加入hibernate_full和JSQL3
3、在菜單Project --> Project Properties --> Build --> Resource 里選中xml文件,選擇“Copy” --在編譯該項(xiàng)目的時(shí)候,會(huì)自動(dòng)將src文件夾里的xml文件拷貝到classes文件夾里的相應(yīng)目錄下
4、在testhibernate項(xiàng)目中創(chuàng)建一個(gè)src目錄
5、將hibernate源文件里的hibernatesrchibernate.properties 和 log4j.properties拷貝到testhibernate項(xiàng)目中的src目錄下
6、修改hibernate.properties中關(guān)于MS Sql Server 2000驅(qū)動(dòng)方面的配置
找到
## 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:.
這段,這里是說(shuō)默認(rèn)的是使用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
這段,比如我們使用的數(shù)據(jù)庫(kù)服務(wù)器機(jī)器名為yuj,數(shù)據(jù)庫(kù)名為testhi,連接到數(shù)據(jù)庫(kù)上去的用戶名為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、創(chuàng)建一個(gè)類testhibernate.Person,這是個(gè)標(biāo)準(zhǔn)的JavaBean,只有3個(gè)屬性和相應(yīng)的getset方法
package testhibernate;
public class Person
{
private String id;
private String name;
private String address;
public void setId(String value)
{
this.id = value;
}
public String getId()
{
return id;
}
public void setName(String value)
{
this.name = value;
}
public String getName()
{
return name;
}
public void setAddress(String value)
{
this.address = value;
}
public String getAddress()
{
return address;
}
}
8、創(chuàng)建一個(gè)對(duì)象-關(guān)系映射的xml文件Person.hbm.xml,放在和Person.java相同的目錄下面
?。?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
?。糷ibernate-mapping>
?。糲lass name="testhibernate.Person">
<!--hibernate為我們生成主鍵id-->
?。糹d name = "id" unsaved-value = "null">
<generator class="uuid.hex"/>
?。?id>
?。?--默認(rèn)把類的變量映射為相同名字的表列,當(dāng)然我們可以修改其映射方式-->
?。紁roperty name="name"/>
<property name="address"/>
?。?class>
?。?hibernate-mapping>
9、創(chuàng)建調(diào)用類Person的客戶端程序Client1.java
package testhibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
/**
*本類只是用來(lái)創(chuàng)建表的,并不往表內(nèi)部插入任何數(shù)據(jù),并且只能使用一次,否則會(huì)刪除已有的表的
*/
public class Client1
{
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration().addClass(Person.class);
//第一次運(yùn)行時(shí)用來(lái)在數(shù)據(jù)庫(kù)中創(chuàng)建表
//并且把sql語(yǔ)句輸出到txt文件用的
//以后的運(yùn)行不能使用該段代碼,否則每次都會(huì)先刪除原表,再新建該表
SchemaExport dbExport = new SchemaExport(conf);
dbExport.setOutputFile("sql.txt");
dbExport.create(true, true);
}
}
package testhibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
public class Client2
{
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration().addClass(Person.class);
sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession();
Transaction t = s.beginTransaction();
Person yuj = new Person();
yuj.setName("john");
yuj.setAddress("上海");
Person x = new Person();
x.setName("zhaoyh");
x.setAddress("上海");
//持久化
s.save(yuj); //此時(shí)yuj已經(jīng)可以在數(shù)據(jù)庫(kù)中找到
s.save(x); //此時(shí)x已經(jīng)可以在數(shù)據(jù)庫(kù)中找到
t.commit();
s.close();
}
}
查看數(shù)據(jù)庫(kù)中,增加了2條記錄,OK!初步使用成功了,剩下的慢慢研究吧……
開(kāi)發(fā)的IDE:JBuilderX
使用的數(shù)據(jù)庫(kù):MS Sql Server 2000
使用的數(shù)據(jù)庫(kù)驅(qū)動(dòng):JSQL Driver(JDBC 3.0)
說(shuō)明:
1、hibernate在配置文件中明確說(shuō)明“Microsoft Driver (not recommended!)”,因此先使用JSQL Driver。
2、JSQL Driver可以到http://www.jnetdirect.com中得到,需要先注冊(cè)個(gè)用戶,才能下載到試用的版本。
3、JDBC3.0只能在JDK1.4及以上版本中使用,JBuilderX默認(rèn)的是JDK1.4
準(zhǔn)備工作:
1、下載Hibernate,目前最高版本是2.1.2
2、在JBuilder中創(chuàng)建一個(gè)lib,起名為hibernate_full,將hibernatelib下的所有jar通通放進(jìn)去,并將hibernatehibernate2.jar也放進(jìn)去
3、在JBuilder中創(chuàng)建一個(gè)lib,起名為JSQL3,將JSQL Driver下的JNetDirectJSQLConnectJDBC_3.0_DriverJSQLConnect.jar放進(jìn)去
開(kāi)始進(jìn)行例子:
1、創(chuàng)建一個(gè)project,命名為testhibernate
2、在屬性里的Required Libraries里加入hibernate_full和JSQL3
3、在菜單Project --> Project Properties --> Build --> Resource 里選中xml文件,選擇“Copy” --在編譯該項(xiàng)目的時(shí)候,會(huì)自動(dòng)將src文件夾里的xml文件拷貝到classes文件夾里的相應(yīng)目錄下
4、在testhibernate項(xiàng)目中創(chuàng)建一個(gè)src目錄
5、將hibernate源文件里的hibernatesrchibernate.properties 和 log4j.properties拷貝到testhibernate項(xiàng)目中的src目錄下
6、修改hibernate.properties中關(guān)于MS Sql Server 2000驅(qū)動(dòng)方面的配置
找到
## 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:.
這段,這里是說(shuō)默認(rèn)的是使用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
這段,比如我們使用的數(shù)據(jù)庫(kù)服務(wù)器機(jī)器名為yuj,數(shù)據(jù)庫(kù)名為testhi,連接到數(shù)據(jù)庫(kù)上去的用戶名為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、創(chuàng)建一個(gè)類testhibernate.Person,這是個(gè)標(biāo)準(zhǔn)的JavaBean,只有3個(gè)屬性和相應(yīng)的getset方法
package testhibernate;
public class Person
{
private String id;
private String name;
private String address;
public void setId(String value)
{
this.id = value;
}
public String getId()
{
return id;
}
public void setName(String value)
{
this.name = value;
}
public String getName()
{
return name;
}
public void setAddress(String value)
{
this.address = value;
}
public String getAddress()
{
return address;
}
}
8、創(chuàng)建一個(gè)對(duì)象-關(guān)系映射的xml文件Person.hbm.xml,放在和Person.java相同的目錄下面
?。?xml version="1.0" encoding="GB2312"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
?。糷ibernate-mapping>
?。糲lass name="testhibernate.Person">
<!--hibernate為我們生成主鍵id-->
?。糹d name = "id" unsaved-value = "null">
<generator class="uuid.hex"/>
?。?id>
?。?--默認(rèn)把類的變量映射為相同名字的表列,當(dāng)然我們可以修改其映射方式-->
?。紁roperty name="name"/>
<property name="address"/>
?。?class>
?。?hibernate-mapping>
9、創(chuàng)建調(diào)用類Person的客戶端程序Client1.java
package testhibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
/**
*本類只是用來(lái)創(chuàng)建表的,并不往表內(nèi)部插入任何數(shù)據(jù),并且只能使用一次,否則會(huì)刪除已有的表的
*/
public class Client1
{
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration().addClass(Person.class);
//第一次運(yùn)行時(shí)用來(lái)在數(shù)據(jù)庫(kù)中創(chuàng)建表
//并且把sql語(yǔ)句輸出到txt文件用的
//以后的運(yùn)行不能使用該段代碼,否則每次都會(huì)先刪除原表,再新建該表
SchemaExport dbExport = new SchemaExport(conf);
dbExport.setOutputFile("sql.txt");
dbExport.create(true, true);
}
}
package testhibernate;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.tool.hbm2ddl.SchemaExport;
public class Client2
{
private static SessionFactory sessionFactory;
public static void main(String[] args) throws Exception
{
Configuration conf = new Configuration().addClass(Person.class);
sessionFactory = conf.buildSessionFactory();
Session s = sessionFactory.openSession();
Transaction t = s.beginTransaction();
Person yuj = new Person();
yuj.setName("john");
yuj.setAddress("上海");
Person x = new Person();
x.setName("zhaoyh");
x.setAddress("上海");
//持久化
s.save(yuj); //此時(shí)yuj已經(jīng)可以在數(shù)據(jù)庫(kù)中找到
s.save(x); //此時(shí)x已經(jīng)可以在數(shù)據(jù)庫(kù)中找到
t.commit();
s.close();
}
}
查看數(shù)據(jù)庫(kù)中,增加了2條記錄,OK!初步使用成功了,剩下的慢慢研究吧……
您可能感興趣的文章:
- 使用mongovue把sqlserver數(shù)據(jù)導(dǎo)入mongodb的步驟
- Java的Hibernate框架結(jié)合MySQL的入門學(xué)習(xí)教程
- spring mvc4.1.6 spring4.1.6 hibernate4.3.11 mysql5.5.25開(kāi)發(fā)環(huán)境搭建圖文教程
- struts2.3.24+spring4.1.6+hibernate4.3.11+mysql5.5.25開(kāi)發(fā)環(huán)境搭建圖文教程
- Hibernate4在MySQL5.1以上版本創(chuàng)建表出錯(cuò) type=InnDB
- 在Java的Hibernate框架中使用SQL語(yǔ)句的簡(jiǎn)單介紹
- 詳解Java的Hibernate框架中的緩存與原生SQL語(yǔ)句的使用
- 解析Hibernate + MySQL中文亂碼問(wèn)題
- 解決hibernate+mysql寫入數(shù)據(jù)庫(kù)亂碼
- SqlServer與MongoDB結(jié)合使用NHibernate
相關(guān)文章
實(shí)例解析JSP中EL表達(dá)式的各種運(yùn)用
這篇文章主要介紹了JSP中EL表達(dá)式的各種運(yùn)用,比如用EL處理數(shù)學(xué)運(yùn)算、內(nèi)置對(duì)象以及數(shù)據(jù)的存取等,代碼示例都相當(dāng)實(shí)用,需要的朋友可以參考下2016-04-04Jsp+Servlet實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)查詢
這篇文章主要介紹了Jsp+Servlet實(shí)現(xiàn)簡(jiǎn)單登錄注冊(cè)查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09Hibernate實(shí)體對(duì)象繼承的三種方法
這篇文章主要介紹了Hibernate實(shí)體對(duì)象繼承的方法的相關(guān)資料,需要的朋友可以參考下2017-06-06jsp學(xué)習(xí)之scriptlet的使用方法詳解
這篇文章主要介紹了jsp學(xué)習(xí)之scriptlet的使用方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07