• JDBC事务(二)转账示例


    示例采用三层框架

    web层:

    package cn.sasa.web;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import cn.sasa.service.TransferService;
    
    public class TransferServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String outAccount = request.getParameter("outAccount");
            String inAccount = request.getParameter("inAccount");
            double money = Double.parseDouble(request.getParameter("money"));
            TransferService tran = new TransferService();
            boolean flag =  tran.doTran(outAccount, inAccount, money);
            if(flag) {
                response.getWriter().write("ok");
            }else {
                response.getWriter().write("no");
            }
        }
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            doGet(request, response);
        }
    }

    service层:

    package cn.sasa.service;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import cn.sasa.dao.TransferDao;
    import cn.sasa.util.C3P0Utils;
    
    public class TransferService {
    
        public boolean doTran(String outAccount, String inAccount, double money) {
            Connection conn = C3P0Utils.getConnection();
            boolean flag = true;
            try {
                conn.setAutoCommit(false);
                TransferDao tran = new TransferDao();
                int rs1 = tran.doOutAccount(conn, outAccount, money);
                int rs2 = tran.doInAccount(conn, inAccount, money);
                
                if(rs1<=0 || rs2<=0) {
                    conn.rollback();
                    flag=false;
                }
                
            } catch (Exception e) {
                flag = false;
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }finally {
                try {
                    conn.commit();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return flag;
        }
    
    }

    dao层:

    package cn.sasa.dao;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import org.apache.commons.dbutils.QueryRunner;
    
    public class TransferDao {
        
        //资金转出
        public int doOutAccount(Connection conn, String outAccount, double money) throws SQLException {
            QueryRunner runner = new QueryRunner();
            String sql = "update account set money=money-? where name=?";
            int rs = runner.update(conn, sql, money,outAccount);
            return rs;
        }
        
        //资金转入
        public int doInAccount(Connection conn, String inAccount, double money) throws SQLException {
            QueryRunner runner = new QueryRunner();
            String sql = "update account set money=money+? where name=?";
            int rs = runner.update(conn, sql, money,inAccount);
            return rs;
        }
    }

    客户端jsp页:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="${pageContext.servletContext.contextPath}/TransferServlet" method="post">
            转出账户:<input type="text" name="outAccount" value=""/><br/>
            转入账户:<input type="text" name="inAccount" value=""/><br/>
            转账金额:<input type="text" name="money" value=""/><br>
            <input type="submit" value="确定">
        </form>
    </body>
    </html>

    事务的注意事项:

    手动开启事务都用 conn.setAutoCommit(false);

    要用同一个conn控制事务。

  • 相关阅读:
    Codeforces Round #392 (Div. 2)
    hihocoder #1419 : 后缀数组四·重复旋律4
    hihocoder #1415 : 后缀数组三·重复旋律3
    LOJ #6284. 数列分块入门 8-分块(区间查询等于一个数c的元素,并将这个区间的所有元素改为c)
    LOJ #6283. 数列分块入门 7-分块(区间乘法、区间加法、单点查询)
    LOJ #6282. 数列分块入门 6-分块(单点插入、单点查询、数据随机生成)
    LOJ #6281. 数列分块入门 5-分块(区间开方、区间求和)
    LOJ #6280. 数列分块入门 4-分块(区间加法、区间求和)
    LOJ #6279. 数列分块入门 3-分块(区间加法、查询区间内小于某个值x的前驱(比其小的最大元素))
    LOJ #6278. 数列分块入门 2-分块(区间加法、查询区间内小于某个值x的元素个数)
  • 原文地址:https://www.cnblogs.com/SasaL/p/10642627.html
Copyright © 2020-2023  润新知