zoukankan      html  css  js  c++  java
  • 事物 银行转账业务

    //web层

    package cn.jy.web;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class Transfer1 extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    request.setCharacterEncoding("utf-8");
    String out = request.getParameter("out");
    String in = request.getParameter("in");
    String moneystr = request.getParameter("money");
    response.setContentType("text/html;charset=utf-8");
    double money = Double.parseDouble(moneystr);
    Transfer1service tf= new Transfer1service();
    boolean success= tf.transfer(out,in,money);
    if(success){
    response.getWriter().write("成功");
    }else{
    response.getWriter().write("失败");

    }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    doGet(request, response);
    }

    }

    //service层

    package cn.jy.web;

    import java.sql.Connection;
    import java.sql.SQLException;

    import cn.jy.utils.DataSourceUtils;

    public class Transfer1service {
    public boolean transfer(String out ,String in,double money){
    TransferDao1 dao=new TransferDao1();
    boolean istrancefersuccess=true;
    Connection con=null;
    try {
    con=DataSourceUtils.getConnection();
    con.setAutoCommit(false);
    dao.out(con,out,money);
    dao.in(con,in,money);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    try {
    istrancefersuccess=false;
    con.rollback();
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    e.printStackTrace();
    }finally{
    try {
    con.commit();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    return istrancefersuccess;
    }
    }

    //dao层

    package cn.jy.web;

    import java.sql.Connection;
    import java.sql.SQLException;

    import org.apache.commons.dbutils.QueryRunner;

    public class TransferDao1 {
    public void out(Connection con,String out ,double money) throws SQLException{
    QueryRunner queryRunner = new QueryRunner();
    String sql="update account set money=money-? where name=?";
    queryRunner.update(con,sql,money,out);
    }
    // public void out(Connection con ,String out,double money) throws SQLException{
    // QueryRunner run = new QueryRunner();
    // String sql="update account set money=money-? where name=?";
    // run.update(con,sql,money,out);
    // }
    public void in(Connection con ,String in,double money) throws SQLException{
    QueryRunner run = new QueryRunner();
    String sql="update account set money=money+? where name=?";
    run.update(con,sql,money,in);
    }

    }

    //transfer  jsp文件  页面的设计

    <%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form method="post" action="${pageContext.request.contextPath}/transfer1">
    转出账户:<input type="text" name="out"/><br/>
    转入账户:<input type="text" name="in"/><br/>
    转入金额:<input type="text" name="money"/><br/>
    <input type="submit" value="确认转账">
    </form>
    </body>
    </html>

  • 相关阅读:
    std thread
    windows更新包发布地址
    How to set up logging level for Spark application in IntelliJ IDEA?
    spark 错误 How to set heap size in spark within the Eclipse environment?
    hadoop 常用命令
    windows 安装hadoop 3.2.1
    windows JAVA_HOME 路径有空格,执行软连接
    day01MyBatisPlus条件构造器(04)
    day01MyBatisPlus的CRUD 接口(03)
    day01MyBatisPlus入门(02)
  • 原文地址:https://www.cnblogs.com/Fisherman13/p/10510200.html
Copyright © 2011-2022 走看看