package bankproject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Transfer_Dom {
Connection conn = null;
PreparedStatement pstmt = null;
String sql = null;
int i = 0;
ResultSet rs = null;
int Money = 0;
int bankincome = 0;
public void Transfer(String name1, String name2, double money)
throws SQLException {
conn = Utils.getConnection();
conn.setAutoCommit(false);
// 开始转账
sql = "update user set usermoney=usermoney-? where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, money);
pstmt.setObject(2, name1);
i = pstmt.executeUpdate();
if (i > 0) {
// 用户1余额
sql = "select usermoney from user where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name1);
rs = pstmt.executeQuery();
if (rs.next()) {
Money = rs.getInt("usermoney");
if (Money > 0) {
sql = "update user set usermoney=usermoney+? where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, money);
pstmt.setObject(2, name2);
i = pstmt.executeUpdate();
sql = "select bankId from user where username=?";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name2);
rs = pstmt.executeQuery();
if (rs.next()) {
String id1 = rs.getString("bankID");
System.out.println(id1);
// 收款人存的银行
sql = "select bankId from user where username = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name2);
rs = pstmt.executeQuery();
if (rs.next()) {
String id2 = rs.getString("bankId");
System.out.println(id2);
// 判断是否同行同地
if (id1 == id2) {
// bankincome = 0;
sql = "select bankareaId from bank where bankId = (select bankId from user where username = ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name1);
rs = pstmt.executeQuery();
if (rs.next()) {
int areaid3 = rs.getInt("bankareaId");
// System.out.println(areaid1);
sql = "select bankareaId from bank where bankId = (select bankId from user where username = ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name2);
rs = pstmt.executeQuery();
if (rs.next()) {
int areaid4 = rs.getInt("bankareaId");
// System.out.println(areaid1);
// if (areaid3 == areaid4 && money <
// 2500) {
// money = money * 0.01;
//
// }
if (areaid3 == areaid4) {
bankincome = 0;
}
if (areaid3 != areaid4 && money < 2500) {
money = money * 0.01;
}
if (areaid3 != areaid4 && money > 2500
&& money < 5000) {
money = 25;
}
// if (areaid3 != areaid4&& money >
// 5000) {
// money = 75;
//
// }
}
// 不同地再判断
if (id1 != id2) {
sql = "select bankareaId from bank where bankId = (select bankId from user where username = ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name1);
rs = pstmt.executeQuery();
if (rs.next()) {
int areaid1 = rs
.getInt("bankareaId");
System.out.println(areaid1);
sql = "select bankareaId from bank where bankId = (select bankId from user where username = ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name2);
rs = pstmt.executeQuery();
if (rs.next()) {
int areaid2 = rs
.getInt("bankareaId");
System.out.println(areaid1);
if (areaid1 == areaid2
&& money < 2500) {
money = money * 0.01;
}
if (areaid1 == areaid2
&& money >= 2500) {
money = 50;
}
if (areaid1 != areaid2
&& money < 2500) {
money = money * 0.01
+ money * 0.01;
}
if (areaid1 != areaid2
&& money > 2500
&& money < 5000) {
money = 25 + money * 0.01;
}
if (areaid1 != areaid2
&& money > 5000) {
money = 75;
}
sql = "update user set usermoney=usermoney - ? where username = ?";
pstmt = conn
.prepareStatement(sql);
pstmt.setObject(1, money);
pstmt.setObject(2, name1);
i = pstmt.executeUpdate();
sql = "select userId from user where username = ?";
pstmt = conn
.prepareStatement(sql);
pstmt.setObject(1, name1);
rs = pstmt.executeQuery();
if (rs.next()) {
int useroneID = rs
.getInt("userId");
sql = "insert into bankincome (bankincomeId,bimoney,userId,bankId) values (?,?,?,?)";
pstmt = conn
.prepareStatement(sql);
pstmt.setObject(1, null);
pstmt.setObject(2, money);
pstmt.setObject(3,
useroneID);
pstmt.setObject(4, id1);
i = pstmt.executeUpdate();
}
}
}
}
}
}
}
sql = "update user set usermoney=usermoney - ? where username = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, money);
pstmt.setObject(2, name1);
i = pstmt.executeUpdate();
sql = "select userId from user where username = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, name1);
rs = pstmt.executeQuery();
if (rs.next()) {
int useroneID = rs.getInt("userId");
sql = "insert into bankincome (bankincomeId,bimoney,userId,bankId) values (?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setObject(1, null);
pstmt.setObject(2, money);
pstmt.setObject(3, useroneID);
pstmt.setObject(4, id1);
i = pstmt.executeUpdate();
}
if (i > 0) {
System.out.println("转账成功!!");
conn.commit();
}
}
}
} else {
System.out.println("转账失败!");
conn.rollback();
}
} else {
System.out.println("余额不足!");
conn.rollback();
}
}
public static void main(String[] args) {
try {
Transfer_Dom transfer = new Transfer_Dom();
transfer.Transfer("小明", "张三", 30);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
B.V.B-90
- 粉丝: 55
- 资源: 26
最新资源
- 电子学习资料设计作品全资料远程温度控制系统资料
- 电子学习资料设计作品全资料智能风扇调速系统资料
- 一体式抽油烟机模型sw18可编辑全套技术资料100%好用.zip.zip
- 基于深度学习的交通标志识别系统(django)源代码(python毕业设计完整源码+LW).zip
- 电子学习资料设计作品全资料智能台灯设计资料
- 电子学习资料设计作品全资料智能温度报警系统设计资料
- 电子学习资料设计作品全资料自动加料机控制系统资料
- 新代-山龙-众泰克-维宏通用 - 副本.arp
- 盐厂装箱机Pro5.0全套技术资料100%好用.zip.zip
- 基于深度学习的身份证识别考勤系统(django)源代码(python毕业设计完整源码+LW).zip
- 电子学习资料设计作品文档1.5V调频无线话筒电路制作
- 一体化滤池sw16可编辑全套技术资料100%好用.zip.zip
- 电子学习资料设计作品文档电容降压电源原理和计算公式
- 油管切断机sw18可编辑全套技术资料100%好用.zip.zip
- 电子学习资料设计作品文档电子琴
- 电子学习资料设计作品文档频率计
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