发表新主题 回复该帖子
主题:利用hibernate执行jdbc中的事物处理
唧唧
帖子档案  楼主 利用hibernate执行jdbc中的事物处理   Post by : 2008-06-23 11:38:14.0
  • 幼儿园
  • 幼儿园
  • UID:3
  • 主题:342
  • 帖子:781
  • 加为好友 加为好友    发送短信 发送短信
java代码
  1. //此为更新的例子     
  2. try{    
  3.     Session session = HibernateUtil.currentSession();    
  4.     try{    
  5.         Connection connection = session.connection();// 获取连接    
  6.         PreparedStatement preparedStatement = null;    
  7.         String deleteSQL = "delete from a where b=c";    
  8.         //开始事物    
  9.         connection.setAutoCommit(false);            
  10.         preparedStatement = connection.prepareStatement(deleteSQL);    
  11.         preparedStatement.executeUpdate();    
  12.         //提交JDBC事务    
  13.         connection.commit();    
  14.         // 恢复JDBC事务的默认提交方式    
  15.         connection.setAutoCommit(true);    
  16.         preparedStatement.close();    
  17.     }catch (SQLException x) {    
  18.         connection.rollback();    
  19.         throw x;    
  20.     }    
  21. }catch(HibernateException e){    
  22.    e.printStackTrace();    
  23.             throw e;       
  24. }finally{    
  25.    HibernateUtil.closeSession();    
  26. }    
  27.      
  28. //关于查询:    
  29. try{    
  30.     Session session = HibernateUtil.currentSession();    
  31.     try{    
  32.         Connection conn = session.connection();    
  33.         PreparedStatement prepstmt = conn.prepareStatement(query);    
  34.         prepstmt.setLong(1,Long.parseLong(paperId));    
  35.         ResultSet rs = prepstmt.executeQuery();       
  36.         ArrayList rsl = new ArrayList();    
  37.         while(rs.next()){    
  38.             HashMap item = new HashMap();    
  39.             item.put("××",Long.toString(rs.getLong("××")));    
  40.             rsl.add(item);        
  41.         }        
  42.         //一定要养成好习惯用完就关,否则会报“游标超出最大值”的错误的    
  43.         rs.close();    
  44.         prepstmt.close();    
  45.     }catch (SQLException x) {    
  46.         connection.rollback();    
  47.         throw x;    
  48.     }    
  49. }catch(HibernateException e){    
  50.    e.printStackTrace();    
  51.             throw e;       
  52. }finally{    
  53.    HibernateUtil.closeSession();    
  54. }    
  55.    
  56. //调用存储过程:    
  57. try{    
  58.     Session session = HibernateUtil.currentSession();    
  59.     Connection conn = session.connection();        
  60.     CallableStatement cstmt = conn.prepareCall("{call SAVE_AS_NEW_PAPER(?,?)}");    
  61.     cstmt.setLong(1new Long(newPid).longValue());         
  62.     cstmt.setLong(2new Long(oldPid).longValue());     
  63.     cstmt.executeQuery();    
  64.     cstmt.close();         
  65. }catch(HibernateException e){    
  66.     throw e;    
  67. }catch(SQLException e){    
  68.     throw e;    
  69. }finally{    
  70.     HibernateUtil.closeSession();    
  71. }  
签名
 ★★★★★★★★
 纵里寻她千百度,蓦然回首,那人却在,灯火阑珊处!
 MyBlog :http://blog.javawind.net
返回页面顶部  


CopyRight © 2008-2009 JavaWind.Net Studio All Rights Reserved
Powered By JWind.BBS Vesion 1.0.0 Beta1 Processed in 18 ms,0 (Queries)  Gzip enabled

WAP - 清除Cookies - 联系我们 - JavaWind.Net Studio - Archiver - TOP Valid XHTML 1.0 Transitional Valid CSS! 粤ICP备07511478号