当前位置:网站首页>Implementation of transfer business
Implementation of transfer business
2022-07-28 16:46:00 【Royal shadow time】
1. home page
<%@ 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" >
Drawee :<input type="text" value=" Xiao Ming " name="from"/></br>
payee :<input type="text" value=" Xiaohong " name="to"/></br>
amount of money :<input type="text" value="100" name="money"/></br>
<input type="submit" value=" Transfer accounts "/>
</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 {
// Prevent confusion code
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
// get data
String from = request.getParameter("from");
String to = request.getParameter("to");
String money = request.getParameter("money");
// call service
PalService ps = new PalServiceIml();
ps.pal(from, to, money);
// Echo data
response.getWriter().print(" Transfer succeeded ");
} catch (Exception e) {
response.getWriter().print(" Transfer failure ");
}
}
}
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 {
/**
* Use DBUtils Transaction management to handle the transfer business
*/
public void pal(String from, String to, String money) {
// Get the connection , Ensure that payment and collection are the same transaction
Connection conn = C3p0Utils.getConnection();
PalDao pd = new PalDaoIml();
// Open transaction
try {
conn.setAutoCommit(false);
// Call deduction
pd.reduce(conn, from, money);
// Call to add money
pd.increase(conn, to, money);
// Submit business And close resources
DbUtils.commitAndCloseQuietly(conn);
} catch (SQLException e) {
// Roll back the business , close resource
DbUtils.rollbackAndCloseQuietly(conn);
e.printStackTrace();
throw new RuntimeException();
}
}
/*
* @Override public void pal(String from, String to, String money) { // Get the connection , Ensure that payment and collection are the same transaction Connection conn = C3p0Utils.getConnection(); PalDao pd = new PalDaoIml(); try { // Turn off auto connect , Turn on manual transactions
* conn.setAutoCommit(false); // Call deduction pd.reduce(conn, from, money); // Call to add money pd.increase(conn, to, money); // Commit transaction conn.commit(); } catch (Exception e) { // Set rollback try { conn.rollback(); } catch
* (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } e.printStackTrace(); // Throw an exception throw new RuntimeException(); } finally { if (conn != null) { // close resource 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 {
// Because you need to use the same transaction , So there is no need to pass new transactions
QueryRunner run = new QueryRunner();
@Override
public void reduce(Connection conn, String from, String money) {
// Money reduction business
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();
}
}
// Plus money business
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();
}
}
}
边栏推荐
- 排序3-选择排序与归并排序(递归实现+非递归实现)
- Sort 1-insert sort and Hill sort
- About the web docking pin printer, lodop uses
- 微软100题-天天做-第11题
- HyperMesh自动保存(增强版)插件使用说明
- Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
- Optimization of network request success rate in IM instant messaging software development
- PHP计算坐标距离
- HM secondary development - data names and its use
- 后台弹出layer提示
猜你喜欢

Leetcode topic

LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数

Sort 2 bubble sort and quick sort (recursive and non recursive explanation)

排序1-插入排序与希尔排序

QT packaging

ANSYS secondary development - MFC interface calls ADPL file

Headline article_ signature

Im im development optimization improves connection success rate, speed, etc

About standard IO buffers

LeetCode-学会复杂带随机指针链表的题(详解)
随机推荐
排序1-插入排序与希尔排序
Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
配置web服务器步骤详细记录(多有借鉴)
Leetcode learn complex questions with random pointer linked lists (detailed explanation)
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
Detailed record of steps to configure web server (many references)
Two of C language programming!! Role of
LeetCode每日一练 —— 160. 相交链表
QT packaging
优化Hypermesh脚本性能的几点建议
Microsoft question 100 - do it every day - question 11
Ansa secondary development - apps and ansa plug-in management
Geodetic coordinate system to Martian coordinate system
2021-04-02
LeetCode每日一练 —— 剑指Offer 56 数组中数字出现的次数
排序5-计数排序
ABAQUS GUI interface solves the problem of Chinese garbled code (plug-in Chinese garbled code is also applicable)
“蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
Splash (rendering JS service) introduction installation
“蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数