小型网上自助银行

1、数据表

2、DBconnection类(连接数据库)

package LESSON5;
import java.sql.*;//*为通配符,表示所有的。即导入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、运行结果

posted @ 2019-06-15 22:29  勤奋的园  阅读(154)  评论(0编辑  收藏  举报