仿银行系统

随便写的一个仿银行登录系统,没有设置安全

主方法:

import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;


public class Login extends JFrame implements ActionListener {
 
 JLabel _name,_password;
 JButton _logon,_exit;
 JTextField _jtfname;
 JPasswordField _jtfpassword;
 JPanel _jpname,_jppassword,_jpbutton;
 
    public Login() {
     super("欢迎登录");
     _name   =   new JLabel("用户名:");
     _password = new JLabel("密    码:");
     _logon = new JButton("登录");
     _logon.addActionListener(this);
     _exit  = new JButton("退出");
     _exit.addActionListener(this);
     _jtfname = new JTextField(8);
     _jtfpassword = new JPasswordField(8);
     _jpname = new JPanel();
     _jppassword = new JPanel();
     _jpbutton = new JPanel();
     
     _jpname.add(_name);
     _jpname.add(_jtfname);
     
     _jppassword.add(_password);
     _jppassword.add(_jtfpassword);
     
     _jpbutton.add(_logon);
     _jpbutton.add(_exit);
     
     add(_jpname,"North");
     add(_jppassword);
     add(_jpbutton,"South");
     
     pack();
     setVisible(true);
     int W = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
     int H = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
     this.setLocation((W-this.getWidth())/2,(H-this.getHeight())/2);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    public static void main (String[] args) {
     new Login(); 
 }
 public void actionPerformed(ActionEvent e){
  if(e.getSource()==_logon){
   String username = _jtfname.getText().trim();
      String password = new String(_jtfpassword.getPassword()).trim();
      DB db = new DB();
      if(db.login(username,password)){
       JOptionPane.showMessageDialog(this,"登录成功");
       new Select(username,password);
       this.dispose();
      }
      else
      {
       JOptionPane.showMessageDialog(this,"登录失败,请确认您的输入正确!"+'/n'+"或者已经成功连接数据库!!!");
      }
  }
  if(e.getSource()==_exit){
   System.exit(0);
  }
 }
}

 

选择界面:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;

public class Select extends JFrame implements ActionListener{

 JLabel _show,welcome,welcome1,welcome2,_empty;
 JPanel _jp1,_jp2,_jp3,_jp4,_jp5;
 JButton _get,_add,_query,_rework,_exit;
 String name,username,password;
 
