当前位置:网站首页>Requirements for transfer transaction cases: 1 Employee 1 transfers money to employee 2. Therefore, two update sals should be executed. Purpose: either both updates are successful or both implementati
Requirements for transfer transaction cases: 1 Employee 1 transfers money to employee 2. Therefore, two update sals should be executed. Purpose: either both updates are successful or both implementati
2022-06-30 04:31:00 【A fat man】
package com.htf.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.htf.util.DBUtils;
/**
* Transfer accounts , Open transaction , Or at the same time , Or fail at the same time
* @author : htf
* Transaction case requirements :
* 1. staff 1 To employees 2 Transfer accounts So we need to execute two update sal
* Purpose : Or they are all updated successfully , Or they all fail
*/
public class Test {
private static final String UPDATE_EMP="update t_emp set sal=sal+? where empno=?";
/**
* Transfer accounts Transfer according to the number
* @param from Number always
* @param to No. 2 transfer
* @param money How much is it
* @return ture It means the transfer is successful ,false Transfer failure
*/
public boolean transfer(Integer from,Integer to,Double money) {
boolean result =false;
Connection conn=null;
PreparedStatement stmt=null;
try {
conn=DBUtils.getConnection();
conn.setAutoCommit(false);// Turn off auto commit transactions
stmt=conn.prepareStatement(UPDATE_EMP);
stmt.setDouble(1, -money);
stmt.setInt(2, from);
int rows1=stmt.executeUpdate();
stmt.setDouble(1, money);
stmt.setInt(2, to);
int rows2=stmt.executeUpdate();
if(rows1==1 && rows2==1) {
result=true;// Transfer succeeded
conn.commit();// Commit transaction
}
} catch (Exception e) {
e.printStackTrace();// Transfer failure
try {
conn.rollback();// Roll back the transaction
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
Test t1=new Test();
t1.transfer(4446, 4447, 1.0);
}
}
边栏推荐
- Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
- 基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
- FortiGate firewall modifies the default timeout of a session
- File and IO
- IO stream, character read / write, copy
- FortiGate firewall configuration link detection link monitor and status query
- Es2019 key summary
- El upload upload file (manual upload, automatic upload, upload progress)
- Array of small C
- el-upload上傳文件(手動上傳,自動上傳,上傳進度)
猜你喜欢

How the FortiGate firewall rejects a port by using the local in policy policy

el-upload上傳文件(手動上傳,自動上傳,上傳進度)

Educoder group purchase suspension box page production
![Blue Bridge Cup: magic cube rotation [Vocational group]](/img/ba/aeae2744f3aaa1052b5af452f990e2.jpg)
Blue Bridge Cup: magic cube rotation [Vocational group]

Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)

Interprocess communication

Five methods to clear floating and their advantages and disadvantages

Blocking queue example

Redis sentry, persistence, master-slave, hand tear LRU

基于海康EhomeDemo工具排查公网部署出现的视频播放异常问题
随机推荐
Es2018 key summary
数据链路层详解
Junior students summarize JS advanced interview questions
Idea grey screen problem
Es2019 key summary
Threejs实现模拟河流,水面水流,水管水流,海面
BeanFactory创建流程
Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview
Day 10 data saving and loading
Qt 6.3.1Conan软件包发布
SQL追加字段
El upload upload file (manual upload, automatic upload, upload progress)
Introduction to system programming
My idea configuration
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
Indefinite parameters of JS function
El upload Upload file (Manual upload, Automatic upload, upload progress)
Qt Creator 8 Beta2发布
Myrpc version 2
Transport layer protocol tcp/udp