欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java+mysql模拟实现银行系统

程序员文章站 2022-03-28 20:03:40
模拟电子银行,开户,存钱,取钱,查询用户信息,修改用户信息,查询余额,查询明细,注销,退出,等等基本实现了相关功能。但是规定每次需要重新登录,还是很麻烦的。另外用户注销时候,没有规定删除operato...

模拟电子银行,开户,存钱,取钱,查询用户信息,修改用户信息,查询余额,查询明细,注销,退出,等等 

基本实现了相关功能。但是规定每次需要重新登录,还是很麻烦的。

另外用户注销时候,没有规定删除operator表中信息,此处会有问题。但想了半天觉得现实中这部分信息需要保留一段时间,不应该删除。orz

数据库设计:

java+mysql模拟实现银行系统

java+mysql模拟实现银行系统

java+mysql模拟实现银行系统

java+mysql模拟实现银行系统

java+mysql模拟实现银行系统

bank.java:

package mybank;
 
import org.apache.commons.dbutils.queryrunner;
 
//import java.sql.date;
import java.util.*;
import java.util.date;
import java.sql.*;
import java.text.decimalformat;
import java.text.parseposition;
import java.text.simpledateformat;
 
import org.apache.commons.dbutils.queryrunner;
import org.apache.commons.dbutils.handlers.arrayhandler;
import org.apache.commons.dbutils.handlers.arraylisthandler;
import org.apache.commons.dbutils.handlers.beanlisthandler;
public class bank {
 
// public static connection getconnection() throws exception
// {
//// class.forname("com.mysql.cj.jdbc.driver");
//// properties info=new properties();
//// info.setproperty("user", "root");
//// info.setproperty("password", "wenmiao11");
//// 
//// connection conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/scsxbank?useunicode=true&characterencoding=utf8&servertimezone=gmt%2b8&usessl=false", info);
//// 
// 
// 
// return conn;
// }
 
 public static int writeoperator(string type,double num,int fromid,int toid) throws exception
 {
 connection conn=jdbcutil.getconn();
 preparedstatement pstmt = conn.preparestatement("insert into operator "
 + "values(0,?,?,?,?,?,?)");
 pstmt.setstring(1,type);
 pstmt.setdouble(2,num);
 pstmt.setint(3,fromid);
 pstmt.setint(4,toid);
 //date d=new date();
 java.sql.date timepara = new java.sql.date(new date().gettime());
 pstmt.setdate(5,timepara);
 calendar cal = calendar.getinstance();//日期类
 java.sql.timestamp timestampnow = new java.sql.timestamp(cal.gettimeinmillis());//转换成正常的日期格式
 java.util.date current =new java.util.date();
 timestampnow = new java.sql.timestamp(current.gettime());
 pstmt.settimestamp(6, timestampnow);
 int k=pstmt.executeupdate();
 return k;
 }
 public static int testmoneyenough(int idnum,double awaymoney) throws exception
 {
 double balance=getbalance(idnum);
 if(balance>awaymoney)
 {
 return 1;
 }
 else
 {
 system.out.println("money is not enough!");
 return 0;
 }
 }
 public static double getbalance(int idnum) throws exception
 {
 connection conn=jdbcutil.getconn();
 string sql="select * from balance where id=?;";
 object[] params2 = {idnum};
 queryrunner qr = new queryrunner(jdbcutil2.getdatasource());
 object[] obj = qr.query(conn, sql, new arrayhandler(),params2);
 
 if(obj.length>0)
 {
 system.out.println(obj[1]);
 return ((double)obj[1]);
 }
 else
 {
 system.out.println("error");
 return -1;
 }
 }
 
 public static int updatebalance(double num,int idnum) throws exception
 {
 connection conn=jdbcutil.getconn();
 preparedstatement pstmt ;
 pstmt = conn.preparestatement("update balance set balance=balance+? where id=?;");
 pstmt.setdouble(1, num);
 pstmt.setint(2, idnum);
 int k=pstmt.executeupdate();
 return k;
 }
 public static int testpassword() throws exception
 {
 system.out.println("input your id number:");
 scanner cin=new scanner(system.in); 
 int idnum=cin.nextint();
 system.out.println("input your password:");
 string password = cin.next();
 connection conn=jdbcutil.getconn();
 string sql="select * from user where id=? and password=?";
 
 object[] params = {idnum,password,};
 
 queryrunner qr = new queryrunner(jdbcutil2.getdatasource());
 object[] obj = qr.query(conn, sql, new arrayhandler(),params);
 
 if(obj.length>0)
 {
 return idnum;
 }
 else
 {
 system.out.println("wrong message!");
 return -1;
 }
 
 }
 
