小型网上自助银行

匿名 (未验证) 提交于 2019-12-02 23:43:01

1、数据表

2、DBconnection类(连接数据库)

package LESSON5; import java.sql.*; public class DBconnection {     private String driver="com.mysql.jdbc.Driver";//驱动程序名       private String url="jdbc:mysql://localhost:3306/db_demo?useUnicode=true&characterEncoding=utf8";     private String user="root";//MySQL配置时的用户名     private String password="123456";//MySQL配置时的密码     public Connection getConnection(){          Connection con=null;          try {             Class.forName(driver);         } catch (ClassNotFoundException e) {                          e.printStackTrace();         }          try {             con=DriverManager.getConnection(url, user, password);         } catch (SQLException e) {                         e.printStackTrace();         }                 return con;     }                     }

3、测试类

package LESSON5; import java.util.*; import java.sql.*; class Account{     public float show(String name)  throws Exception{//显示余额         DBconnection dbconnection=new DBconnection();         Connection conn=dbconnection.getConnection();         String sql = "select * from users where username='"+name+"' ";          Statement statement;                 statement = conn.createStatement();         ResultSet rs=statement.executeQuery(sql);         String username=null;         String accountid=null;         float ba=0;             //System.out.println("用户名"+"\t"+"账户"+"\t"+"账户余额");         while(rs.next()){         accountid=rs.getString("accountid");                 ba=rs.getFloat("balance");         System.out.println("账户"+"\t\t "+"余额");         System.out.println(accountid+"\t"+ba);          }         return ba;//返回新的账户余额供下一次存取款操作使用             }         public void deposit(float amount,float balance,String username)throws Exception{//存款         float NewBalance=amount+balance;         DBconnection dbconnection=new DBconnection();         Connection conn=dbconnection.getConnection();         int i=0;         String sql="update users set  balance=?  where username=?" ;//注意要有where条件                 PreparedStatement preStmt =conn.prepareStatement(sql);         preStmt.setFloat(1, NewBalance);         preStmt.setString(2,username);         preStmt.executeUpdate();         }     public void withdraw(float amount,float balance,String username)throws Exception{//取款         if(amount<=balance){         float NewBalance=balance-amount;         DBconnection dbconnection=new DBconnection();         Connection conn=dbconnection.getConnection();         int i=0;         String sql="update users set  balance=?  where username=?" ;//注意要有where条件                 PreparedStatement preStmt =conn.prepareStatement(sql);         preStmt.setFloat(1, NewBalance);         preStmt.setString(2,username);                 preStmt.executeUpdate();                                         }     else{         System.out.println("余额不足");     } } } public class exercise3 {     public static void main(String[] args) throws Exception {         System.out.println("欢迎来到XX银行自助服务终端");         DBconnection dbconnection=new DBconnection();         Account account=new Account();                 Scanner sc=new Scanner(System.in);         int num=3;         while(true){         System.out.println("请输入用户名");         String name=sc.nextLine();         System.out.println("请输入密码");         String p=sc.nextLine();         String sql = "select * from users where username='"+name+"'and password='"+p+"'";                  Connection conn=dbconnection.getConnection();         Statement statement = conn.createStatement();         ResultSet rs=statement.executeQuery(sql);//ResultSet类,用于存放获取的结果集            String username=null;         String accountid=null;         float ba=0;             if(rs.next()){             username=rs.getString("username");             accountid=rs.getString("accountid");                     ba=rs.getFloat("balance");             System.out.println("欢迎您,"+username);             System.out.println("账户"+"\t\t "+"余额");             System.out.println(accountid+"\t"+ba);                     rs.close();             conn.close();                 while(true){                                             System.out.println("请输入要执行的操作:1 存款 2取款 3查询余额 4退出");             int i=sc.nextInt();             if(i==1){                 System.out.println("请输入存款金额");                 float amount1=sc.nextFloat();                 account.deposit(amount1,ba,username);                 ba=account.show(username);                                 }else if(i==2){                 System.out.println("请输入取款金额");                 float amount2=sc.nextFloat();                 account.withdraw(amount2,ba,username);                 ba=account.show(username);                                             }else if(i==3){                 account.show(username);                             }             else if(i==4){                 System.exit(0);                             }             else{                 System.out.println("输入错误");             }                                                                                             }            }         else{             num--;             if(num==0){                 System.out.println("您的账户已冻结");                 break;             }             System.out.println("账号或密码错误,您还有"+num+"次机会.");         }         }          } }

4、运行结果

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!