<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'zhuanzhang.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>转账页面</h1>
<form action="${pageContext.request.contextPath}/AccountServlet"
method="post">
付款人<input type="text" name="from"> 收款人<input type="text"
name="to"> 转账金额<input type="text" name="money"> <input
type="submit" value="转账">
</form>
</body>
</html>
package transaction;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AccountServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//接收参数
String from=request.getParameter("from");
String to=request.getParameter("to");
Double money=Double.parseDouble(request.getParameter("money"));
//处理数据
AccountService as=new AccountService();
try {
as.transfer(from,to,money);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet( request, response);
}
}
package transaction;
import java.sql.Connection;
import java.sql.SQLException;
import utils.c3p0tool;
public class AccountService {
//业务层完成转账方法
public void transfer(String from, String to, Double money) throws SQLException {
// TODO Auto-generated method stub
AccountDao ad=new AccountDao();
//方式一:在业务层获得Connection,传递给Dao
Connection con = c3p0tool.getConnection();
con.setAutoCommit(false);//不让数据库一步一步执行
try {
ad.outmoney(from,money);
ad.inmoney(to,money);
con.commit();
} catch (Exception e) {//如果发生异常
// TODO Auto-generated catch block
con.rollback();
}
public void transfer2(String from, String to, Double money) throws SQLException {
// TODO Auto-generated method stub
AccountDao ad=new AccountDao();
//方式二:将connection绑定在当前线程
try {
c3p0tool.beginTransaction();
ad.outmoney(from,money);
ad.inmoney(to,money);
c3p0tool.commitTransaction();
} catch (Exception e) {//如果发生异常
// TODO Auto-generated catch block
c3p0tool.rollback();
}
}
}
}
package transaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import utils.c3p0tool;
public class AccountDao {
public void outmoney(String from, Double money) throws SQLException {
// TODO Auto-generated method stub
//获得连接
/*Connection con = c3p0tool.getConnection();*/
Connection con = c3p0tool.getConnection();
//编写sql语句
String sql="update account set money=money-? where name=?";
//预编译sql
PreparedStatement pst = con.prepareStatement(sql);
pst.setDouble(1, money);
pst.setString(2, from);
pst.executeUpdate();
}
public void inmoney(String to, Double money) throws SQLException {
// TODO Auto-generated method stub
//获得连接
/*Connection con = c3p0tool.getConnection();*/
Connection con = c3p0tool.getConnection();
//编写sql语句
//int z=1/0;
String sql="update account set money=money+? where name=?";
//预编译sql
PreparedStatement pst = con.prepareStatement(sql);
pst.setDouble(1, money);
pst.setString(2, to);
pst.executeUpdate();
}
}
package utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class c3p0tool {
private static final ComboPooledDataSource datasource=new ComboPooledDataSource("");
private static final ThreadLocal<Connection>tl=new ThreadLocal<Connection>();
//将connection绑定在当前线程中
public static Connection getConnection(){
Connection con = null;
//
try {
con=tl.get();
if(con==null){
con=datasource.getConnection();
tl.set(con);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public static void update(String sql,Object...objects) throws SQLException{
Connection con=getConnection();
PreparedStatement prs = con.prepareStatement(sql);
int count=prs.getParameterMetaData().getParameterCount();
for(int i=0;i<count;i++){
prs.setObject(i+1,objects[i]);
}
prs.executeUpdate();
jdbctool.release(prs,con);
}
public static ComboPooledDataSource getDataSource(){
return datasource;
}
public static void beginTransaction() throws SQLException{
Connection con = c3p0tool.getConnection();
con.setAutoCommit(false);
}
public static void commitTransaction() throws SQLException{
Connection con = tl.get();
con.commit();
}
public static void rollback() throws SQLException{
Connection con = tl.get();
con.rollback();
}
}
package transaction;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import utils.c3p0tool;
public class AccountService {
//业务层完成转账方法
public void transfer(String from, String to, Double money) throws SQLException {
AccountDao ad=new AccountDao();
Connection con = c3p0tool.getConnection();
con.setAutoCommit(false);//不让数据库一步一步执行
try {
ad.outmoney(con,from,money);
ad.inmoney(con,to,money);
DbUtils.commitAndCloseQuietly(con);
}catch(Exception e){
DbUtils.rollbackAndCloseQuietly(con);
}
}
}
package transaction;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import utils.c3p0tool;
public class AccountDao {
public void outmoney(Connection con, String from, Double money) throws SQLException {
// TODO Auto-generated method stub
QueryRunner qr=new QueryRunner();
String sql="update account set money=money-? where name=?";
qr.update(con, sql, money,from);
}
public void inmoney(Connection con, String to, Double money) throws SQLException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
QueryRunner qr=new QueryRunner();
String sql="update account set money=money+? where name=?";
qr.update(con, sql, money,to);
//int z=1/0;
}
}