jdbc实现事务
时间:2021-07-01 10:21:17
帮助过:2人阅读
;
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实现事务
标签: