1.连接数据库,建好表
2.先建个项目,我的名字是com.nz,可随意
3.在com.nz下建个DAO包
package com.nz.dao;
import com.nz.entity.BankAccount1;
import com.nz.util.MySQLDriverUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 86150 on 2022/10/17.
*/
public class BankDao {
//查
public List<BankAccount1> BankSelect() throws SQLException {
//获取连接
Connection connect = MySQLDriverUtil.getConnect();
//准备SQL
String sql = "select * from bank_account1";
//预装载
PreparedStatement ps = connect.prepareStatement(sql);
//执行
ResultSet result = ps.executeQuery();
//初始化
List<BankAccount1> list = new ArrayList<BankAccount1>();
//收参
while(result.next()){
int id = result.getInt(1);
String baNum = result.getString("ba_num");
double baBalance = result.getDouble("ba_balance");
String baCreateDate = result.getString("ba_create_date");
BankAccount1 bankAccount1 = new BankAccount1(id,baNum,baBalance,baCreateDate);
list.add(bankAccount1);
}
//返回值
return list;
}
//转账流程(A扣款,B增款)
public boolean transfer(String fromAccount,String toAccount,double amount) throws SQLException {
//获取连接
Connection connect = MySQLDriverUtil.getConnect();
//扣款
String sql1 = "update bank_account set ba_balance=ba_balance-"+amount+" where ba_num='"+fromAccount+"'";
PreparedStatement ps1 = connect.prepareStatement(sql1);
//增款
String sql2 = "update bank_account set ba_balance=ba_balance+"+amount+" where ba_num='"+toAccount+"'";
PreparedStatement ps2 = connect.prepareStatement(sql2);
//执行
int i1 = ps1.executeUpdate();
int i2 = ps2.executeUpdate();
return true;
}
//判断转出人余额是否足够
public boolean checkAmount(String fromAccount,double amount) throws SQLException{
//获取连接
Connection connect = MySQLDriverUtil.getConnect();
//准备SQL
String sql="SELECT * from bank_account WHERE ba_num='"+fromAccount+"'";
//预装载
PreparedStatement ps=connect.prepareStatement(sql);
//执行
ResultSet result = ps.executeQuery();
//收参
while(result.next()){
double aDouble = result.getDouble("ba_balance");
//条件判断
if(amount>aDouble){
return false;
}
}
return true;
}
//判断收款人编号是否存在
public boolean checkAccount(String toAccount)throws SQLException{
//获取链接
Connection connection =MySQLDriverUtil.getConnect();
//准备SQL
String sql = "SELECT * from bank_account WHERE ba_num='"+toAccount+"'";
//预装载
PreparedStatement ps=connection.prepareStatement(sql);
//执行
ResultSet resultSet =ps.executeQuery();
//收参
while(resultSet.next()){
return true;
}
return false;
}
}
4.在com.nz下建个entity包
package com.nz.entity;
/**
* Created by 86150 on 2022/10/17.
*/
public class BankAccount1 {
private int baId;
private String baNum;
private double baBalance;
private String baCreateDate;
public int getBaId() {
return baId;
}
public void setBaId(int baId) {
this.baId = baId;
}
public String getBaNum() {
return baNum;
}
public void setBaNum(String baNum) {
this.baNum = baNum;
}
public double getBaBalance() {
return baBalance;
}
public void setBaBalance(double baBalance) {
this.baBalance = baBalance;
}
public String getBaCreateDate() {
return baCreateDate;
}
public void setBaCreateDate(String baCreateDate) {
this.baCreateDate = baCreateDate;
}
@Override
public String toString() {
return "BankAccount1{" +
"baId=" + baId +
", baNum='" + baNum + '\'' +
", baBalance=" + baBalance +
", baCreateDate='" + baCreateDate + '\'' +
'}';
}
public BankAccount1(int baId, String baNum, double baBalance, String baCreateDate) {
this.baId = baId;
this.baNum = baNum;
this.baBalance = baBalance;
this.baCreateDate = baCreateDate;
}
public BankAccount1() {
}
}
5.在com.nz下建个service包
package com.nz.service;
import com.nz.entity.BankAccount1;
import java.util.List;
/**
* Created by 86150 on 2022/10/17.
*/
public interface BankService {
//查
public List<BankAccount1> BankQuery();
//转账流程(A扣款,B增款)
public boolean transfer(String fromAccount,String toAccount,double amount) throws Exception;
}
6.在com.nz下建个impl(impl包放在service下)
package com.nz.service.impl;
import com.nz.dao.BankDao;
import com.nz.entity.BankAccount1;
import com.nz.service.BankService;
import com.nz.util.MySQLDriverUtil;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 86150 on 2022/10/17.
*/
public class BankServiceImpl implements BankService {
BankDao bankDao = new BankDao();
//查
public List<BankAccount1> BankQuery() {
//初始化
List<BankAccount1> list = new ArrayList<>();
try {
list = bankDao.BankSelect();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//获取连接
Connection connect = MySQLDriverUtil.getConnect();
MySQLDriverUtil.flowClose(connect);
}
return list;
}
//转账流程(A扣款,B增款)
public boolean transfer(String fromAccount, String toAccount, double amount){
//获取连接
Connection connect = MySQLDriverUtil.getConnect();
try {
//关闭自动提交
connect.setAutoCommit(false);
//条件判断
boolean bl1 = bankDao.checkAmount(fromAccount, amount);
boolean bl2 = bankDao.checkAccount(toAccount);
if (bl1 == false) {
throw new Exception("遇到错误停止,你太穷了!");
}
if (bl2 == false) {
throw new Exception("遇到错误停止,对方账号不存在!");
// return false;
}
boolean bl = bankDao.transfer(fromAccount, toAccount, amount);
//手动提交
connect.commit();
return true;
} catch (Exception e) {
//回滚
try {
connect.rollback();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}finally {
MySQLDriverUtil.flowClose(connect);
}
return false;
}
}
7.在com.nz下建个test包
package com.nz.test;
import com.nz.service.BankService;
import com.nz.service.impl.BankServiceImpl;
/**
* Created by 86150 on 2022/10/17.
*/
public class TestMain {
public static void main(String[] args) throws Exception {
BankService bankService = new BankServiceImpl();
//查
// List<BankAccount> list = bankService.BankQuery();
boolean bl = bankService.transfer("1352614", "150830", 5000);
System.out.println(bl);
}
}
8.在com.nz下建个util包
package com.nz.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class MySQLDriverUtil {
//外层定义全局类变量,以便使用
public static final Properties pro = new Properties();
//静态代码块,只执行一次类加载
static{
//将配置文件转为输入流
InputStream is = MySQLDriverUtil.class.getResourceAsStream("/jdbc.properties");
try {
//加载配置文件
pro.load(is);
is.close();
//类加载驱动
Class.forName(pro.getProperty("driver"));
} catch (Exception e) {
e.printStackTrace();
}
}
//1.在全局创建一个静态的ThreadLocal的对象 全局线程栈
private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
//对本机建立jdbc链接
public static Connection getConnect(){
//2.先从ThreadLocal中获取当前线程上绑定的Connection对象
Connection conn = tl.get();
try {
//3.判断数据库连接对象是否为null
if(conn == null){ //4.如果为null 创建一个新的数据库连接
conn = DriverManager.getConnection(pro.getProperty("url"), pro.getProperty("username"),
pro.getProperty("password"));
//5.将新的数据库连接存储到ThreadLocal对象中
tl.set(conn);
}
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
//关闭连接流,并清空全局线程栈
public static void flowClose(Connection con){
try {
con.close();
tl.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭update流
public static void flowClose(Connection con, PreparedStatement ps){
try {
con.close();
ps.close();
tl.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭select流,并销毁全局线程栈
public static void flowClose(Connection con, PreparedStatement ps,ResultSet rs){
try {
con.close();
ps.close();
rs.close();
tl.remove();
} catch (SQLException e) {
e.printStackTrace();
}
}
}