DBUtils实现事务管理,完成转账功能(代码实现)

前期准备:*需要准备三个jar包 分别是mysql-connector-java-5.0.8-bin.jar commons-dbutils-1.4.jar c3p0-0.9.1.2.jar
*需要配置文件 c3p0-config.xml 并且把里面的数据库的名称改掉
*需要工具类 JDBCUtils.java
*需要在WebContent 下创建一个文件夹 ,创建一个jsp文件,里面写转账页面的代码
*导包必须是sql包,不能是mysql包或者其他包

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

转账页面

付款人:
收款人:
转账金额:


[2]
package com.itheima.transaction.domain;

public class Account {
private int id;
private String name;
private Double money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}

}

[3]
package com.itheima.transaction.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 com.itheima.transaction.Service.AccountService;

/**
* DBUtils实现事务管理,完成转账功能
* 付款人:from
* 收款人:to
*/
public class AccountServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
//接收数据
String from = request.getParameter(“from”);
String to = request.getParameter(“to”);
Double money = Double.parseDouble(request.getParameter(“money”));
//调用业务层处理数据
AccountService accountService = new AccountService();
accountService.transfer(from,to,money);
} catch (Exception e) {
e.printStackTrace();
}

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

}

[4]
package com.itheima.transaction.Service;

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

import org.apache.commons.dbutils.DbUtils;

import com.itheima.transaction.Dao.AccountDao;
import com.itheima.transaction.Utils.JDBCUtils;

public class AccountService {
//扩展:只有Connection对象才能管理实务,需要在业务层获得Connection对象,将Connection给DAO层才可以

public void transfer(String from, String to, Double money) throws Exception {
    //调用DAO层处理数据
    AccountDao accountDao = new AccountDao();
    //获取连接者对象,通过连接者对象conn往下传递    导sql包,不是mysql包
    Connection conn = JDBCUtils.getConnection();
    try {
        //是否手动开启事务
        conn.setAutoCommit(false);
        //在DAO层创建一个扣钱的方法
        accountDao.outMoney(conn,from,money);
        //int d = 1/0;
        //在DAO层创建一个加钱的方法
        accountDao.inMoney(conn,to,money);
        //提交事务并安静安静处理
        DbUtils.commitAndCloseQuietly(conn);
    } catch (Exception e) {
        //回滚事务并安静安静处理
        DbUtils.rollbackAndCloseQuietly(conn);
        e.printStackTrace();
    }
}

}

[5]
package com.itheima.transaction.Dao;

import java.sql.Connection;

import org.apache.commons.dbutils.QueryRunner;

public class AccountDao {
/*
*DAO扣钱的方法
*/
public void outMoney(Connection conn, String from, Double money) throws Exception {
//创建DBUtils的核心类
QueryRunner queryRunner = new QueryRunner();
String sql = “update account set money = money - ? where name = ?”;
queryRunner.update(conn, sql, money,from); //money是第一个问号 from是第二个问号
}
/*
*Dao加钱的方法
*/
public void inMoney(Connection conn, String to, Double money) throws Exception {
QueryRunner queryRunner = new QueryRunner();
String sql = “update account set money = money + ? where name = ?”;
queryRunner.update(conn, sql, money, to);
}
}

猜你喜欢

转载自blog.csdn.net/pf503603/article/details/82286691