日期:2014-05-16 浏览次数:20480 次
//hql语句查询 public ArrayList findClassByHql(String hql){ ArrayList list= new ArrayList(); Session session=null; Transaction tc=null; try{ session=HibernateSessionFactory.getSession(); tc=session.beginTransaction(); Query qy=session.createQuery(hql); if(qy!=null){ list=(ArrayList)qy.list(); } tc.commit(); }catch(Exception e){ e.printStackTrace(); } finally { if(session!=null) { session.close(); } } return list; } //通过ID得到类, public Account findAccountByID(String userName){ Account account=new Account(); Session session=null; Transaction tc=null; try{ session=HibernateSessionFactory.getSession(); tc=session.beginTransaction(); account =(com.my.jkp.hibernate.Account)session.load(com.my.jkp.hibernate.Account.class,userName ); }catch(Exception e){ e.printStackTrace(); if(tc!=null){ tc.rollback(); } } finally{ if(session!=null){ session.close(); } } return account; } //查询所有用户信息 public ArrayList findAllAccount(){ ArrayList list=new ArrayList(); String hql="from Account"; list=this.findClassByHql(hql); return list; } //增加用户 public boolean saveAccount(Account account){ boolean tag=false; Session session=null; Transaction tc=null; try{ session=HibernateSessionFactory.getSession(); tc=session.beginTransaction(); session.save(account); tc.commit(); }catch(Exception e){ e.printStackTrace(); } finally { if(session!=null) { session.close(); } } return tag; } //修改用户 public boolean updateAccount(Account account){ boolean tag=false; Session session=null; Transaction tc=null; try{ session=HibernateSessionFactory.getSession(); tc=session.beginTransaction(); session.update(account); tc.commit(); }catch(Exception e){ e.printStackTrace(); } finally { if(session!=null) { session.close(); } } return tag; } //删除用户 public boolean deleteAccount(String userName){ boolean tag=false; Session session=null; Transaction tc=null; Account Account=new Account(); try{ session=HibernateSessionFactory.getSession(); tc=session.beginTransaction(); Account =(com.my.jkp.hibernate.Account)session.load(com.my.jkp.hibernate.Account.class,userName ); session.delete(Account); tc.commit(); }catch(Exception e){ e.printStackTrace(); } finally { if(session!=null) { session.close(); } } return tag; }