    public Select(String username,String password) {
     super("欢迎您!");
     this.username = username;
     this.password = password;
     name = username;
     _show = new JLabel("欢迎您  "+name+"  先生/女士");
     welcome = new JLabel("欢迎您登录本行自动取款系统");
     welcome1= new JLabel("本行采取的是世界定级的安全技术,24小时监控");
     welcome2= new JLabel("保证您的信用卡不会被黑~~~~");
     _empty = new JLabel();
     _query = new JButton("查询余额");
     _query.addActionListener(this);
     _add = new JButton("存款");
     _add.addActionListener(this);
     _get = new JButton("取款");
     _get.addActionListener(this);
     _rework = new JButton("修改密码");
     _rework.addActionListener(this);
     _exit = new JButton("退出");
     _exit.addActionListener(this);
     _jp1 = new JPanel(new GridLayout(4,1));
     _jp2 = new JPanel(new GridLayout(3,1));
     _jp3 = new JPanel(new GridLayout(3,1));
     _jp4 = new JPanel();
     _jp5 = new JPanel();
     _jp1.add(_show);
     _jp1.add(welcome);
     _jp1.add(welcome1);
     _jp1.add(welcome2);
     _jp2.add(_query);
     _jp2.add(_add);
     _jp2.add(_get);
     _jp3.add(_empty);
     _jp3.add(_rework);
     _jp3.add(_exit);
     
     add(_jp1,"North");
     _jp4.add(_jp2);
     _jp5.add(_jp3);
     add(_jp4,"West");
     add(_jp5,"East");
     pack();
     setResizable(false);
     setVisible(true);
     int W = (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
     int H = (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
     this.setLocation((W-this.getWidth())/2,(H-this.getHeight())/2);
     this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
   
    private boolean isDigitString(String s){
     boolean b = true;
     for (int i = 0; i<s.length(); i++){
      char c= s.charAt(i);
      if(!Character.isDigit(c)){
       b = false;
       break;
      }
     }
     return b;
    }
   
    public void actionPerformed(ActionEvent e){
     if(e.getSource()==_query)
     {
      double  balance = new DB().queryBalance(username,password);
       JOptionPane.showMessageDialog(this,""+balance+"");
     }
     if(e.getSource()==_add)
     {
      String addmoney = JOptionPane.showInputDialog(this,"存款","0.0");
  if(addmoney==null)
      {
       JOptionPane.showMessageDialog(this,"您的操作有误!");
      }else{
      if(isDigitString(addmoney))
      {
       double money = Integer.parseInt(addmoney);
       new DB().addMoney(username,password,money); 
       JOptionPane.showMessageDialog(this,"操作成功!");
      }
      else
      {
        JOptionPane.showMessageDialog(this,"您的操作有误!");
        }}
     }
     if(e.getSource()==_get)
     {
      DB db = new DB();
      String addmoney = JOptionPane.showInputDialog(this,"您有"+db.queryBalance(username,password)+"人民币","0.0");
      if(isDigitString(addmoney))
      {
       double money = Integer.parseInt(addmoney);
       if(db.queryBalance(username,password)>=money)
       {
       new DB().getMoney(username,password,money); 
       JOptionPane.showMessageDialog(this,"操作成功!"); 
       }
   else
       {
        JOptionPane.showMessageDialog(this,"操作失败!");
       }
      }
     }
     if(e.getSource()==_rework)
     {
      DB db = new DB();
      String repassword = JOptionPane.showInputDialog(this,"");
      if(repassword==null)
      {
       JOptionPane.showMessageDialog(this,"取消成功!");
      }
   else
   {
   new DB().rework(username,repassword);
   if(db.rework(username,repassword)==repassword)
   {
   JOptionPane.showMessageDialog(this,"修改成功"+'/n'+"请重新登陆!");
      dispose();
      new Login(); 
   } 
  }
     }
     if(e.getSource()==_exit)
     {
      System.exit(0);
     }
    }
}

 

银行DAO:

import java.sql.*;
//对数据库test的操作类
public class DB{

 Connection con=null;
 PreparedStatement ps = null;
 ResultSet rs = null;
     
    public DB() {
     try{
      Class.forName("com.mysql.jdbc.Driver");
      con = DriverManager.getConnection(
       "jdbc:mysql://localhost:3306/test","root","123");  
     }catch(ClassNotFoundException e){
      
     }catch(SQLException e){
      
     }
    }
   
    public boolean login(String username,String password){
     boolean b = false;
     try{
      String sql = "select * from bank_user where name=? and password=?";
      ps = con.prepareStatement(sql);
      ps.setString(1,username);
      ps.setString(2,password);
      rs = ps.executeQuery();
      if(rs.next()){
       b = true;
      }
      else{
       b = false;
      }
     }catch(SQLException e){
      System.out.println (e.getMessage());
     }
     return b;
    }
   
    public double queryBalance(String username,String password){
     double b =0;
  String sql = "select * from bank_user where name=? and password=?";
  try{
   ps = con.prepareStatement(sql);
   ps.setString(1,username);
   ps.setString(2,password);
   rs = ps.executeQuery();
   if(rs.next()){
    b = rs.getDouble("balance");
   }
  }catch(SQLException e){
  }
  return b;
    }
   
    public void getMoney(String username,String password,double amount){
     try{
      String sql = "update bank_user set balance=balance-? where name=? and password=?";
      ps = con.prepareStatement(sql);
      ps.setDouble(1,amount);
      ps.setString(2,username);
      ps.setString(3,password);
      ps.executeUpdate();
     }catch(SQLException e){
     }
    }
   
    public void addMoney(String username,String password,double amount){
     try{
      String sql = "update bank_user set balance=balance+? where name=? and password=?";
      ps = con.prepareStatement(sql);
      ps.setDouble(1,amount);
      ps.setString(2,username);
      ps.setString(3,password);
      ps.executeUpdate();
     }catch(SQLException e){
     }
    }
    public String rework(String username,String repassword)
    {
     try{
      String sql = "update bank_user set password=? where name=?";
      ps = con.prepareStatement(sql);
      ps.setString(1,repassword);
      ps.setString(2,username);
      ps.executeUpdate();
     }catch(SQLException e){
     }
     return repassword;
    }
}

你可能感兴趣的:(sql,String,user,query,import,login)