1.创建一个新项目first
2.加载数据库和hibernate驱动,方法:
a.在项目名上点右键->属性->path->required libraries->add->如果列表中有所要的包,则直接选择->OK
b.在项目名上点右键->属性->path->required libraries->add->如果列表中没有所要的包,则直接选择->NEW->ADD->选择包所在路径-
>OK->在name框中输入包名字->OK->OK->OK
C.注意,在选择包前,一定要将包复制到JBuilder的thirdparty目录
3.在项目名上点右键->属性->build->resource->在右边的文件类型列表中选择XML,再选择COPY->ok
4.创建虚拟目录,file->new...->web->web module
5.在项目根目录下(project source)创建hibernate.cfg.xml和tb_users.hbm.xml配置文件
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>
<!-- Mapping files -->
<property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
<property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Hibernate</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">shuangwei_2004</property>
<property name="connection.provider_class">net.sf.hibernate.connection.DBCPConnectionProvider</property>
<property name="connection.pool_size">2</property>
<property name="dbcp.maxActive">100</property>
<property name="dbcp.whenExhaustedAction">1</property>
<property name="dbcp.maxWait">120000</property>
<property name="dbcp.maxIdle">10</property>
<mapping resource="tb_Users.hbm.xml"/>
</session-factory>
</hibernate-configuration>
tb_Users.hbm.xml
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.kudo.mapping.TB_Users" table="tb_Users">
<id name="userID" type="int" column="UserID">
<generator class="increment"/>
</id>
<property name="username" type="string">
<column name="username"/>
</property>
<property name="nickname" type="string">
<column name="nickname"/>
</property>
<property name="userpwd" type="string">
<column name="password"/>
</property>
<property name="email" type="string">
<column name="email"/>
</property>
</class>
</hibernate-mapping>
6.在项目根目录下(project source)创建HibernateUtil.java(hibernate连接类)和Users.java(数据操作类)
HibernateUtil.java
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
import org.apache.commons.logging.*;
public class HibernateUtil {
public HibernateUtil() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static Log log = LogFactory.getLog(HibernateUtil.class);
private static Configuration configuration;
private static SessionFactory sessionFactory;
static {
try {
sessionFactory = new Configuration().configure().
buildSessionFactory();
} catch (Throwable ex) {
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static Configuration getConfiguration() {
return configuration;
}
public static void rebuildSessionFactory()
// throws DatastoreException
{
synchronized (sessionFactory) {
try {
sessionFactory = getConfiguration().buildSessionFactory();
} catch (Exception ex) {
log.error(ex.getMessage());
// throw DatastoreException.datastoreError(ex);
}
}
}
public static void rebuildSessionFactory(Configuration cfg)
// throws DatastoreException
{
synchronized (sessionFactory) {
try {
sessionFactory = cfg.buildSessionFactory();
configuration = cfg;
} catch (Exception ex) {
log.error(ex.getMessage());
// throw DatastoreException.datastoreError(ex);
}
}
}
public static Session getSession()
// throws DatastoreException
{
try {
return sessionFactory.openSession();
} catch (HibernateException ex) {
return null;
}
}
public static void close() {
try {
sessionFactory.close();
} catch (Exception ex) {
log.error(ex.getMessage());
}
}
public static void closeSession(Session session) {
// throws DatastoreException{
try {
session.close();
} catch (Exception ex) {
log.error(ex.getMessage());
// throw DatastoreException.datastoreError(ex);
}
}
public static void rollbackTransaction(Transaction transaction)
// throws DatastoreException
{
try {
if (transaction != null) {
transaction.rollback();
}
} catch (Exception ex) {
log.error(ex.getMessage());
// throw DatastoreException.datastoreError(ex);
}
}
private void jbInit() throws Exception {
}
}
Users.java
import net.sf.hibernate.*;
import java.util.*;
import java.text.DateFormat;
import java.sql.SQLException;
import java.sql.Types;
import java.sql.Connection;
import java.sql.CallableStatement;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.expression.*;
import java.io.*;
import com.kudo.mapping.TB_Users;
import javax.servlet.http.*;
public class Users {
public Users() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
//返回所有用户信息
public static List GetUsers() throws HibernateException
{
Session hsession=HibernateUtil.getSession();
List reUsers=null;
Transaction trans=null;
try
{
trans=hsession.beginTransaction();
Query query=hsession.createQuery("from TB_Users");
reUsers=query.list();
trans.commit();
}
catch(HibernateException ex)
{
HibernateUtil.rollbackTransaction(trans);
throw new HibernateException(ex.getMessage());
}
finally
{
HibernateUtil.closeSession(hsession);
}
return reUsers;
}
}
7.java hibernate配置
命令:
java net.sf.hibernate.tool.hbm2java.codegenerator tb_users.hbm.xml
配置文件:
D:\Borland\JBuilder2005\jdk1.4\lib\tools.jar;D:\Borland\JBuilder2005\jdk1.4\lib\dt.jar;D:\Borland\JBuilder2005\jdk1.4
\jre\lib\rt.jar;D:\Borland\JBuilder2005\thirdparty\hibernate\hibernate-tools.jar;D:\Borland\JBuilder2005
\thirdparty\hibernate\commons-collections-2.1.1.jar;D:\Borland\JBuilder2005\thirdparty\hibernate\commons-lang-
2.0.jar;D:\Borland\JBuilder2005\thirdparty\hibernate\commons-logging-1.0.4.jar;D:\Borland\JBuilder2005
\thirdparty\hibernate\hibernate2.jar;D:\Borland\JBuilder2005\thirdparty\hibernate\jdom.jar