设计前端jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
<title>银行账户转账</title>
</head>
<body>
<form action="transfer" method="post">
转出账户:<input type="text" name="fromActno"><br>
转入账户:<input type="text" name="toActno"><br>
转账金额:<input type="text" name="money"><br>
<input type="submit" value="转账">
</form>
</body>
</html>
Servlet业务处理
package com.powernode.bank.web.servlet;
import com.powernode.bank.exceptions.AppException;
import com.powernode.bank.exceptions.MoneyNotEnoughException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
/**
* 不使用mvc架构模式下完成银行账户转账
* 分析程序存在的问题
* 缺点:
* 1、代码复用性差,代码和代码之间的耦合度太高
* 2、耦合度高导致代码很难扩展
* 3、操作数据库的代码和业务逻辑代码混杂在一起
* 4、
*
* 以下程序负责了什么
* 1> 负责了数据的接收
* 2> 负责了核心的业务处理
* 3> 负责了数据库表中的CRUD操作
* 4> 页面的数据展示
*
* @author b
* @version 1.0
* @since 1.0
*/
@WebServlet("/transfer")
public class AccountTransferServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取响应流对象
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// 获取转账相关的信息
String fromActno = request.getParameter("fromActno");
String toActno = request.getParameter("toActno");
double money = Double.parseDouble(request.getParameter("money"));
// 编写转账的业务逻辑代码,连接数据库,进行转账操作
// 1、转账之前要判断转出账户的余额是否充足
Connection conn = null;
PreparedStatement ps = null;
PreparedStatement ps2 = null;
PreparedStatement ps3 = null;
ResultSet rs = null;
try {
// 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 获取连接
String url = "jdbc:mysql://localhost:3306/mvc";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
// 获取预编译的数据库操作对象
String sql1 = "select balance from t_act where actno = ?";
ps = conn.prepareStatement(sql1);
ps.setString(1,fromActno);
// 执行SQL语句,返回结果集
rs = ps.executeQuery();
if (rs.next()) {
double balance = rs.getDouble("balance");
if(balance < money){
// 余额不足,使用异常机制处理
throw new MoneyNotEnoughException("余额不足");
}
// 执行到这,说明余额充足
// 开始转账
// act001账户减去money
// 开启事务,(不再自动提交,改为手动提交)
conn.setAutoCommit(false);
String sql2 = "update t_act set balance = balance - ? where actno = ?";
ps2 = conn.prepareStatement(sql2);
ps2.setDouble(1, money);
ps2.setString(2, fromActno);
int count = ps2.executeUpdate();
// act002账户加上money
String sql3 = "update t_act set balance = balance + ? where actno = ?";
ps3 = conn.prepareStatement(sql3);
ps3.setDouble(1, money);
ps3.setString(2, toActno);
// 累计
count = ps3.executeUpdate();
if (count != 2) {
throw new AppException("App异常,请联系管理员");
}
// 手动提交事务
conn.commit();
// 转账成功
out.print("转账成功");
}
} catch (Exception e) {
// 回滚事务
try {
if (conn != null) {
conn.rollback();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
// 异常处理
e.printStackTrace();
out.print(e.getMessage());
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException();
}
}
// 释放资源
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException();
}
}
if (ps2 != null) {
try {
ps2.close();
} catch (SQLException e) {
throw new RuntimeException();
}
}
if (ps3 != null) {
try {
ps3.close();
} catch (SQLException e) {
throw new RuntimeException();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException();
}
}
}
}
}
异常处理
package com.powernode.bank.exceptions;
/**
* App异常
* @author b
* @version 1.0
* @since 1.0
*/
public class AppException extends Exception{
public AppException() {
}
public AppException(String message) {
super(message);
}
}
package com.powernode.bank.exceptions;
/**
* 余额不足异常
* @author b
* @version 1.0
* @since 1.0
*/
public class MoneyNotEnoughException extends Exception{
public MoneyNotEnoughException() {
}
public MoneyNotEnoughException(String message) {
super(message);
}
}