mysql数据库事务没起作用!
String strSql= "delete from "+tables+ " where id= "+id;
String strSql_delCzcs= "delete from ChaoZuo where SheBei= "+id;
String strSql_delJx= "delete from JianXiu where SheBeiLeiBie= "+lb+ " and SheBei= "+id;
String strSql_delQx= "delete from QueXian where SheBeiId= "+id;
String strSql_delTz= "delete from TiaoZa where SheBei= "+id;
String strSql_delYd= "delete from YiDongJiLu where SheBei= "+id;
String strSql_delTy= "delete from TiaoYa where SheBei= "+id;
Connection conn=cons.Connect();
try
{
Statement stmt=conn.createStatement();
conn.setAutoCommit(false);
stmt.executeUpdate(strSql);
stmt.executeUpdate(strSql_delCzcs);
stmt.executeUpdate(strSql_delJx);
stmt.executeUpdate(strSql_delQx);
stmt.executeUpdate(strSql_delTz);
stmt.executeUpdate(strSql_delYd);
stmt.executeUpdate(strSql_delTy);//这一句出错
conn.commit();
stmt.close();
conn.close();
request.getRequestDispatcher( "Error.jsp?ts=删除成功 ").forward(request,response);
return;
}
catch(Exception e)
{
request.getRequestDispatcher( "Error.jsp?ts=数据库操作错误 ").forward(request,response);
其中最后一句的表已经被删除,所以提示数据库操作错误,但我发现,前面几句sql语句已经执行了,这是怎么回事?
}
[解决办法]
catch(Exception e)
{
conn.rollback();
request.getRequestDispatcher( "Error.jsp?ts=数据库操作错误 ").forward(request,response);
}
[解决办法]
mysql较老的版本不支持事务
[解决办法]
catch(SQLException e)
{
try
{
conn.rollback();
}
catch(SQLException e1)
{
}
request.getRequestDispatcher( "Error.jsp?ts=数据库操作错误 ").forward(request,response);
}
这里rollback也会产生异常,需要捕捉
[解决办法]
如果你不用框架的话 ,事务操作一般都是try...catch...模式了,可以学习下spring