银行ATM机模拟系统(Java+jdbc+mysql数据库)

您所在的位置:网站首页 银行atm机系统对象图表怎么看 银行ATM机模拟系统(Java+jdbc+mysql数据库)

银行ATM机模拟系统(Java+jdbc+mysql数据库)

2024-03-12 17:16| 来源: 网络整理| 查看: 265

概述:

最近做了一个简易的银行ATM机模拟系统,能够利用jdbc操作数据库,模拟了一些简单的功能,如下面所示:

1.查询余额 2.存钱 3.取钱 4.转账 5.查询交易记录 6.注销账户 7.修改密码 8.退出系统

有需要jdbc驱动jar包和mysql数据库文件的伙伴可以留言或私信我。

详细代码: import java.sql.*; import java.text.SimpleDateFormat; import java.util.Scanner; import java.util.Date; public class bankAtm{ static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/bank_manage?serverTimezone=UTC&useSSL=false"; static final String USER = "root"; static final String PASS = "1234"; //数据库密码 public static void show(String name) throws ClassNotFoundException, SQLException { Connection conn = null; PreparedStatement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); String sql1 = "SELECT * FROM account_tab where name = ?"; stmt = conn.prepareStatement(sql1); stmt.setString(1,name); ResultSet rs = stmt.executeQuery(); double amount = 0; while(rs.next()) { amount = rs.getDouble("account"); } System.out.println("账户余额为:"+amount); rs.close(); stmt.close(); conn.close(); } public static void deposit(String name) throws ClassNotFoundException, SQLException { Connection conn = null; PreparedStatement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); String sql1 = "SELECT * FROM account_tab where name = ?"; stmt = conn.prepareStatement(sql1); stmt.setString(1,name); ResultSet rs = stmt.executeQuery(); double oldAmount = 0; while(rs.next()) { oldAmount = rs.getDouble("account"); } System.out.println("请输入存入的金额:"); Scanner sc = new Scanner(System.in); double amount = sc.nextDouble(); double newAmount = amount + oldAmount; String sql2 = "update account_tab set account =? where name =?"; stmt = conn.prepareStatement(sql2); stmt.setDouble(1,newAmount); stmt.setString(2,name); int res = stmt.executeUpdate(); if(res!=0){ System.out.println("操作成功,余额为:"+newAmount); record(name,"存入",amount); } else{ System.out.println("操作失败!"); } rs.close(); stmt.close(); conn.close(); } public static void withdraw(String name) throws ClassNotFoundException, SQLException { Connection conn = null; PreparedStatement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); String sql1 = "SELECT * FROM account_tab where name = ?"; stmt = conn.prepareStatement(sql1); stmt.setString(1,name); ResultSet rs = stmt.executeQuery(); double oldAmount = 0; while(rs.next()) { oldAmount = rs.getDouble("account"); } System.out.println("请输入取出的金额:"); Scanner sc = new Scanner(System.in); double amount = sc.nextDouble(); double newAmount = oldAmount - amount; if(newAmount String sql2 = "update account_tab set account =? where name =?"; stmt = conn.prepareStatement(sql2); stmt.setDouble(1, newAmount); stmt.setString(2, name); int res = stmt.executeUpdate(); if (res != 0) { System.out.println("操作成功,余额为:" + newAmount); record(name,"取出",amount); } else { System.out.println("操作失败!"); } } rs.close(); stmt.close(); conn.close(); } public static void transfer(String name) throws ClassNotFoundException, SQLException { Connection conn = null; PreparedStatement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); String sql1 = "SELECT * FROM account_tab where name = ?"; stmt = conn.prepareStatement(sql1); stmt.setString(1,name); ResultSet rs = stmt.executeQuery(); double oldAmount = 0; while(rs.next()) { oldAmount = rs.getDouble("account"); } System.out.println("请输入要转入的账户:"); Scanner sc = new Scanner(System.in); String sql2 = "SELECT * FROM account_tab where name = ?"; stmt = conn.prepareStatement(sql2); String targetAccount = sc.next(); stmt.setString(1,targetAccount); ResultSet rs1 = stmt.executeQuery(); double targetAmount = 0; if(rs1.next()){ targetAmount = rs1.getDouble("account"); System.out.println("请输入转出金额:"); double amount = sc.nextDouble(); double newAmount = oldAmount - amount; if(newAmount targetAmount = targetAmount + amount; String sql3 = "update account_tab set account =? where name =?"; stmt = conn.prepareStatement(sql3); stmt.setDouble(1, newAmount); stmt.setString(2, name); int res1 = stmt.executeUpdate(); String sql4 = "update account_tab set account =? where name =?"; stmt = conn.prepareStatement(sql4); stmt.setDouble(1, targetAmount); stmt.setString(2, targetAccount); int res2 = stmt.executeUpdate(); if ((res1!=0)&&(res2!=0)) { System.out.println("操作成功,余额为:" + newAmount); record(name,"转出",amount); record(targetAccount,"转入",amount); } else { System.out.println("操作失败!"); } } } else{ System.out.println("不存在该账户!"); } rs.close(); stmt.close(); conn.close(); } public static boolean accountDisposal(String name) throws ClassNotFoundException, SQLException { System.out.println("您正在执行注销户操作,请确认是否继续:"); System.out.println("1.继续"); System.out.println("2.返回"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if(choice==1) { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 String sql = "delete from account_tab where name=?"; stmt = conn.prepareStatement(sql); stmt.setString(1, name); int result = stmt.executeUpdate(); if(result!=0){ System.out.println("注销账户成功!"); System.out.println("已自动退出系统!!"); return true; } else{ System.out.println("操作失败!"); } } else{ System.out.println("操作取消!"); return false; } return false; } public static void changePassword(String name) throws ClassNotFoundException, SQLException { System.out.println("请输入旧密码:"); Scanner sc = new Scanner(System.in); String oldPassword = sc.next(); String newPassword =null; String sql = "select * from account_tab where name=? and password = ?; "; Connection conn = null; PreparedStatement stmt = null; // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 stmt = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, oldPassword); ResultSet rs = ps.executeQuery(); if (rs.next()) { System.out.println("请输入新密码:"); newPassword = sc.next(); if(newPassword.length()!=6){ System.out.println("新密码不符合要求!!!"); System.out.println("必须为六位数字!!!"); } else{ String sql1 = "update account_tab set password =? where name =?"; stmt = conn.prepareStatement(sql1); stmt.setString(1, newPassword); stmt.setString(2, name); int res2 = stmt.executeUpdate(); if(res2!=0){ System.out.println("密码修改成功!!!"); } else{ System.out.println("操作失败!!!"); } } } else{ System.out.println("密码错误!!!"); System.out.println("操作已终止!!!"); } } public static void showRecord(String name) throws ClassNotFoundException, SQLException { Connection conn = null; PreparedStatement stmt = null; Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); String sql = "SELECT * FROM record_tab where name =?"; stmt = conn.prepareStatement(sql); stmt.setString(1,name); ResultSet rs = stmt.executeQuery(); while (rs.next()) { // 通过字段检索 String type = rs.getString("type"); double amount = rs.getDouble("amount"); Timestamp time = rs.getTimestamp("time"); // 输出数据 System.out.print("交易类型: "+type+"\t"); System.out.print("交易金额: "+amount+ "\t"); System.out.print("交易时间: "+time+"\t"); System.out.print("\n"); } // 完成后关闭 rs.close(); stmt.close(); conn.close(); } public static void record(String name,String type,double amount) throws ClassNotFoundException, SQLException { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 conn = DriverManager.getConnection(DB_URL, USER, PASS); Date date = new Date();//获得当前时间 Timestamp t = new Timestamp(date.getTime());//将时间转换成Timestamp类型,这样便可以存入到Mysql数据库中 String sql = "insert into record_tab(name,type,amount,time) values(?,?,?,?)"; stmt = conn.prepareStatement(sql); stmt.setString(1,name); stmt.setString(2,type); stmt.setDouble(3,amount); stmt.setTimestamp(4,t); stmt.executeUpdate(); } public static boolean login(String name) throws ClassNotFoundException, SQLException { System.out.println("请输入密码:"); Scanner sc = new Scanner(System.in); String password = sc.next(); String sql = "select * from account_tab where name=? and password = ?; "; Connection conn = null; PreparedStatement stmt = null; // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 conn = DriverManager.getConnection(DB_URL, USER, PASS); // 执行查询 stmt = conn.prepareStatement(sql); PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, password); ResultSet rs = ps.executeQuery(); if (rs.next()) { System.out.println("成功进入系统!"); System.out.print("进入时间:"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间 rs.close(); stmt.close(); conn.close(); return true; } else { System.out.println("账号或密码错误!"); rs.close(); stmt.close(); conn.close(); return false; } } public static boolean doMain(String name) throws SQLException, ClassNotFoundException { System.out.println("请选择功能:"); System.out.print("1.查询余额"); System.out.print("\t2.存钱"); System.out.print("\t3.取钱"); System.out.print("\t4.转账"); System.out.print("\t5.查询交易记录"); System.out.print("\t6.注销账户"); System.out.print("\t7.修改密码"); System.out.println("\t8.退出系统"); Scanner sc1 = new Scanner(System.in); int choice = sc1.nextInt(); switch (choice) { case 1: show(name); break; case 2: deposit(name); break; case 3: withdraw(name); break; case 4: transfer(name); break; case 5: showRecord(name); break; case 6: boolean flag = accountDisposal(name); if(flag == true) return false; break; case 7: changePassword(name); break; case 8: System.out.println("退出成功"); return false; } return true; } public static void main(String args[]) throws SQLException, ClassNotFoundException { boolean flag = true; boolean userFlag = false; String name=null; System.out.println("欢迎使用模拟ATM系统!!!"); System.out.println("请选择:"); System.out.println("1.进入系统"); System.out.println("2.退出"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if(choice==1){ System.out.println("请输入账户:"); Scanner sc1 = new Scanner(System.in); name = sc1.next(); userFlag=login(name); } else{ System.out.println("退出成功!"); } while(flag&&userFlag){ flag=doMain(name); } } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3