jdbc实现事务

//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;
上一篇:centos 7 安装vmware 12


下一篇:【Struts2】 国际化