Hibernate连接SQLServer2000数据库(例子)
先从例子开始(使用MyEclipse):
建立一个Java工程,名为HibernateProject,右击工程名---->Build Path---->Add Libraries--->MyEclipse Libraries(点击“next”)--->选择需要的Hibernate包,我不知道哪些需要,哪些不需要,所以3.2版本的三个包全部添加了,另外还添加了SQLServer的三个驱动包(msbase.jar等放在自己建立的包SqlDriver中)
(1)建立数据库db_Hibernate,然后在查询分析其中在该数据库的基础上建立表login:
create table login
(
id int identity primary key not null,
name varchar(15),
password varchar(15)
)
上面的identity不能少,否则程序运行时会报错,无法插入数据。
(2) 编写类文件UserInfo.java:
package hibernate.ch01;
public class UserInfo {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
保存后在工程的src文件夹下就生成了包hibernate.ch01(目录结构见截图)
(3) 编写对象关系映射文件UserInfo.hbm.xml(注意开头三行与下面配置文件hibernate.cfg.xml开头的不同):
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC "-//hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- created by afuer -->
<!-- 类和表之间的关联 -->
<class name="hibernate.ch01.UserInfo" table="login">
<!-- 类对象的主键和表主键的关联 -->
<id name="id" type="integer"><column name="id"/>
<!-- 指明主键的自增长类型 -->
<generator class="identity"/>
</id>
<!-- 以下为普通字段关联 -->
<property name="userName" type="string">
<column name="name" length="100"/>
</property>
<property name="password" type="string">
<column name="password" length="100"/>
</property>
</class>
</hibernate-mapping>
(4)编写配置文件hibernate.cfg.xml(此文件一定是在src目录下):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- SessionFactory配置 -->
<session-factory>
<!-- 指定数据库使用的SQL方言 -->
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<!-- 指定连接数据库的驱动 -->
<property name="connection.driver_class">
com.microsoft.jdbc.sqlserver.SQLServerDriver
</property>
<!-- 指定连接数据库的路径 -->
<property name="connection.url">
jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=db_Hibernate
</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">sa</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password"></property>
<!-- 当show_sql属性为true时,表示当程序运行时在控制台输出SQL语句,默认为false -->
<property name="show_sql">true</property>
<!-- 指定是否按照标准格式在控制台输出SQL语句 -->
<property name="format_sql">true</property>
<!-- 指定是否在SQL语句中输出便于调试的注释信息 -->
<property name="use_sql_commnets">true</property>
<!-- 指定持久化类映射文件 -->
<property name="myeclipse.connection.profile">SqlDriver</property>
<property name="dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<mapping resource="hibernate/ch01/UserInfo.hbm.xml" />
</session-factory>
</hibernate-configuration>
编写运行测试类HibernateTest.java:
package hibernate.ch01;
import hibernate.ch01.UserInfo;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String[] args) {
//加载配置文件'
SessionFactory sessions=new Configuration().configure().buildSessionFactory();
Session session=sessions.openSession();
Transaction tx=null;
try{
//开始事务
tx=session.beginTransaction();
//给对象设定值
UserInfo u=new UserInfo();
u.setUserName("Tracy");
u.setPassword("123");
System.out.println("开始插入到数据库...");
//保存到数据库
session.save(u);
//从持久化类UserInfo中读取数据
UserInfo u1=(UserInfo)session.load(UserInfo.class,new Integer(2));
System.out.println("从数据库加载数据的用户名为"+u1.getUserName());
//结束事务
tx.commit();
tx=null;
System.out.println("嗨!恭喜你第一个程序运行成功!");
}catch(HibernateException e){
e.printStackTrace();
if(tx!=null){
tx.rollback();
}
}finally{
session.close();
}
}
}
(5)运行结果截图