JBuilder+hibernate配置

JBuilder+hibernate配置
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

<?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>
    <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
<?xml version="1.0"?>
<!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
 

package com.kudo.data;
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 package com.kudo.data;
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

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/andilyliao/archive/2006/08/08/1039273.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值