分享
 
 
 

Java: Too Simple Hibernate Sample with Ms SQL Server

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/*

Test.java 编译及运行 命令行:

C:\J2SDK\bin\javac -classpath "E:\Hibernate\hibernate-2.1.2\hibernate-2.1\hibernate2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-optional-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\c3p0-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\cglib-2.0-rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-collections-2.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-dbcp-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-lang-1.0.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-logging-1.0.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-pool-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\concurrent-1.3.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\connector.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\dom4j-1.4.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ehcache-0.6.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jaas.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-cache.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-common.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-jmx.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-system.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jcs-1.0-dev.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jdbc2_0-stdext.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jgroups-2.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jta.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\junit-3.8.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\log4j-1.2.8.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\odmg-3.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\oscache-2.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\proxool-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\swarmcache-1.0rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xalan-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xerces-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xml-apis.jar;F:\mssql\jdbc\lib\msbase.jar;F:\mssql\jdbc\lib\mssqlserver.jar;F:\mssql\jdbc\lib\msutil.jar;C:\J2SDK\demo\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\jfc\Java2D\Java2Demo.jar;C:\J2SDK\demo\plugin\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\plugin\jfc\Java2D\Java2Demo.jar;C:\J2SDK\jre\javaws\javaws.jar;C:\J2SDK\jre\lib\charsets.jar;C:\J2SDK\jre\lib\deploy.jar;C:\J2SDK\jre\lib\ext\dnsns.jar;C:\J2SDK\jre\lib\ext\ldapsec.jar;C:\J2SDK\jre\lib\ext\localedata.jar;C:\J2SDK\jre\lib\ext\sunjce_provider.jar;C:\J2SDK\jre\lib\ext\sunpkcs11.jar;C:\J2SDK\jre\lib\im\indicim.jar;C:\J2SDK\jre\lib\im\thaiim.jar;C:\J2SDK\jre\lib\javaws.jar;C:\J2SDK\jre\lib\jce.jar;C:\J2SDK\jre\lib\jsse.jar;C:\J2SDK\jre\lib\plugin.jar;C:\J2SDK\jre\lib\rt.jar;C:\J2SDK\lib\dt.jar;C:\J2SDK\lib\htmlconverter.jar;C:\J2SDK\lib\jconsole.jar;C:\J2SDK\lib\tools.jar" Test.java

C:\J2SDK\bin\java -classpath ".;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\hibernate2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ant-optional-1.5.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\c3p0-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\cglib-2.0-rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-collections-2.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-dbcp-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-lang-1.0.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-logging-1.0.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\commons-pool-1.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\concurrent-1.3.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\connector.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\dom4j-1.4.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\ehcache-0.6.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jaas.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-cache.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-common.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-jmx.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jboss-system.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jcs-1.0-dev.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jdbc2_0-stdext.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jgroups-2.2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\jta.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\junit-3.8.1.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\log4j-1.2.8.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\odmg-3.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\oscache-2.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\proxool-0.8.3.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\swarmcache-1.0rc2.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xalan-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xerces-2.4.0.jar;E:\Hibernate\hibernate-2.1.2\hibernate-2.1\lib\xml-apis.jar;F:\mssql\jdbc\lib\msbase.jar;F:\mssql\jdbc\lib\mssqlserver.jar;F:\mssql\jdbc\lib\msutil.jar;C:\J2SDK\demo\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\jfc\Java2D\Java2Demo.jar;C:\J2SDK\demo\plugin\jfc\CodePointIM\CodePointIM.jar;C:\J2SDK\demo\plugin\jfc\Java2D\Java2Demo.jar;C:\J2SDK\jre\javaws\javaws.jar;C:\J2SDK\jre\lib\charsets.jar;C:\J2SDK\jre\lib\deploy.jar;C:\J2SDK\jre\lib\ext\dnsns.jar;C:\J2SDK\jre\lib\ext\ldapsec.jar;C:\J2SDK\jre\lib\ext\localedata.jar;C:\J2SDK\jre\lib\ext\sunjce_provider.jar;C:\J2SDK\jre\lib\ext\sunpkcs11.jar;C:\J2SDK\jre\lib\im\indicim.jar;C:\J2SDK\jre\lib\im\thaiim.jar;C:\J2SDK\jre\lib\javaws.jar;C:\J2SDK\jre\lib\jce.jar;C:\J2SDK\jre\lib\jsse.jar;C:\J2SDK\jre\lib\plugin.jar;C:\J2SDK\jre\lib\rt.jar;C:\J2SDK\lib\dt.jar;C:\J2SDK\lib\htmlconverter.jar;C:\J2SDK\lib\jconsole.jar;C:\J2SDK\lib\tools.jar" Test

*/

import net.sf.hibernate.*;

import net.sf.hibernate.cfg.*;

public class Test

{

public static void main(String[] args) throws Exception

{

SessionFactory sf = new Configuration().configure().buildSessionFactory();

Session session = sf.openSession();

Transaction tx = session.beginTransaction();

for (int i = 0; i < 200; i++)

{

Customer customer = new Customer();

customer.setUsername("customer" + i);

customer.setPassword("customer");

session.save(customer);

}

tx.commit();

//session.close();

// Query query = session.createQuery("select username from customers as c");

// java.util.Iterator i = query.iterate();

// while (i.hasNext())

// {

// Customer c = (Customer) i.next();

// System.out.println(c.getUsername());

// }

}

}

class Customer

{

private int _id;

private String _username;

private String _password;

public int getId()

{

return _id;

}

public String getPassword()

{

return _password;

}

public String getUsername()

{

return _username;

}

public void setId(int id)

{

this._id = id;

}

public void setPassword(String password)

{

this._password = password;

}

public void setUsername(String username)

{

this._username = username;

}

}

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">

<hibernate-configuration>

<session-factory name="java:/hibernate/HibernateFactory">

<property name="show_sql">

true

</property>

<property name="connection.driver_class">

com.microsoft.jdbc.sqlserver.SQLServerDriver

</property>

<property name="connection.url">

jdbc:microsoft:sqlserver://server\\psqlke;DataBaseName=hibernate_test

</property>

<property name="connection.username">

sa

</property>

<property name="connection.password">

123abc

</property>

<property name="dialect">

net.sf.hibernate.dialect.SQLServerDialect

</property>

<mapping resource="Customer.hbm.xml" />

</session-factory>

</hibernate-configuration>

Customer.hbm.xml

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="Customer" table="customers">

<id name="id" column="CID">

<generator class="increment" />

</id>

<property name="username" column="USERNAME" />

<property name="password" column="PASSWORD" />

</class>

</hibernate-mapping>

SQL:

CREATE TABLE CUSTOMER

(

CID INTEGER NOT NULL PRIMARY KEY,

USERNAME VARCHAR(12) NOT NULL,

PASSWORD VARCHAR(12)

);

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有