当前位置:网站首页>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);
}
}
边栏推荐
- Modifier of JS regular expression
- thinkphp5实现导入功能
- SQL追加字段
- 【WEBRTC】ADM: rtc_ include_ internal_ audio_ Device triggers RTC_ Dcheck (ADM) assertion
- Memorize unfamiliar words at SSM stage and update them from time to time
- QT creator 8 beta2 release
- Equity interest [non DP]
- iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
- Qt Creator 8 Beta2发布
- Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)
猜你喜欢

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

OneNote production schedule

System programming summary

Myrpc version 0

什么是光耦电路,实际使用中应该注意些什么?

Myrpc version 4

Myrpc version 1

Interview topic of MySQL

Explain the underlying principles of JVM garbage collection in simple terms

Myrpc version 6
随机推荐
Error encountered in SQL statement, solve
Unity when entering a string in the editor, escape the input of characters
Explain the underlying principles of JVM garbage collection in simple terms
Junior students summarize JS basic interview questions
JS import and export
小C的数组(array)
How to use div boxes to simulate line triangles
[从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)
Myrpc version 3
Transport layer protocol tcp/udp
IO stream, byte stream read / write copy
win10系统使用浏览器下载后,内容无故移动或删除
File system and directory operations
01 backpack, dynamic planning
JS proxy
lego_ Reading and summary of loam code
SQL追加字段
Detailed explanation of network layer
Find the interface and add parameters to the form