事务实现转账功能

文件已上传,目前我也不知道这类东西观看或者下载需要什么东西,如有需要可联系我,一个正在努力学习的身体健康的程序渊。
dao

ackage cn.hahaha.bank.dao; import cn.hahaha.bank.javabean.User; import cn.hahaha.bank.utils.C3P0_utils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import java.sql.Connection; import java.sql.SQLException; public class BankDao { public boolean judgeDao(User user){ Connection con = C3P0_utils.getConnection(); QueryRunner qr = new QueryRunner(); User query = null; try { query = qr.query(con, "select * from bank where uname = ? and password = ?", new BeanHandler(User.class), user.getName(), user.getPassword()); return true; } catch (Exception e) { e.printStackTrace(); } return false; } public boolean judgeDao(String name){ Connection con = C3P0_utils.getConnection(); QueryRunner qr = new QueryRunner(); User query = null; try { query = qr.query(con, "select * from bank where uname = ?", new BeanHandler(User.class), name); if (query==null){ return false; }else { return true; } } catch (Exception e) { e.printStackTrace(); } return false; }//public void transfer(User u, String name1, Double money) throws SQLException { // // //} public void outMoney(User user,Double money){ Connection con = C3P0_utils.getConnection(); QueryRunner qr = new QueryRunner(); try { qr.update(con, "update bank set money = money - ? where uname = ?", money, user.getName()); } catch (SQLException e) { e.printStackTrace(); }} public void inMoney(String name,Double money){QueryRunner qr = new QueryRunner(); try { Connection con = C3P0_utils.getConnection(); qr.update(con, "update bank set money = money + ? where uname = ?", money, name); } catch (SQLException e) { e.printStackTrace(); } } }

domain
package cn.hahaha.bank.domain; import cn.hahaha.bank.dao.BankDao; import cn.hahaha.bank.javabean.User; import cn.hahaha.bank.service.AccountService; import org.apache.commons.dbutils.DbUtils; import java.util.Scanner; public class Bank { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Scanner sc1 = new Scanner(System.in); System.out.println("请输入用户名:"); String name = sc.nextLine(); System.out.println("请输入密码:"); int password = sc1.nextInt(); User user = new User(name,password,0); BankDao bankDao = new BankDao(); boolean b = bankDao.judgeDao(user); if (b){ account(user); }} public static void account(User user){ System.out.println("-----请选择接下来所要进行的操作:-----"); System.out.println("-----1.转账"); System.out.println("-----其他业务待定。。。"); Scanner sc = new Scanner(System.in); Scanner sc1 = new Scanner(System.in); int chose = sc.nextInt(); if (chose == 1){ System.out.println("请输入要转的人姓名:"); String name1 = sc1.nextLine(); BankDao bankDao = new BankDao(); boolean b = bankDao.judgeDao(name1); if (b){ System.out.println("请输入转的金额:"); Double money = sc.nextDouble(); AccountService as = new AccountService(); as.transfer(user,name1,money); System.out.println("操作已完成!"); }else{ System.out.println("不存在此用户!"); } } } }

javabean
package cn.hahaha.bank.javabean; public class User { private String name; private int password; private double money; public User() { }public User(String name, int password, double money) { this.name = name; this.password = password; this.money = money; }@Override public String toString() { return "User{" + "name='" + name + '\'' + ", password=" + password + ", money=" + money + '}'; }public String getName() { return name; }public void setName(String name) { this.name = name; }public int getPassword() { return password; }public void setPassword(int password) { this.password = password; }public double getMoney() { return money; }public void setMoney(double money) { this.money = money; } }

【事务实现转账功能】service
package cn.hahaha.bank.service; import cn.hahaha.bank.dao.BankDao; import cn.hahaha.bank.javabean.User; import cn.hahaha.bank.utils.C3P0_utils; import org.apache.commons.dbutils.DbUtils; import java.sql.Connection; import java.sql.SQLException; public class AccountService { public void transfer(User u, String name1, Double money){ Connection connection = C3P0_utils.getConnection(); try { connection.setAutoCommit(false); BankDao bankDao = new BankDao(); bankDao.outMoney(u,money); //int i=1/0; bankDao.inMoney(name1,money); DbUtils.commitAndCloseQuietly(connection); System.out.println("操作完成!"); } catch (SQLException e) { DbUtils.rollbackAndCloseQuietly(connection); e.printStackTrace(); System.out.println("操作失败!"); }} }

utils
package cn.hahaha.bank.utils; import com.mchange.v2.c3p0.ComboPooledDataSource; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; public class C3P0_utils { public static DataSource ds = new ComboPooledDataSource(); static ThreadLocal t1 = new ThreadLocal(); public static Connection getConnection() { try { Connection con = t1.get(); if (con == null) { con = ds.getConnection(); t1.set(con); con = t1.get(); } return con; } catch (Exception e) { throw new RuntimeException(e); } } }

以上代码比较完整,需要导入jar包,配置xml文件,如有问题,可找我,我可发完整代码jar包和配置文件,在此留下微信:jl372444090,如有疑问可联系,但请注明缘由,负责不予理会。

    推荐阅读