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();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
create table bank ( bankId int primary key auto_increment, bankname varchar(13), bankareaId int, foreign key(bankareaId)references bankarea(bankareaId) ); insert into bank values(1,'中国农业银行',1),(2,'交通银行',2); ,(3,'中国农业银行',2),(4,'交通银行',1); create table bankarea ( bankareaId int primary key auto_increment, bankareaname varchar(20) ); insert into bankarea values(1,'南宁'),(2,'北京'); create table user( userId int primary key auto_increment, username varchar(13), bankId int, usermoney double, foreign key(bankId)references bank(bankId) ); insert into user values(1,'小明',1,100),(2,'张三',2,0);
资源推荐
资源详情
资源评论
收起资源包目录
JDBC模拟银行转账.rar (9个子文件)
aaaa
bin
bankproject
Utils.class 3KB
Transfer_Dom.class 5KB
newfile.sql 1KB
.settings
org.eclipse.jdt.core.prefs 635B
src
bankproject
Transfer_Dom.java 7KB
Utils.java 1KB
newfile.sql 1KB
.project 380B
.classpath 374B
共 9 条
- 1
资源评论
B.V.B-90
- 粉丝: 52
- 资源: 26
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功