Java模拟银行转账(操作事务)
2018-02-28 01:45
615 查看
第一步:建立一张银行账户表 叫 BankAccount 并建立一个序列id number pk //用户id
ano varchar2(30) uk //用户卡号
apassword varchar2(30) //用户密码
aname varchar2(30) //用户名
amoney number //余额
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAno() {
return ano;
}
public void setAno(String ano) {
this.ano = ano;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public String getApassword() {
r
4000
eturn apassword;
}
public void setApassword(String apassword) {
this.apassword = apassword;
}
public String getAmoney() {
return amoney;
}
public void setAmoney(String amoney) {
this.amoney = amoney;
}
@Override
public String toString() {
return "BankAccount [id=" + id + ", ano=" + ano + ", aname=" + aname + ", apassword=" + apassword + ", amoney="
+ amoney + "]";
}
public BankAccount(String ano, String aname, String apassword, String amoney) {
super();
this.ano = ano;
this.aname = aname;
this.apassword = apassword;
this.amoney = amoney;
}
public BankAccount(int id, String ano, String aname, String apassword, String amoney) {
super();
this.id = id;
this.ano = ano;
this.aname = aname;
this.apassword = apassword;
this.amoney = amoney;
}
public BankAccount() {
super();
// TODO Auto-generated constructor stub
}
private int id;
private String ano;
private String aname;
private String apassword;
private String amoney;
}第三步:创建账号,编写开户程序import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import com.xdl.util.JdbcUtil2;
public class CreateBankAccount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入开户的卡号:");
String ano = sc.nextLine();
System.out.println("请输入开户的名字:");
String aname = sc.nextLine();
System.out.println("请输入开户的密码:");
String apassword = sc.nextLine();
System.out.println("请输入开户的钱数:");
String smoney = sc.nextLine();
//double amoney = Double.parseDouble(smoney);
// 把输入的数据 包装成对象
BankAccount ba = new BankAccount(ano, aname, apassword, smoney);
Connection conn = null;
PreparedStatement ps = null;
conn = JdbcUtil2.getConnection();
String sql="insert into bank_account values(bank_account_id_seq.nextval,?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, ba.getAno());
ps.setString(2, ba.getApassword());
ps.setString(3, ba.getAname());
ps.setDouble(4, Double.parseDouble(ba.getAmoney()));
int rows = ps.executeUpdate();
if(rows == 1) {
System.out.println("开户成功");
}else{
System.out.println("开户失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JdbcUtil2.releaseResource(conn, ps, null);
}
}
}第四步:编写转账程序
1、先登录,登录成功才可以执行转账程序
2、转账逻辑时,链接会自动进行数据的提交,这样万一转入的账户不成功,则无法回退之前的操作,为了解决 操作语句 需要同时成功或同事失败 则需要关闭连接的自动提交
禁止自动提交 conn.setAutoCommit(false);
语句都成功 就提交 conn.commit() ;否则进行回滚 conn.rollback();
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import com.xdl.util.JdbcUtil2;
public class BankAccountTransfer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入登录的卡号:");
String ano = sc.nextLine();
System.out.println("请输入登录的密码:");
String apassword = sc.nextLine();
Connection conn = null;
PreparedStatement ps = null;
/* 定义两个转账的 PreparedStatement */
PreparedStatement from = null;
PreparedStatement to = null;
ResultSet rs = null;
BankAccount ba = null;
conn = JdbcUtil2.getConnection();
String sql="select * from bank_account where ano=? and apassword=?";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, ano);
ps.setString(2, apassword);
rs = ps.executeQuery();
if(rs.next()){
ba = new BankAccount(rs.getInt("id"),
rs.getString("ano"), rs.getString("aname"), rs.getString("apassword"),
rs.getString("amoney"));
}
if(ba!=null){
System.out.println("登录成功");
// 从当前账户中扣除输入的金额
System.out.println("请输入转账的金额:");
String smoney=sc.nextLine();
double money = Double.parseDouble(smoney);
sql="update bank_account set amoney=amoney-? where ano=?";
// 禁止自动提交
conn.setAutoCommit(false);
from = conn.prepareStatement(sql);
from.setDouble(1, money);
from.setString(2, ba.getAno());
int fromf=from.executeUpdate();
// 提示用户输入转入的账号 和 账户名
System.out.println("请输入收款的账号:");
String toano = sc.nextLine();
System.out.println("请输入收款人姓名:");
String toaname= sc.nextLine();
sql="update bank_account set amoney=amoney+? where ano=? and aname=?";
to=conn.prepareStatement(sql);
to.setDouble(1, money);
to.setString(2, toano);
to.setString(3, toaname);
int tof = to.executeUpdate();
if(fromf==1 && tof==1){
System.out.println("转账成功");
conn.commit(); //提交
}else{
System.out.println("转账失败");
conn.rollback(); //回滚
}
}else{
System.out.println("登录失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
JdbcUtil2.releaseResource(conn, ps, rs);
JdbcUtil2.releaseResource(conn, from, null);
JdbcUtil2.releaseResource(conn, to, null);
}
}
}
ano varchar2(30) uk //用户卡号
apassword varchar2(30) //用户密码
aname varchar2(30) //用户名
amoney number //余额
//创建银行用户表 drop table bank_account; create table bank_account( id number constraint bank_account_id_pk primary key, ano varchar(30) constraint bank_account_ano_uk unique, apassword varchar(30), aname varchar(30), amoney number ); drop sequence bank_account_id_seq; create sequence bank_account_id_seq; 6.2 写一个java程序 用来开户 只要输入 账号 密码 开户人的姓名 余额 开户信息如下 6225880111887788 123456 zhangsan 99999999 6225880111887799 123456 lisi 9第二步:创建Bean包,编写实体类public class BankAccount {
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAno() {
return ano;
}
public void setAno(String ano) {
this.ano = ano;
}
public String getAname() {
return aname;
}
public void setAname(String aname) {
this.aname = aname;
}
public String getApassword() {
r
4000
eturn apassword;
}
public void setApassword(String apassword) {
this.apassword = apassword;
}
public String getAmoney() {
return amoney;
}
public void setAmoney(String amoney) {
this.amoney = amoney;
}
@Override
public String toString() {
return "BankAccount [id=" + id + ", ano=" + ano + ", aname=" + aname + ", apassword=" + apassword + ", amoney="
+ amoney + "]";
}
public BankAccount(String ano, String aname, String apassword, String amoney) {
super();
this.ano = ano;
this.aname = aname;
this.apassword = apassword;
this.amoney = amoney;
}
public BankAccount(int id, String ano, String aname, String apassword, String amoney) {
super();
this.id = id;
this.ano = ano;
this.aname = aname;
this.apassword = apassword;
this.amoney = amoney;
}
public BankAccount() {
super();
// TODO Auto-generated constructor stub
}
private int id;
private String ano;
private String aname;
private String apassword;
private String amoney;
}第三步:创建账号,编写开户程序import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
import com.xdl.util.JdbcUtil2;
public class CreateBankAccount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入开户的卡号:");
String ano = sc.nextLine();
System.out.println("请输入开户的名字:");
String aname = sc.nextLine();
System.out.println("请输入开户的密码:");
String apassword = sc.nextLine();
System.out.println("请输入开户的钱数:");
String smoney = sc.nextLine();
//double amoney = Double.parseDouble(smoney);
// 把输入的数据 包装成对象
BankAccount ba = new BankAccount(ano, aname, apassword, smoney);
Connection conn = null;
PreparedStatement ps = null;
conn = JdbcUtil2.getConnection();
String sql="insert into bank_account values(bank_account_id_seq.nextval,?,?,?,?)";
try {
ps = conn.prepareStatement(sql);
ps.setString(1, ba.getAno());
ps.setString(2, ba.getApassword());
ps.setString(3, ba.getAname());
ps.setDouble(4, Double.parseDouble(ba.getAmoney()));
int rows = ps.executeUpdate();
if(rows == 1) {
System.out.println("开户成功");
}else{
System.out.println("开户失败");
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
JdbcUtil2.releaseResource(conn, ps, null);
}
}
}第四步:编写转账程序
1、先登录,登录成功才可以执行转账程序
2、转账逻辑时,链接会自动进行数据的提交,这样万一转入的账户不成功,则无法回退之前的操作,为了解决 操作语句 需要同时成功或同事失败 则需要关闭连接的自动提交
禁止自动提交 conn.setAutoCommit(false);
语句都成功 就提交 conn.commit() ;否则进行回滚 conn.rollback();
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import com.xdl.util.JdbcUtil2;
public class BankAccountTransfer {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入登录的卡号:");
String ano = sc.nextLine();
System.out.println("请输入登录的密码:");
String apassword = sc.nextLine();
Connection conn = null;
PreparedStatement ps = null;
/* 定义两个转账的 PreparedStatement */
PreparedStatement from = null;
PreparedStatement to = null;
ResultSet rs = null;
BankAccount ba = null;
conn = JdbcUtil2.getConnection();
String sql="select * from bank_account where ano=? and apassword=?";
try {
ps=conn.prepareStatement(sql);
ps.setString(1, ano);
ps.setString(2, apassword);
rs = ps.executeQuery();
if(rs.next()){
ba = new BankAccount(rs.getInt("id"),
rs.getString("ano"), rs.getString("aname"), rs.getString("apassword"),
rs.getString("amoney"));
}
if(ba!=null){
System.out.println("登录成功");
// 从当前账户中扣除输入的金额
System.out.println("请输入转账的金额:");
String smoney=sc.nextLine();
double money = Double.parseDouble(smoney);
sql="update bank_account set amoney=amoney-? where ano=?";
// 禁止自动提交
conn.setAutoCommit(false);
from = conn.prepareStatement(sql);
from.setDouble(1, money);
from.setString(2, ba.getAno());
int fromf=from.executeUpdate();
// 提示用户输入转入的账号 和 账户名
System.out.println("请输入收款的账号:");
String toano = sc.nextLine();
System.out.println("请输入收款人姓名:");
String toaname= sc.nextLine();
sql="update bank_account set amoney=amoney+? where ano=? and aname=?";
to=conn.prepareStatement(sql);
to.setDouble(1, money);
to.setString(2, toano);
to.setString(3, toaname);
int tof = to.executeUpdate();
if(fromf==1 && tof==1){
System.out.println("转账成功");
conn.commit(); //提交
}else{
System.out.println("转账失败");
conn.rollback(); //回滚
}
}else{
System.out.println("登录失败");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
try {
conn.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally{
JdbcUtil2.releaseResource(conn, ps, rs);
JdbcUtil2.releaseResource(conn, from, null);
JdbcUtil2.releaseResource(conn, to, null);
}
}
}
相关文章推荐
- MySQL数据库事务实例(模拟银行转账)
- Spring通过事务实现模拟银行转账案例
- python操作MySQL 模拟简单银行转账操作
- Python 操作MySQL实例之银行转账模拟
- Spring 事务操作(银行转账案例),使用spring 对jdbc的支持完成对数据库的操作
- Python操作MySQL模拟银行转账
- JAVA模拟银行存钱,取钱操作以及计算利息操作
- python操作MySQL 模拟简单银行转账操作
- python操作MySQL 模拟简单银行转账操作
- Java基于JDBC实现事务,银行转账及货物进出库功能示例
- 使用事务实现银行转账操作
- 一个MVC架构的线程安全的银行转账案例(事务控制)
- python 简单模拟银行转账功能
- 金融IT——模拟银行ATM系统Java实现
- 浏览器与服务器交互原理以及用java模拟浏览器操作v
- 模拟银行系统(单用户操作)
- 模拟自助取款机操作--java小程序
- JDBC学习笔记(五):模拟银行转账
- Spring中使用事务搭建转账环境 转账操作一个账户要减少资金操作一个账户要增加资金操作,如果在两个操作间出现异常转账失败 所以要使用事务
- Android——简单模拟银行转账