 public static void openanaccount() throws exception
 {
 scanner cin=new scanner(system.in); 
 
 system.out.println("input your name:");
 string name=cin.next();
 string password;
 string passwordagain;
 
 while(true)
 {
 system.out.println("input your password:");
 password=cin.next();
 
 system.out.println("input your password again:");
 passwordagain=cin.next();
 
 if(password.equals(passwordagain))
 {
 break;
 }
 else
 {
 system.out.println("different password, please input again!");
 }
 }
 connection conn=jdbcutil.getconn();
 
 preparedstatement pstmt = conn.preparestatement("select * from maxid");
 //string sql="select max(id) from user";
 resultset rs=pstmt.executequery();
 
 rs.next();
 
 //rs.getobject(1);
 int id=rs.getint(1)+1;
 pstmt = conn.preparestatement("update maxid set id=id+1");
 pstmt.executeupdate();
 //string strmaxid=rs.getstring(1);
 //system.out.println(id);
 
 //sql="insert into user values(%d,%s,%s)";
 pstmt = conn.preparestatement("insert into user values(?,?,?)");
 
 pstmt.setint(1, id);
 pstmt.setstring(2, name);//system.out.println("again!");
 pstmt.setstring(3, password);
 
 int k = pstmt.executeupdate();
 pstmt = conn.preparestatement("insert into balance values(?,?)");
 
 pstmt.setint(1, id);
 pstmt.setdouble(2, 0);
 
 int l = pstmt.executeupdate();
 
 //system.out.println("dif");
 decimalformat df=new decimalformat("0000000000");
 if(k!=0&&l!=0)
 system.out.println("create the account successfully,your id is "+df.format(id));
 //15246785055
 k=writeoperator("enroll",-1,id,0);
// pstmt = conn.preparestatement("insert into operator values(?,?,?,?,?)");
// pstmt.setint(1, 0);
// pstmt.setstring(2, "enroll");//system.out.println("again!");
// pstmt.setint(3, 0);
// pstmt.setstring(4, string.valueof(id));//system.out.println("again!");
// pstmt.setstring(5, "");//system.out.println("again!");
// k=pstmt.executeupdate();
 if(k==1)
 system.out.println("success again");
 else
 system.out.println("tat");
 }
 
 
 public static void showbalance() throws exception
 {
 system.out.println("input your id number:");
 scanner cin=new scanner(system.in); 
 int idnum=cin.nextint();
 //system.out.println(idnum);
 system.out.println("input your password:");
 string password = cin.next();
 
 connection conn=jdbcutil.getconn();
 string sql="select * from user where id=? and password=?";
 
 object[] params = {idnum,password,};
 
 queryrunner qr = new queryrunner(jdbcutil2.getdatasource());
 
 
 object[] obj = qr.query(conn, sql, new arrayhandler(),params);
 //system.out.println("2w2w");
 //string id=obj[0].tostring();
 if(obj.length>0)
 {
 //system.out.println("-----");
 system.out.println(obj[1]+" : 你好!");
 }
 else
 {
 system.out.println("wrong message!");
 return;
 }
 int k=writeoperator("login",-1,idnum,0);
// preparedstatement pstmt ;
// pstmt = conn.preparestatement("insert into operator values(?,?,?,?,?)");
// pstmt.setint(1, 0);
// pstmt.setstring(2, "login");//system.out.println("again!");
// pstmt.setint(3, 0);
// pstmt.setstring(4, string.valueof(id));//system.out.println("again!");
// pstmt.setstring(5, "");//system.out.println("again!");
// int k=pstmt.executeupdate();
 if(k==1)
 system.out.println("success again");
 else
 system.out.println("tat");
 
 getbalance(idnum);
 
// sql="select * from balance where id=?;";
// object[] params2 = {idnum};
// qr = new queryrunner(jdbcutil2.getdatasource());
// obj = qr.query(conn, sql, new arrayhandler(),params2);
//
// if(obj.length>0)
// system.out.println(obj[1]);
// else
// system.out.println("error");
 
 }
 
 public static void saving() throws exception
 {
 scanner cin=new scanner(system.in); 
 int idnum=testpassword();
 if(idnum==-1)
 {
 system.out.println("wrong message!");
 return;
 }
 else
 {
 system.out.println("input saving number:");
 double accountnum=cin.nextdouble();
 system.out.println("before saving, your balance is "+getbalance(idnum)+"¥");
 updatebalance(accountnum,idnum);
 writeoperator("saving",accountnum,idnum,0);
 system.out.println("after saving, your balance is "+getbalance(idnum)+"¥");
 } 
 }
 
