1.首页
<%@ 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>
<div align="center">
<form action="${pageContext.request.contextPath }/count" method="post" >
付款人:<input type="text" value="小明" name="from"/></br>
收款人:<input type="text" value="小红" name="to"/></br>
金额:<input type="text" value="100" name="money"/></br>
<input type="submit" value="转账"/>
</form>
</div>
</body>
</html>
2.servlet
package yynh.com.pal.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import yynh.com.pal.service.PalService;
import yynh.com.pal.service.PalServiceIml;
/**
* Servlet implementation class Count
*/
public class Count extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 防止乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// 获取数据
String from = request.getParameter("from");
String to = request.getParameter("to");
String money = request.getParameter("money");
// 调用service
PalService ps = new PalServiceIml();
ps.pal(from, to, money);
// 回显数据
response.getWriter().print("转账成功");
} catch (Exception e) {
response.getWriter().print("转账失败");
}
}
}
3.service
package yynh.com.pal.service;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import yynh.com.pal.dao.PalDao;
import yynh.com.pal.dao.PalDaoIml;
import yynh.com.pal.utils.C3p0Utils;
public class PalServiceIml implements PalService {
/**
* 使用DBUtils的事务管理来处理转账业务
*/
public void pal(String from, String to, String money) {
// 获取连接,保证付款和收款是用的同一个事务
Connection conn = C3p0Utils.getConnection();
PalDao pd = new PalDaoIml();
// 开启事务
try {
conn.setAutoCommit(false);
// 调用扣钱
pd.reduce(conn, from, money);
// 调用加钱
pd.increase(conn, to, money);
// 提交业务 并且关闭资源
DbUtils.commitAndCloseQuietly(conn);
} catch (SQLException e) {
// 回滚业务,关闭资源
DbUtils.rollbackAndCloseQuietly(conn);
e.printStackTrace();
throw new RuntimeException();
}
}
/*
* @Override public void pal(String from, String to, String money) { // 获取连接,保证付款和收款是用的同一个事务 Connection conn = C3p0Utils.getConnection(); PalDao pd = new PalDaoIml(); try { // 关闭自动连接,开启手动事务
* conn.setAutoCommit(false); // 调用扣钱 pd.reduce(conn, from, money); // 调用加钱 pd.increase(conn, to, money); // 提交事务 conn.commit(); } catch (Exception e) { // 设置回滚 try { conn.rollback(); } catch
* (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); // 抛出异常 throw new RuntimeException(); } finally { if (conn != null) { // 关闭资源 try {
* conn.close(); } catch (Exception e) { e.printStackTrace(); } }
*
* }
*
* }
*/
}
4.dao
package yynh.com.pal.dao;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
public class PalDaoIml implements PalDao {
// 由于需要使用同一个事务,所以不需要传递新的事务了
QueryRunner run = new QueryRunner();
@Override
public void reduce(Connection conn, String from, String money) {
// 减钱业务
String sql = "update account set money=money-? where name=?";
Object[] param = { money, from };
try {
run.update(conn, sql, param);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
// 加钱业务
public void increase(Connection conn, String to, String money) {
String sql = "update account set money=money+? where name=?";
Object[] param = { money, to };
try {
run.update(conn, sql, param);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
}