JavaWeb 学习笔记 6——事务实例,我的支付宝 3 面 + 美团 4 面 + 拼多多四面
<%@ page import="java.sql.*"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.sql.Timestamp"%>
<div style="padding: 2px; text-align: left;">
<a href="${pageContext.request.requestURI}">只显示余额</a> <br>
<a href="${pageContext.request.requestURI}?action=a2b">A 向 B 转账 200 元</a><br>
<a href="${pageContext.request.requestURI}?action=b2a">B 向 A 转账 200 元</a>
</div>
<%
Connection conn=null;
Statement stmt=null;
ResultSet rs=null;
try{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());//注册驱动
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/account","root","root");//获取数据库连接
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from tb_currency");//查询结果集
%>
<table bgcolor="#CCCCCC" cellspacing=1 cellpadding=5 width=100%>
<caption ><FONT SIZE=4><B>转账记录表</B></FONT></caption>
<tr bgcolor=#DDDDDD>
<th>账号</th>
<th>余额</th>
<th>最后修改日期</th>
</tr>
<%
while(rs.next())
{
String account=rs.getString("account");//获取账户名
double currency=rs.getDouble("currency");//获取账户余额
String last_modifield=rs.getTimestamp("last_modified").toString();//获取上次操作时间
out.println("<tr bgcolor=#FFFFFF>");
out.println("<td align=center>"+account+"</td>");//将账户名显示在表中
out.println("<td align=center>"+currency+"</td>");//将余额显示在表中
out.println("<td ali
gn=center>"+last_modifield+"</td>"); //将上次操作时间显示在表中
out.println("</tr>");
}
}catch(SQLException e)
{
out.println("发生异常:"+e.getMessage());
e.printStackTrace();
}finally{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}
%>
</table>
<%
String action=request.getParameter("action");//获取 action 操作
if("a2b".equals(action))
{
out.println("业务:A 向 B 转账 200 元。<br/>");
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?"成功":"失败")+"<br/>");
String sql2="UPDATE tb_currency SET currency=currency+200 WHERE account='B'";
int result2=stmt.executeUpdate(sql2);
out.println("B 账号进款 200 元,结果:"+ (result2==1?"成功":"失败")+"<br/>");
if(result1==1 && result2==1)
{
conn.commit();//提交事务
out.println("转账成功,事务提交。<a href='listCurrency.jsp'>点此刷新账表</a>");
}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 元。<br/>");
评论