 public static void withdrawal() throws exception
 {
 scanner cin=new scanner(system.in); 
 int idnum=testpassword();
 if(idnum==-1)
 {
 system.out.println("wrong message!");
 return;
 }
 else
 {
 system.out.println("input withdrawal number:");
 double accountnum=cin.nextdouble();
 int flag=testmoneyenough(idnum,accountnum);
 if(flag>0)
 {
 system.out.println("before withdrawal, your balance is "+getbalance(idnum)+"¥");
 updatebalance(accountnum*(-1),idnum);
 writeoperator("withdrawal",accountnum,idnum,0);
 system.out.println("after withdrawal, your balance is "+getbalance(idnum)+"¥");
 }
 else
 return;
 }
 }
 
 public static void transfer() throws exception
 {
 scanner cin=new scanner(system.in);
 int idnum=testpassword();
 if(idnum>0)
 {
 system.out.println("which evil will deprive your little money?");
 int toidnum=cin.nextint();
 system.out.println("and how much?");
 double accountnum=cin.nextint();
 int flag=testmoneyenough(idnum,accountnum);
 if(flag>0)
 {
 system.out.println("before transfer, your balance is "+getbalance(idnum)+"¥");
 connection conn=jdbcutil.getconn();
 conn.setautocommit(false);
 string sql1 = "update balance set balance=balance-? where id=?";
 string sql2 = "update balance set balance=balance+? where id=?";
 preparedstatement pstmt = conn.preparestatement(sql1);
 pstmt.setdouble(1,accountnum);
 pstmt.setint(2,idnum);
 pstmt.executeupdate();
 pstmt = conn.preparestatement(sql2);
 pstmt.setdouble(1,accountnum);
 pstmt.setint(2,toidnum);
 pstmt.executeupdate();
 conn.commit();
 writeoperator("transfer",accountnum,idnum,toidnum);
 
 system.out.println("after transfer, your balance is "+getbalance(idnum)+"¥");
 }
 else
 return;
 }
 else
 return;
 }
 
 public static void seekdetails() throws exception
 {
 int idnum=testpassword();
 writeoperator("seek",0,idnum,0);
 if(idnum>0)
 {
 connection conn=jdbcutil.getconn();
 
 string sql = "select * from operator where fromid=?;";
 preparedstatement pstmt = conn.preparestatement(sql);
 pstmt.setint(1,idnum);
 resultset rs = pstmt.executequery();
 while(rs.next())
 {
 system.out.println(rs.getint(1)+" "+rs.getstring(2));
 }
 }
 else
 {
 return;
 }
 }
 
 public static void logoff() throws exception
 {
 int idnum=testpassword();
 if(idnum>0)
 {
 system.out.println("are you sure?(y/n)");
 scanner cin=new scanner(system.in);
 string s=cin.next();
 if(s.equals("y"))
 {
 connection conn=jdbcutil.getconn();
 
 string sql = "delete from user where id=?;";
 preparedstatement pstmt = conn.preparestatement(sql);
 pstmt.setint(1,idnum);
 int k=pstmt.executeupdate();
 
 sql = "delete from balance where id=?;";
 pstmt = conn.preparestatement(sql);
 pstmt.setint(1,idnum);
 int l=pstmt.executeupdate();
 if(k>0&&l>0)
 {
 writeoperator("delete",0,idnum,0);
 system.out.println("delete successfully!");
 }
 else
 {
 system.out.println("there must be something wrong!");
 }
 }
 else
 {
 return;
 }
 }
 else
 {
 return;
 }
 }
 
 public static void main(string[] args) throws exception {
 // todo auto-generated method stub
 scanner cin=new scanner(system.in);
 int flag=0;
 while(true)
 {
 system.out.println("--欢迎使用并没有钱的银行系统!--\n"
 + "(1):开户\n"
 + "(2):显示余额\n"
 + "(3):存钱\n"
 + "(4):取钱\n"
 + "(5):转账\n"
 + "(6):明细查询\n"
 + "(7):注销\n"
 + "(0):退出\n"
 + "请输入你的操作:\n");
 
 int i=cin.nextint();
 switch(i)
 {
 case 1 :
 {
 openanaccount();
 break;
 }
 case 2 :
 {
 showbalance();
 break;
 }
 case 3 :
 {
 saving();
 break;
 }
 case 4 :
 {
 withdrawal();
 break;
 }
 case 5 :
 {
 transfer();
 break;
 }
 case 6 :
 {
 seekdetails();
 break;
 }
 case 7 :
 {
 logoff();
 break;
 }
 case 0:
 {
 system.out.println("欢迎下次使用!");
 flag=1;
 break;
 }
 default:
 {
 system.out.println("输入错误,请重试!");
 }
 }
 if(flag==1)
 break;
 
 }
 cin.close();
 }
 
}

