栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java基础: 现在有一银行,要做一套取款系统,需要使用控制台操作。银行所提需求要求 必能够进行简单地新增用户、修改用户信息、存钱、取钱操作。要求使用控制台操作scanner,同时使用JDBC连接数据

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java基础: 现在有一银行,要做一套取款系统,需要使用控制台操作。银行所提需求要求 必能够进行简单地新增用户、修改用户信息、存钱、取钱操作。要求使用控制台操作scanner,同时使用JDBC连接数据

源码:

首先链接数据库 :创建包com.hp.util   在包里创建类DBHepler 在包里链接数据库

package com.hp.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBHepler {
    public static Connection  getConn() {
        Connection conn= null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String  url="jdbc:mysql://localhost:3306/usmydb?serverTimezone=Asia/Shanghai";
            conn= DriverManager.getConnection(url,"root","123456");
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return  conn;
    }
    //输出看一下是否链接
    public static void main(String[] args) {

        System.out.println(getConn());
    }
}

运行效果:

 输出这个则数据库链接成功

然后创建包com.hp.test  在包里创建类Test 在test里面实现各种方法

package com.hp.test;

import com.hp.util.DBHepler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Test {
    public  void  Meu(){




        System.out.println("**************************");
        System.out.println("*******欢迎来到银行********");
        System.out.println("***1、用户新增***");
        System.out.println("***2、修改用户***");
        System.out.println("***3、存钱***");
        System.out.println("***4、取钱***");
        System.out.println("***5、退卡***");
        System.out.println("**************************");
        System.out.println("请选择您要的操作内容:");

        Scanner  sc=new Scanner(System.in);
        String   str=sc.next();
        char  c=str.charAt(0);
        switch (c){
            case '1':
                add();
                break;
            case '2':
                update();
                break;
            case '3':
                deposit();
                break;
            case '4':
                drawMonry();
                break;
            case '5':
                withdraw();
                break;
        }
    }
    //展示全部
    public void  showAll(){
        Connection  conn= DBHepler.getConn();
        PreparedStatement  ps=null;
        String  sql="select  *  from  bank";
        try {
            ps=conn.prepareStatement(sql);
            ResultSet  rs=ps.executeQuery();
            while (rs.next()) {
                System.out.println("用户id:"+rs.getInt(1)+"  "+"用户姓名:"+rs.getString(2)+"  "
                        +"用户密码:"+rs.getString(3)+"  "+"用户余额"+rs.getDouble(4)+"  ");

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //增加用户
    public  void  add(){
        System.out.println("********用户新增********");
        Scanner  sc=new Scanner(System.in);
        System.out.println("请输入添加用户账号");
        String  name=sc.next();
        System.out.println("请输入添加用户密码");
        String  password=sc.next();
        System.out.println("请输入添加用户余额");
        double  banlace=sc.nextDouble();
        Connection  conn=DBHepler.getConn();
        String  sql="insert into bank  values(null,?,?,?)";
        PreparedStatement  ps=null;
        try {
            ps=conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            ps.setDouble(3, banlace);
            int  i=ps.executeUpdate();
            if (i>0){
                System.out.println("添加成功");
                Meu();
            }else{
                System.out.println("添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //修改用户
    public  void  update(){
        System.out.println("********用户修改********");
        System.out.println("************修改用户信息**************");
        Scanner sc= new Scanner(System.in);
        System.out.println("输入修改的用户id");
        int id=sc.nextInt();
        System.out.println("输入修改用户账号");
        String name=sc.next();
        System.out.println("输入修改用户密码");
        String password=sc.next();
        System.out.println("输入修改用户金额");
        double banlance=sc.nextDouble();
        Connection  conn=DBHepler.getConn();
        String  sql="update  bank  set  name=?,password=?,balance=?  where  id=?";
        PreparedStatement  ps=null;
        try {
            ps=conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, password);
            ps.setDouble(3, banlance);
            ps.setInt(4, id);
            int   i=ps.executeUpdate();
            if(i>0){
                System.out.println("修改成功");
                Meu();
            }else {
                System.out.println("修改失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                conn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //用户存钱
    public  void  deposit(){
        System.out.println("*******用户存钱********");
        Scanner  sc=new Scanner(System.in);
        System.out.println("请输入存钱用户的姓名:");
        String  name=sc.next();
        System.out.println("请输入存钱用户的密码:");
        String  password=sc.next();
        System.out.println("请输入存钱用户的金额:");
        double  balance=sc.nextDouble();
        Connection  connn=DBHepler.getConn();
        String  sql="update bank set balance=balance+? where name=? and password=?";
        PreparedStatement  ps=null;
        try {
            ps= connn.prepareStatement(sql);
            ps.setDouble(1, balance);
            ps.setString(2, name);
            ps.setString(3, password);
            int  i=ps.executeUpdate();
            if (i>0) {
                System.out.println("存入成功,存入金额为:"+balance);
                Meu();
            }else{
                System.out.println("存入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    //用户取钱
    public  void  drawMonry(){
        System.out.println("*******用户取钱********");
        Scanner  sc=new Scanner(System.in);
        System.out.println("请输入取钱用户的姓名:");
        String  name=sc.next();
        System.out.println("请输入取钱用户的密码:");
        String  password=sc.next();
        System.out.println("请输入取钱用户的金额:");
        double  balance=sc.nextDouble();
        Connection  connn=DBHepler.getConn();
        String  sql="update bank set balance=balance-? where name=? and password=?";
        PreparedStatement  ps=null;
        try {
            ps= connn.prepareStatement(sql);
            ps.setDouble(1, balance);
            ps.setString(2, name);
            ps.setString(3, password);
            int  i=ps.executeUpdate();
            if (i>0) {
                System.out.println("取出成功,取出金额为:"+balance);
                Meu();
            }else{
                System.out.println("取出失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                connn.close();
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public  void  withdraw(){
        System.out.println("退出成功,请收好您的卡");
        System.exit(0);
    }
}

最后创建包 com.hp.controller  在包里面创建测试类bank

package com.hp.controller;

import com.hp.test.Test;

public class Bank {
    public static void main(String[] args) {
        Test  t=new Test();
        t.Meu();
    }

}

 最后运行效果:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/341033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号