事物 银行转账业务

//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>

猜你喜欢

转载自www.cnblogs.com/Fisherman13/p/10510200.html