转账汇款案例

垃圾版代码,只提供一个思路

第一个版本

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP ‘index.jsp‘ starting page</title>

</head>

<body>
    <form action="${pageContext.request.contextPath}/account" method="post">
        转款人<input type="text" name="outuser"> 收款人<input type="text"
            name="inuser"> 金额<input type="text" name="money"> <input
            type="submit" value="转账">
    </form>
</body>
</html>

servlet

public class AccountServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        // 1.收集数据
        String outuser = request.getParameter("outuser");
        String inuser = request.getParameter("inuser");
        String money = request.getParameter("money");

        // 2.调用AccountService中的account方法

        AccountService service = new AccountService();

        try {
            service.account(outuser, inuser, money);

            response.getWriter().write("汇款成功");
        } catch (AccountException e) {
            e.printStackTrace();
            response.getWriter().write(e.getMessage());
        }

    }

}

service

public class AccountService {
    // 原始转账方法
    public void _account(String outuser, String inuser, String money)
            throws AccountException {
        AccountDao dao = new AccountDao();

        // 获取连接对象,并开启事务
        Connection con = null;

        try {
            con = JdbcUtils.getConnection();
            con.setAutoCommit(false); // 开启事务
            // 调用AccountDao中两个方法
            // 转出
            dao.accountOut(con, outuser, money);
            // 转入
            dao.accountIn(con, inuser, money);
        } catch (SQLException e) {
            e.printStackTrace();
            // 事务回滚
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            throw new AccountException("转账失败");
        } catch (AccountException e) {
            e.printStackTrace();
            // 事务回滚
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            throw e;
        } finally {

            try {
                con.commit(); // 提交事务
                con.close(); // 关闭资源
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    // 转账方法
    public void account(String outuser, String inuser, String money)
            throws AccountException {
        AccountDao dao = new AccountDao();

        try {
            // 开启事务
            JdbcUtils.startTransaction();
            // 转出
            dao.accountOut(outuser, money);
            // 转入
            dao.accountIn(inuser, money);
        } catch (SQLException e) {
            e.printStackTrace();
            // 事务回滚
            try {
                JdbcUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            throw new AccountException("转账失败");
        } catch (AccountException e) {
            e.printStackTrace();
            // 事务回滚
            try {
                JdbcUtils.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            throw e;
        } finally {

            try {
                JdbcUtils.commit();// 提交事务
                JdbcUtils.closeConnection(); // 关闭资源
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

dao

public class AccountDao {

    // 转入
    public void accountIn(Connection con, String inuser, String money)
            throws SQLException, AccountException {
        String sql = "update account set money=money+? where username=?";

        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, money);
        pst.setString(2, inuser);

        int row = pst.executeUpdate();
        if (row == 0) {
            throw new AccountException("转入失败");
        }
        pst.close();
    }

    // 转出
    public void accountOut(Connection con, String outuser, String money)
            throws SQLException, AccountException {

        String sql = "update account set money=money-? where username=?";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, money);
        pst.setString(2, outuser);

        int row = pst.executeUpdate();
        if (row == 0) {
            throw new AccountException("转出失败");
        }
        pst.close();
    }

    // 转入--使用ThreadLocal获取连接
    public void accountIn(String inuser, String money) throws SQLException,
            AccountException {
        Connection con = JdbcUtils.getConnection();
        String sql = "update account set money=money+? where username=?";

        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, money);
        pst.setString(2, inuser);

        int row = pst.executeUpdate();
        if (row == 0) {
            throw new AccountException("转入失败");
        }
        pst.close();
    }

    // 转出
    public void accountOut(String outuser, String money) throws SQLException,
            AccountException {
        Connection con = JdbcUtils.getConnection();
        String sql = "update account set money=money-? where username=?";
        PreparedStatement pst = con.prepareStatement(sql);
        pst.setString(1, money);
        pst.setString(2, outuser);

        int row = pst.executeUpdate();
        if (row == 0) {
            throw new AccountException("转出失败");
        }
        pst.close();
    }
}

exception

public class AccountException extends Exception {

    public AccountException() {
        super();
    }

    public AccountException(String message, Throwable cause) {
        super(message, cause);
    }

    public AccountException(String message) {
        super(message);
    }

    public AccountException(Throwable cause) {
        super(cause);
    }

}
//高级的,使用ThreadLocal
public class JdbcUtils {

    private static final String DRIVERCLASSNAME;
    private static final String URL;
    private static final String USER;
    private static final String PASSWORD;

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        DRIVERCLASSNAME = bundle.getString("DRIVERCLASSNAME");
        URL = bundle.getString("URL");
        USER = bundle.getString("USER");
        PASSWORD = bundle.getString("PASSWORD");
    }

    static {
        try {
            Class.forName(DRIVERCLASSNAME);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private static final ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); // 它就相当于是一个Map集合。

    public static Connection getConnection() throws SQLException {

        // Connection con = tl.get(); // 从ThreadLocal中获取,第一次一得到的一定是null.
        // if (con == null) {
        Connection con = DriverManager.getConnection(URL, USER, PASSWORD);
        // tl.set(con);// 将con装入到ThreadLocal中。
        // }
        return con;
    }

    // 封装事务操作.
    // 开启事务
    public static void startTransaction() throws SQLException {
        Connection con = getConnection();
        con.setAutoCommit(false);
    }

    public static void rollback() throws SQLException {
        Connection con = getConnection();
        con.rollback();
    }

    // 事务提交
    public static void commit() throws SQLException {
        Connection con = getConnection();
        con.commit();
    }

    public static void closeStatement(Statement st) throws SQLException {
        if (st != null)
            st.close();
    }

    public static void closeResultSet(ResultSet rs) throws SQLException {
        if (rs != null)
            rs.close();
    }

ThreadLocal

它的底层是使用了一个Map集合

Map<Thread,Object>

它的key就是当前的线程对象.

set(Object obj) 它就相当于  map.put(Thread.currentThread(),obj);

get()它就相当于 map.get(Thread.currentThread()));

时间: 2024-08-24 02:53:01

转账汇款案例的相关文章

转账汇款总结

1.招商银行转到其他银行,实时可以到账 2.工商银行转账到招商银行,也是1分钟内到账. ------------------------------ 总结,也就是说招商银行一进一出都是实时的. 通过支付宝用 工商银行转到中国银行,要好久才能到账,没有选择2个小时的时候.

转账案例中引入事务

JSP模式&JDBC 使用MVC设计模式开发一个转账案例 JSP的回顾 JSP : * JSP的概述: * JSP:Java Server Pages. * JSP的运行原理:翻译成Servlet,编译成Class进行执行. * JSP的脚本元素: * <%!   %> * <%    %> * <%=   %> * JSP的注释: * JSP的三个指令: * page,include,taglib   <%@ 指令名称 属性="属性值"

【JDBC】实现JDBC实现银行的转账事务

JDBC中的事务是默认提交的,也就是说每执行一次PreparedStatement,那么数据就会被写入到磁盘.如果需要关闭默认提交,使用  void setAutoCommit(false)  . db.properties driverClassName=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@localhost:1521:xe username=system password=517839 db.properties JDBCUtilPr

2015 史考特(Scottrade)开户指南 + 招行香港一卡通汇款

最近刚开始炒美股.总的来说分为两步:一是开户,即选一个美股券商开设股票交易账户:二是汇款注资,把人民币换成美元转账到股票交易账户上.上述第一点其实相对简单,美股券商大多都对美国以外的外国人开放申请,且开户流程在网上就能搞定:第二点稍微麻烦,这是由于我国严格的外汇管制,把钱弄出国并不容易. 我选择的券商是Scottrade(史考特证券),开户过程全部通过网上办理(偶尔会接一两个电话),全程中文:汇款是通过招行香港一卡通,去了一次北京的招行网点,其他全在网上搞定.下面简单介绍一下我的流程和经验. 1

ATM-JAVA程序 //程序有5处相同错误,找不出原因 转账功能没有实现,修改密码来不及实现了

package JCC;//信1705-3 20173681 靳晨晨import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Scanner;import java.util.StringTokenizer;im

互联网金融的前世、今生和未来-系列四(今生):百花齐放的互联网金融业态

互联网金融的前世.今生和未来--系列一:山雨欲来 互联网金融的前世.今生和未来-系列二(前世):金融与技术的首次亲密接触之金融电子化 互联网金融的前世.今生和未来-系列三(今生):一场跨界的战争 今生:金融与互联网的深度融合--互联网金融 1.日益便捷的移动支付 移动支付指依托无线通信和移动互联网技术,通过智能手机或其他移动终端设备实现的电子支付,主要分为近场支付和远程支付两种方式.近场支付指通过近场支付终端向商家进行非接触式支付,交易数据在现场通过手机射频.红外.蓝牙以及NFC技术(主流技术)

iOS APP安全杂谈

iOS APP安全杂谈 高小厨 · 2015/06/30 10:16 0x00 序 以前总是在这里看到各位大牛分享其安全渗透经验,而今我也很荣幸的收到了乌云的约稿,兴奋之情难以言表.由于IOS是一种闭源的操作系统,源码恐怕也只有几个人见过,其安全性究竟如何我们不得而知,突然想起一段话:恐惧来源于我们的无知.好在国内早有大牛团队—盘古团队总是走在世界的前沿给我们带来最新鲜的IOS安全详解,感谢沙梓社和吴航的<IOS应用逆向工程>让我对IOS逆向充满兴趣,感谢念茜的博客将我领入了IOS安全之门.

Java进阶学习第十八天——事物与连接池

文档版本 开发工具 测试平台 工程名字 日期 作者 备注 V1.0 2016.05.13 lutianfei none 事务 事务的概念 事务就是一个事情,组成这个事情可能有多个单元,要求这些单元,要么全都成功,要么全都不成功. 在开发中,有事务的存在,可以保证数据完整性. 例如:A--B转帐,对应于如下两条sql语句 update account set money=money-100 where name='a'; update account set money=money+100 whe

中国银行app下载|中国银行app安卓版下载

中银在线app是我非常喜欢的一款国内顶级的知名金融应用平台,在软件中拥有深厚的金融资金背景和强大的金融链支撑,力求为客户提供超高的服务品质,里面还有全面的金融学习课程表,各类财经知识随便学习非常的好用.中国银行app链接应用简介中国银行app是中国银行推出的一款手机×××端,对一些手机银行查询版客户.理财版客户或者是贵宾版客户比较适合这款软件,而且只要是注册了中国手机银行的用户,都可以免费享受账户管理.转账汇款.投资理财.信用卡.账单缴付.电子支付等这些银行服务,并且还包括优惠活动.网点查询.掌