jdbcutil.java:

package mybank;
 
import java.io.filenotfoundexception;
import java.io.filereader;
import java.io.ioexception;
import java.io.reader;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.sqlexception;
import java.util.arraylist;
import java.util.properties;
 
/*
 * 为了提高代码复用性,进行简单的封装
 * */
public class jdbcutil {
 /*public static final string drivername = "com.mysql.jdbc.driver";
 public static final string url = "jdbc:mysql:///test";
 public static final string user = "root";
 public static final string password = "123456";*/
 
/* public static string driver;
 public static string url;
 public static string user;
 public static string password;
 */
 //加载配置文件
 public static properties ps = null;
 static{
 ps = new properties();
 reader r = null;
 try {
 r = new filereader("src/db.properties");
 } catch (filenotfoundexception e) {
 // todo auto-generated catch block
 e.printstacktrace();
 }
 try {
 ps.load(r);
 } catch (ioexception e) {
 // todo auto-generated catch block
 e.printstacktrace();
 }
 
 //driver = ps.getproperty(driver);
 }
 
 static{
 try {
 class.forname(ps.getproperty("driver"));
 } catch (classnotfoundexception e) {
 system.out.println("注册驱动失败。。");
 e.printstacktrace();
 }
 }
 /*static{
 try {
 class.forname(drivername);
 } catch (classnotfoundexception e) {
 system.out.println("注册驱动失败。。");
 e.printstacktrace();
 }
 }*/
 
 //提供获取连接对象的方法
 public static connection getconn(){
 connection conn = null;
 try {
 conn = drivermanager.getconnection(ps.getproperty("url"), ps.getproperty("user"), ps.getproperty("password"));
 } catch (sqlexception e) {
 system.out.println("获取连接失败。");
 e.printstacktrace();
 }
 return conn;
 }
 
 //提供一个专门执行dml语句的方法
 public static int executeudi(string sql,object[] params) throws exception{
 connection conn = getconn();
 preparedstatement pstmt = conn.preparestatement(sql);
 for(int i=0;params!=null&&i<params.length;i++){
 pstmt.setobject(i+1, params[i]);
 }
 return pstmt.executeupdate();
 }
 
 //提供一个转么执行select语句的方法,返回对象集合
 public static arraylist executequery(string sql,object[] params){
 //....
 return null;
 }
}

jdbcutil2.java:

package mybank;
 
import java.io.filenotfoundexception;
import java.io.filereader;
import java.io.ioexception;
import java.io.reader;
import java.util.properties;
 
import javax.sql.datasource;
 
import org.apache.commons.dbcp2.basicdatasource;
 
 
 
public class jdbcutil2 {
 //加载配置文件
 public static properties ps = null;
 static{
 ps = new properties();
 reader r = null;
 try {
 r = new filereader("src/db.properties");
 } catch (filenotfoundexception e) {
 // todo auto-generated catch block
 e.printstacktrace();
 }
 try {
 ps.load(r);
 } catch (ioexception e) {
 // todo auto-generated catch block
 e.printstacktrace();
 }
 
 //driver = ps.getproperty(driver);
 }
 
 /*
 * 创建连接池
 * */
 public static basicdatasource datasource = new basicdatasource();
 /*
 * 对连接池进行配置,基本配置
 * */
 static{
 datasource.setdriverclassname(ps.getproperty("driver"));
 datasource.seturl(ps.getproperty("url"));
 datasource.setusername(ps.getproperty("user"));
 datasource.setpassword(ps.getproperty("password"));
 }
 
 /*
 * 返回连接对象
 * */
 public static datasource getdatasource(){
 return datasource;
 }
}

db.properties:

driver=com.mysql.cj.jdbc.driver
url=jdbc:mysql://localhost:3306/scsxbank?useunicode=true&characterencoding=utf8&servertimezone=gmt%2b8&usessl=false
user=root
password=xxx

关于管理系统的更多内容请点击《管理系统专题》进行学习

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。