{
out.println(“发生异常:”+e.getMessage());
e.printStackTrace();
}finally{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
%>
<%
String action=request.getParameter(“action”);//获取action操作
if(“a2b”.equals(action))
{
out.println(“业务:A向B转账200元。
”);
try
{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/account”,“root”,“root”);
conn.setAutoCommit(false);//将自动提交设为false
stmt=conn.createStatement();
String sql1="UPDATE tb_currency SET currency=currency-200 WHERE account='A’and currency>=200 ";
int result1=stmt.executeUpdate(sql1);
out.println(“A账号扣款200元,结果:”+ (result1==1?“成功”:“失败”)+“
”);
String sql2=“UPDATE tb_currency SET currency=currency+200 WHERE account=‘B’”;
int result2=stmt.executeUpdate(sql2);
out.println(“B账号进款200元,结果:”+ (result2==1?“成功”:“失败”)+“
”);
if(result11 && result21)
{
conn.commit();//提交事务
out.println(“转账成功,事务提交。点此刷新账表”);
}else{
conn.rollback();//事务回滚
out.println(“转账失败,事务回滚。”);
}
}finally{
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
}else if(“b2a”.equals(action))
{
out.println(“业务:B向A转账200元。
”);
try
{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());//注册驱动
conn=DriverManager.getConnection(“jdbc:mysql://localhost:3306/account”,“root”,“root”);//获取连接
conn.setAutoCommit(false);//将自动提交设为false
stmt=conn.createStatement();
String sql1="UPDATE tb_currency SET currency=currency-200 WHERE account='B’and currency>=200 ";
int result1=stmt.executeUpdate(sql1);
out.println(“B账号扣款200元,结果:”+ (result1==1?“成功”:“失败”)+“
”);
String sql2=“UPDATE tb_currency SET currency=currency+200 WHERE account=‘A’”;
int result2=stmt.executeUpdate(sql2);
out.println(“A账号进款200元,结果:”+ (result2==1?“成功”:“失败”)+“
”);
if(result11 && result21)
{
conn.commit();//提交事务
out.println(“转账成功,事务提交。 点此刷新账表”);
}else{
conn.rollback();//事务回滚
out.println(“转账失败,事务回滚。”);
}
}finally{
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
}
%>
A向B转200,操作成功,事务提交
B向A转200,由于B余额不足,操作失败,事务回滚。
总结
在清楚了各个大厂的面试重点之后,就能很好的提高你刷题以及面试准备的效率,接下来小编也为大家准备了最新的互联网大厂资料。
的互联网大厂资料。
[外链图片转存中…(img-4RvxH6Cl-1719264179661)]
[外链图片转存中…(img-vjei2nis-1719264179662)]
[外链图片转存中…(img-Zv7qwyjR-1719264179662)]
[外链图片转存中…(img-EIlWzUxa-1719264179663)]