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();
    }

}

 最后运行效果:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是用Java编写的一个简单银行转账系统,包括取款,存款,转账等功能,其中用到了数据库的连接,采用Eclipse编写,包含数据库的设计文件。非常适合有一定基础Java初学者使用。 package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.sql.*; /** * * @author gujunjia */ public class DataBase { static Connection conn; static PreparedStatement st; static ResultSet rs; /** * 加载驱动 */ public static void loadDriver() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("加载驱动失败"); } } /** * 创建数据库的连接 * * @param database * 需要访问的数据库的名字 */ public static void connectionDatabase(String database) { try { String url = "jdbc:mysql://localhost:3306/" + database; String username = "root"; String password = "gujunjia"; conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { System.out.println(e.getMessage()); } } /** * 关闭数据连接 */ public static void closeConnection() { if (rs != null) { // 关闭记录集 try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (st != null) { // 关闭声明 try { st.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { // 关闭连接对象 try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } package com.gujunjia.bank; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * 本类主要实现整个系统的界面 * * @author gujunjia */ public class MainFrame extends JFrame implements ActionListener, FocusListener { /** * */ private static final long serialVersionUID = 1L; public static String userId; JTextField userIdText; JPasswordField passwordText; JButton registerButton; JButton logInButton; public MainFrame() { super("个人银行系统"); this.setSize(400, 500); this.setLocation(getMidDimension(new Dimension(400, 500))); getAppearance(); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } /** * 获取屏幕的中间尺寸 * * @param d * Dimension类型 * @return 一个Point类型的参数 */ public static Point getMidDimension(Dimension d) { Point p = new Point(); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); p.setLocation((dim.width - d.width) / 2, (dim.height - d.height) / 2); return p; } /** * 布局 * * @return Container */ public Container getAppearance() { Container container = this.getContentPane(); container.setLayout(new GridLayout(4, 0)); JLabel label1 = new JLabel("个人银行系统"); label1.setFont(new Font("楷体", Font.BOLD, 40)); JLabel label2 = new JLabel("账号:"); label2.setFont(new Font("楷体", Font.PLAIN, 15)); JLabel label3 = new JLabel("密码:"); label3.setFont(new Font("楷体", Font.PLAIN, 15)); userIdText = new JTextField(20); userIdText.addFocusListener(this); passwordText = new JPasswordField(20); passwordText.addFocusListener(this); JPanel jp1 = new JPanel(); JPanel jp2 = new JPanel(); JPanel jp3 = new JPanel(); JPanel jp4 = new JPanel(); jp1.add(label1); jp2.add(label2); jp2.add(userIdText); jp3.add(label3); jp3.add(passwordText); registerButton = new JButton("注册"); registerButton.addActionListener(this); registerButton.setFont(new Font("楷体", Font.BOLD, 15)); logInButton = new JButton("登录"); logInButton.addActionListener(this); logInButton.setFont(new Font("楷体", Font.BOLD, 15)); jp4.add(registerButton); jp4.add(logInButton); container.add(jp1); container.add(jp2); container.add(jp3); container.add(jp4); return container; } public void actionPerformed(ActionEvent e) { Object btn = e.getSource(); if (btn == registerButton) { new Register(); } else if (btn == logInButton) { String id = userIdText.getText().trim(); String password = new String(passwordText.getPassword()); Bank bank = new Bank(); if (id.equals("") || password.equals("")) { JOptionPane.showMessageDialog(null, "请输入账号和密码"); } else { String dPassword = bank.getPassword(id); if (password.equals(dPassword)) { userId = id; this.dispose(); new UserGUI(); } else { JOptionPane.showMessageDialog(this, "密码或用户名错误", "错误", JOptionPane.ERROR_MESSAGE); } } } } @Override public void focusGained(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { userIdText.setText(""); userIdText.setFont(new Font("宋体", Font.BOLD, 15)); } else if (text == passwordText) { passwordText.setText(""); } } @Override public void focusLost(FocusEvent e) { Object text = e.getSource(); if (text == userIdText) { if (userIdText.getText().equals("")) { userIdText.setText("请输入账号"); userIdText.setFont(new Font("楷体", Font.ITALIC, 15)); } } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值