Hibernate操作数据库

/引入导入的hibernate包 
import net.sf.hibernate.cfg.Configuration; 
import net.sf.hibernate.HibernateException; 
import net.sf.hibernate.SessionFactory; 
import net.sf.hibernate.Session; 
import net.sf.hibernate.Transaction; 
import java.util.List; 
import com.hygj.bean.Users; 
public class HibernateUtil { 
    //定义私有变量cfg用来捕获hibernate.cfg.xml配制文件 
    private net.sf.hibernate.cfg.Configuration cfg=null; 
    //定义变量sf用来管理连接 
    private net.sf.hibernate.SessionFactory sf=null; 
    //定义session用来实现具体的操作 
    private net.sf.hibernate.Session session=null; 
    //管理一个用户的多种操作,进行事物的管理 
    private net.sf.hibernate.Transaction tr=null; 

    public HibernateUtil() { 
      try { 
        cfg = new net.sf.hibernate.cfg.Configuration().configure(); 
        sf=cfg.buildSessionFactory(); 
      } 
      catch (HibernateException ex) { 
      } 
    } 


    //全部查询 
    public void chaxun(){ 
      System.out.println("----chaxun----"); 
      //产生回话 
      try { 
        this.sessions = this.sff.openSession(); 
        session = sf.openSession(); 
        //开始事务 
        this.trs = this.sessions.beginTransaction(); 
        tr=session.beginTransaction(); 
        //生成查询 
        net.sf.hibernate.Query query=session.createQuery("from Users"); 
        //获得结果集 
        List list=query.list(); 
        java.util.Iterator it=list.iterator(); 
        while(it.hasNext()){ 
          Users user=(Users)it.next(); 
          System.out.println("id:"+user.getId()); 
          System.out.println("name:"+user.getUsername()); 
          System.out.println("password:"+user.getPassword()); 
          System.out.println("--------"); 
        } 
        //执行完成,提交事务 
        tr.commit(); 
      } 
      catch (HibernateException ex) { 
      } 
      finally{ 
        try { 
          session.close(); 
        } 
        catch (HibernateException ex1) { 
        } 
      } 
    } 

    //根据编号查询 
    public void chaxun(int id){ 
      try { 
        session = sf.openSession(); 
        tr=session.beginTransaction(); 
        //产生查询,需要根据id进行查询 
       net.sf.hibernate.Query query=session.createQuery("from Users where id=?"); 
        // net.sf.hibernate.Query que=session.createQuery("from Users where id=:id"); 
      //先为参数赋值,再查询 
      query.setInteger(0,id); 
      //que.setInteger("id",id); 
       //List list=que.list(); 
      List list=query.list(); 
      //进行空对象验证 
      if(list.size()>0){ 
        Users user=(Users)list.get(0); 
        System.out.println("id:"+user.getId()); 
          System.out.println("name:"+user.getUsername()); 
          System.out.println("password:"+user.getPassword()); 
          System.out.println("--------"); 

      } 
      else{ 
        System.out.println("no record"); 
      } 
      tr.commit(); 
      } 
      catch (HibernateException ex) { 
      } 
      finally{ 
        try { 
          session.close(); 
        } 
        catch (HibernateException ex1) { 
        } 
      } 

    } 

//模糊查询 
    public void chaxun(String name){ 
      try { 
        session = sf.openSession(); 
        tr=session.beginTransaction(); 
        //产生查询,需要根据id进行查询 
       net.sf.hibernate.Query query=session.createQuery("from Users where username like ?"); 
        // net.sf.hibernate.Query que=session.createQuery("from Users where id=:id"); 
      //先为参数赋值,再查询 
      String str="%"+name+"%"; 
      query.setString(0,str); 
      //que.setInteger("id",id); 
       //List list=que.list(); 
      List list=query.list(); 
      //进行空对象验证 
      if(list.size()>0){ 
       for(int i=0;i<list.size();i++){ 
         Users user=(Users)list.get(i); 
       System.out.println("id:"+user.getId()); 
         System.out.println("name:"+user.getUsername()); 
         System.out.println("password:"+user.getPassword()); 
         System.out.println("--------"); 
       } 

      } 
      else{ 
        System.out.println("no record"); 
      } 
      tr.commit(); 
      } 
      catch (HibernateException ex) { 
      } 
      finally{ 
        try { 
          session.close(); 
        } 
        catch (HibernateException ex1) { 
        } 
      } 


    } 

//添加数据 
    public void add(String name,String pwd){ 
      try { 
        session = sf.openSession(); 
        tr=session.beginTransaction(); 
        //实例化新的bean 类 
        Users user=new Users(); 
        //为bean赋值 
        user.setUsername(name); 
        user.setPassword(pwd); 
        //保存提交 
        session.save(user); 
        tr.commit(); 
      } 
      catch (HibernateException ex) { 
      } 
      finally{ 
        try { 
          session.close(); 
        } 
        catch (HibernateException ex1) { 
        } 
      } 

    } 

//修改数据 
    public void update(int id,String pwd){ 
      try { 
        session = sf.openSession(); 
        tr=session.beginTransaction(); 
        //先定位数据 
        net.sf.hibernate.Query query=session.createQuery("from Users where id=?"); 
        query.setInteger(0,id); 
        List list=query.list(); 
        if(list.size()>0){ 
          Users user=(Users)list.get(0); 
          //该密码 
          user.setPassword(pwd); 
          //保存回去 
          session.update(user); 
          session.flush(); 
          tr.commit(); 
          System.out.println("update ok"); 
        } 
        else{ 
          System.out.println("no record can be updated"); 
        } 
      } 
      catch (HibernateException ex) { 
      } 
      finally{ 
        try { 
          session.close(); 
        } 
        catch (HibernateException ex1) { 
        } 
      } 


    } 


//删除数据 
    public void delete(int id){ 
      try { 
        session = sf.openSession(); 
        tr=session.beginTransaction(); 
        //先定位数据 
        net.sf.hibernate.Query query=session.createQuery("from Users where id=?"); 
        query.setInteger(0,id); 
        List list=query.list(); 
        if(list.size()>0){ 
          Users user=(Users)list.get(0); 
          //删除数据 
          session.delete(user); 
          session.flush(); 
          tr.commit(); 
          System.out.println("delete ok"); 
        } 
        else{ 
          System.out.println("no record can be deleted"); 
        } 
      } 
      catch (HibernateException ex) { 
      } 
      finally{ 
        try { 
          session.close(); 
        } 
        catch (HibernateException ex1) { 
        } 
      } 


    } 

    public static void main(String[] args) { 
      HibernateUtil hibernateUtil1 = new HibernateUtil(); 
      //hibernateUtil1.add("subin","123456"); 
      //hibernateUtil1.chaxun(3); 
      //hibernateUtil1.update(3,"9876543"); 
      //hibernateUtil1.chaxun(3); 
      hibernateUtil1.delete(4); 
    } 



连接操作在配置文件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> 
    <property name="show_sql">true</property> 
    <property name="connection.driver_class"> 
    com.microsoft.jdbc.sqlserver.SQLServerDriver 
    </property> 
    <property name="connection.url"> 
    jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs; 
    </property> 
    <property name="connection.username"> 
    sa 
    </property> 
    <property name="connection.password"> 

    </property> 

    <mapping resource="com/hygj/bean/Users.hbm.xml" /> 
</session-factory> 
</hibernate-configuration> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值