Hibernate基于JDBC的批量删除

String hql="delete from Useradd here addid =10 ";//Useradd为类名.addid为Useradd的属性
//.特别要注意该hql语句中的Useradd不能有别名.
UserDao userdao=new UserDao();
boolean flag=userdao.deletebyJdbcAPI(String hql);

public class UserDao{
      public boolean deletebyJdbcAPI(String hql){
boolean flag=false;
Session session=HibernateSessionFactory.getSession();
Transaction tx=null;
Connection con=null;
PreparedStatement pstn=null;
try{
  tx=session.beginTransaction();
  con=session.connection();
  pstn=con.prepareStatement(hql);
  int i=pstn.executeUdate();
  if(i>0){
   flag=true;
  }
  tx.commit();
}catch(Exception ex){
  if(tx!=null){
   tx.rollback();
  }
}finally{
  session.close();
}
return flag;
     }
}
posted @ 2010-12-20 22:01  左正  阅读(256)  评论(0编辑  收藏  举报