• 实现微信转账功能


    package com.wx.view;
    
    import java.util.Scanner;
    
    import com.wx.Money;
    import com.wx.User;
    import com.wx.dao.WxDao;
    
    public class Transfer {
        public static void main(String[] args) {
            System.out.println("*************微信转账模块**************");
            Scanner sc = new Scanner(System.in);
            System.out.print("请输入您的微信账号:");
            String uname = sc.next();
            System.out.print("请输入您的微信密码:");
            String pwd = sc.next();
            //判断用户名和密码的结果
            User u = new WxDao().checkLogin(uname, pwd);
            if(u!=null){
                //登陆成功
                System.out.println("恭喜您登陆成功!");
                System.out.println("用户名:"+u.getUname());
                //根据用户名获取用户账号金额
                Money m = new WxDao().getMoney(uname);
                System.out.println("账户余额:"+m.getMoeny());
                //获取好友名称与金额
                System.out.print("请输入转账的好友名称:");
                String fname = sc.next();
                System.out.print("请输入转账金额:");
                double money = sc.nextDouble();
                //执行转账的操作
                new WxDao().Transfer(money, uname, fname);
                //重新查询账户金额
                 m = new WxDao().getMoney(uname);
                System.out.println("转账成功!您的当前账户余额为"+m.getMoeny());
            }else{
                //登陆失败
                System.out.println("您的账号或密码输入错误!!");
            }
        }
    }
    package com.wx.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.wx.Money;
    import com.wx.User;
    
    public class WxDao {
        //校验用户名和密码
        public User checkLogin(String user,String pwd){
            PreparedStatement psmt = null;
            ResultSet rs = null;
            Connection con = null;
            User u = null;
            con = Tool.getConn2();
            try {
                //根据用户输入的用户名和密码查找是否有匹配的数据
                psmt = con.prepareStatement("select * from wx_user where uname=? and pwd = ?");
                psmt.setString(1, user);
                psmt.setString(2, pwd);
                rs = psmt.executeQuery();
                if(rs.next()){
                    //将查询的用户名和密码封装到用户对象u中
                    u= new User();    
                    u.setUid(rs.getInt(1));
                    u.setUname(rs.getString(2));
                    u.setPwd(rs.getString(3));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally{
                Tool.close(con, psmt);
            }
            return u;
        }
        
        //实现查询账户余额功能
            public Money getMoney(String user){
                PreparedStatement psmt = null;
                ResultSet rs = null;
                Connection con = null;
                Money m = new Money();
                con = Tool.getConn2();
                try {
                    psmt = con.prepareStatement("select * from wx_money where uname=? ");
                    psmt.setString(1, user);
                    rs = psmt.executeQuery();
                    if(rs.next()){
                        m.setUname(rs.getString(1));
                        m.setMoeny(rs.getDouble(2));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }finally{
                    Tool.close(con, psmt);
                }
                return m;
            }
            //实现转账功能
            public  void Transfer(double money,String uname,String fname){
                PreparedStatement psmt = null;
                Connection con = null;
                con = Tool.getConn2();
                try {
                    //减去用户账户中的金额
                    psmt = con.prepareStatement("update wx_money set money=money-? where uname=? ");
                    psmt.setDouble(1, money);
                    psmt.setString(2, uname);
                    psmt.executeUpdate();
                    //增加好友账户中的金额
                    psmt = con.prepareStatement("update wx_money set money=money+? where uname=? ");
                    psmt.setDouble(1, money);
                    psmt.setString(2, fname);
                    psmt.executeUpdate();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
    }
    package com.wx.dao;
    
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    public class Tool {
        
    
        public static Connection getConn2(){
            Connection conn = null;
            try {
                Class.forName("com.mysql.jdbc.Driver");
                conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","sys");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return conn;
        }
        
        
        public static void close(Connection con,PreparedStatement psmt){
            
            try {
                
                if(con!=null){
                    con.close();
                }
                if(psmt!=null){
                    psmt.close();
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    
        }
    }
    package com.wx;
    
    public class Money {
        String uname;
        double moeny;
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public double getMoeny() {
            return moeny;
        }
        public void setMoeny(double moeny) {
            this.moeny = moeny;
        }
        
    }
    package com.wx;
    
    public class User {
        int uid;
        String uname;
        String pwd;
        public int getUid() {
            return uid;
        }
        public void setUid(int uid) {
            this.uid = uid;
        }
        public String getUname() {
            return uname;
        }
        public void setUname(String uname) {
            this.uname = uname;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        
    }
  • 相关阅读:
    背水一战 Windows 10 (26)
    背水一战 Windows 10 (25)
    背水一战 Windows 10 (24)
    背水一战 Windows 10 (23)
    背水一战 Windows 10 (22)
    背水一战 Windows 10 (21)
    背水一战 Windows 10 (20)
    背水一战 Windows 10 (19)
    背水一战 Windows 10 (18)
    背水一战 Windows 10 (17)
  • 原文地址:https://www.cnblogs.com/wangzhanxin98/p/9221731.html
Copyright © 2020-2023  润新知