码迷,mamicode.com
首页 > 数据库 > 详细

jdbc实现事务

时间:2016-10-03 21:25:35      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

//conn需要自己获取,这里我用的时springjdbcTemplate
Connection conn = null; PreparedStatement pstm = null; try { conn = jdbc.getDataSource().getConnection(); } catch (SQLException e1) { log.error(e1.getMessage()); return "服务器错误!"; } FatherModule firstErrorFatherModule = null; String sql = null; try { for(FatherModule fatherModule : fatherModules) { firstErrorFatherModule = fatherModule; sql = "update sfk_father_module t set t.sort = ? where t.id = ?;"; pstm = conn.prepareStatement(sql); pstm.setInt(1, fatherModule.getSort());//注意从1开始 pstm.setLong(2, fatherModule.getId()); pstm.executeUpdate();//注意这里为空 } conn.commit(); } catch (SQLException e) { log.error(e.getMessage()); try { conn.rollback(); } catch (SQLException e1) { log.error("后台错误"); } return "sortId = "+firstErrorFatherModule.getSort(); } finally { if(pstm!=null) { try { pstm.close(); } catch (SQLException e) { log.error(e.getMessage()); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { log.error(e.getMessage()); } } } return null;

 

jdbc实现事务

标签:

原文地址:http://www.cnblogs.com/rocky-AGE-24/p/5929555.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!