java 银行管理系统(连接数据库Mysql)

java 银行管理系统(连接数据库Mysql)

  • 逻辑与功能图
    • 实现逻辑
    • 功能图
  • 代码

逻辑与功能图

实现逻辑

程序共有三个文件
Bankmangement.java:包括程序运行的主函数,方法等
User.java:实体类,用于创建对象
JDBCUtils:对数据库方法进行封装

功能图

1.User实体类
java 银行管理系统(连接数据库Mysql)_第1张图片
数据库共有三张表,分别是admin管理员帐号,user用户账号,record用户记录。
2.Bankmangement功能图
java 银行管理系统(连接数据库Mysql)_第2张图片

代码

Bankmangement

package bank;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
public class Bankmanagement {                   //知识点2:类的声明
    private static Scanner sc = new Scanner(System.in);         //知识点3:类成员的访问控制(私有,只能被这个类本身访问)
    public static void main(String[] args) {
        menu();
    }
    public static void menu(){
        System.out.println("-------------------欢迎进入银行储蓄系统----------------");
        System.out.println("-------------------请选择您的登录方式------------------");
        System.out.println("-------------------0、登陆管理员账户-----------------");
        System.out.println("-------------------1、登录用户账户-----------------");
        System.out.println("-------------------2、注册账户-----------------");
        String select = sc.next();
        switch (select) {
            case "0":
                //登录
                login();      //知识点8:调用方法
                break;
            case "1":
                //登录
                login1();      //知识点8:调用方法
                break;
            case "2":
                //注册:将注册操作定义到一个方法中然后调用方法
                register();
                break;
        }
    }
    //注册账户
    public static void register() {
        System.out.println("--------------欢迎进入注册页面-----------");
        Random r = new Random();
        System.out.println("请输入账户名:");
        String username = sc.next();
        System.out.println("请输入账户密码");
        String password = sc.next();
        System.out.println("请输入手机号:");
        String phonenumber = sc.next();
        System.out.println("请输入地址:");
        String address = sc.next();
        System.out.println("请输入账户余额:");
        float balance = sc.nextFloat();
        boolean flag = new Bankmanagement().adminlogin(username,password);
        while (!flag){
            int id = r.nextInt(100000,999999);
            Connection conn = null;
            Statement stmt = null;
            PreparedStatement pstmt = null;
            try{
                conn = JDBCUtils.getConnection();
                //定义sql语句
                String sql = "insert into user(username,id,password,phonenumber,address,balance) values(?,?,?,?,?,?)";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,username);
                pstmt.setInt(2,id);
                pstmt.setString(3,password);
                pstmt.setString(4,phonenumber);
                pstmt.setString(5,address);
                pstmt.setDouble(6,balance);
                //判断
                int count = pstmt.executeUpdate();
                if (count>0){
                    System.out.println("注册成功");
                    System.out.println("您的id账号为:"+id);
                    menu();
                }else{
                    System.out.println("注册失败");
                    return;
                }

            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                JDBCUtils.close(pstmt,conn);
            }

        }
        System.out.println("账户已存在请重新输入!");
    }
    public boolean judgement(String username){
        if (username == null){
            return false;
        }
        //数据库连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "select * from user where username = ?";
            //获取执行对象
            pstmt = conn.prepareStatement(sql);
            //给问号赋值
            pstmt.setString(1,username);
            //执行查询
            rs = pstmt.executeQuery();
            //判断
            return rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }
        return false;
    }
    //登录管理员账户
    public static void login(){
        System.out.println("--------欢迎进入登录页面--------");
        for (int i = 1; i <= 3; i++) {
            System.out.println("请输入管理员账号");
            String username = sc.next();
            System.out.println("请输入管理员密码");
            String password = sc.next();

            boolean flag = new Bankmanagement().adminlogin(username,password);
            while (flag) {            //知识点12:while语句
                System.out.println("请录入您要进行的操作");
                System.out.println("1、添加账户");
                System.out.println("2、所有账户个人信息的查询");
                System.out.println("3、删除账户");
                System.out.println("4、退出系统");
                String num = sc.next();

                switch (num) {           //知识点13:switch选择语句
                    case "1":
                        Add();
                        break;
                    case "2":
                        checkAllUsers();
                        break;
                    case "3":
                        //注册:将注册操作定义到一个方法中然后调用方法
                        Delete();
                        break;
                    case "4":
                        menu();
                        break;
                }

            }
                System.out.println("登录失败,用户名或密码错误");


        }
        System.out.println("您的3次机会已经用完,自动退出系统");
        System.exit(0);
    }
    public boolean adminlogin(String username,String password){
        if (username == null||password == null){
            System.out.println("!!!!");
            return false;
        }
        //数据库连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "select * from admin where username = ? and password = ?";
            //获取执行对象
            pstmt = conn.prepareStatement(sql);
            //给问号赋值
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            //执行查询
            rs = pstmt.executeQuery();
            //判断
            return rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }
        return false;
    }
    //登录账户
    public static void login1(){
        System.out.println("--------欢迎进入登录页面--------");
        for (int i = 1; i <= 3; i++) {
            System.out.println("请录入您要登录的用户名");
            String username = sc.next();
            System.out.println("请录入您的id");
            int id = sc.nextInt();
            System.out.println("请录入您要登录的密码");
            String password = sc.next();
                    boolean flag = new Bankmanagement().userlogin(username,password,id);
                    while (flag) {            //知识点12:while语句
                        User u = new User(username,id,password);
                        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("6、转账");
                        System.out.println("7、打印清单");
                        System.out.println("8、注销账户");
                        System.out.println("9、退出系统");
                        String num2 = sc.next();
                        switch (num2) {           //知识点13:switch选择语句
                            case "1":
                                updateMsg(u);
                                break;
                            case "2":
                                updatePwd(u);
                                break;
                            case "3":
                                updateOne (u);
                                break;
                            case "4":
                                saveMoney(u);
                                break;
                            case "5":
                                drawMoney(u);
                                break;
                            case "6":
                                transferMoney(u);
                                break;
                            case "7":
                                print(u);
                                break;
                            case "8":
                                cancel(u);
                                break;
                            case "9":
                                menu();
                                break;
                        }
                    }
                        System.out.println("登录失败");

        }
        System.out.println("您的3次机会已经用完,自动退出系统");
        System.exit(0);
    }
    public boolean userlogin(String username,String password,int id){
        if (username == null||password == null||id ==0){
            System.out.println("!!!!");
            return false;
        }
        //数据库连接
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "select * from user where username = '"+username+"'and id = '"+id+"' and password = '"+password+"'";
            //获取执行对象
            stmt = conn.createStatement();
            //执行查询
            rs = stmt.executeQuery(sql);
            //判断

            return rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,stmt,conn);
        }
        return false;
    }


    //修改用户信息
    public static void updateMsg(bank.User u) {                //知识点:声明一个方法
        System.out.println("----------欢迎进入修改账户信息模块----------");
            Connection conn = null;
            PreparedStatement pstmt = null;
            try{
                System.out.println("请录入您要修改的手机号:");
                String phonenumber = sc.next();
                System.out.println("请录入您要修改的地址:");
                String address = sc.next();
                System.out.println("请录入您要修改的余额:");
                float balance = Float.parseFloat(sc.next());
                conn = JDBCUtils.getConnection();
                //定义sql语句
                String sql="update user set phonenumber=?,address=?,balance=? where username=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,phonenumber);
                pstmt.setString(2,address);
                pstmt.setDouble(3,balance);
                pstmt.setString(4,u.getName());

//            pstmt.executeUpdate();
                //获取执行对象
//            stmt = conn.createStatement();
                //执行查询
//            rs = stmt.executeQuery(sql);
                //判断
                int count = pstmt.executeUpdate();
                if (count>0){
                    System.out.println("修改成功");
                }else{
                    System.out.println("修改失败");
                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                JDBCUtils.close(pstmt,conn);
            }
    }
    //删除账户
    public static void Delete()
    {
        System.out.println("----------欢迎进入用户信息删除模块----------");
        System.out.println("请录入您要删除的用户名");
        String username = sc.next();
        boolean flag = new Bankmanagement().judge(username);

        while (flag){
            Connection conn = null;
            Statement stmt = null;
            PreparedStatement pstmt = null;
            try{
                conn = JDBCUtils.getConnection();
                //定义sql语句
                String sql = "delete from user where username=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,username);
//            pstmt.executeUpdate();
                //获取执行对象
//            stmt = conn.createStatement();
                //执行查询
//            rs = stmt.executeQuery(sql);
                //判断
                int count =pstmt.executeUpdate();
                if (count>0){
                    System.out.println("删除成功");
                    return;
                }else{
                    System.out.println("删除失败,请重试!");
                    Delete();
                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                JDBCUtils.close(pstmt,conn);
            }

        }
        System.out.println("你输入的账号不存在,请重新输入!");
        Delete();

    }
    //添加用户
    public static void Add(){
        System.out.println("请输入增加的账户名:");
        String username = sc.next();
        System.out.println("请输入账户密码");
        String password = sc.next();
        System.out.println("请输入手机号:");
        String phonenumber = sc.next();
        System.out.println("请输入地址:");
        String address = sc.next();
        System.out.println("请输入账户余额:");
        float balance = sc.nextFloat();
        Random r = new Random();
//        bank.User user2 = new bank.User(username , number, password,  address, balance);    //知识点:构造方法
//        userlist.add(user2);   //知识点19:调用集合添加方法
//
//        System.out.println("该用户添加成功!!");
//        return;
        int id = r.nextInt(100000,999999);
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement pstmt = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "insert into user(username,id,password,phonenumber,address,balance) values(?,?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,username);
            pstmt.setInt(2,id);
            pstmt.setString(3,password);
            pstmt.setString(4,phonenumber);
            pstmt.setString(5,address);
            pstmt.setDouble(6,balance);
//            pstmt.executeUpdate();
            //获取执行对象
//            stmt = conn.createStatement();
            //执行查询
//            rs = stmt.executeQuery(sql);
            //判断
            int count = pstmt.executeUpdate();
            if (count>0){
                System.out.println("添加成功");
                System.out.println("id账号为:"+id);
            }else{
                System.out.println("添加失败");
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
        }


    }
    //修改密码
    public static void updatePwd(bank.User u) {
        System.out.println("请录入您的原密码:");
        String oldPwd = sc.next();
        boolean flag = new Bankmanagement().userlogin(u.getName(),oldPwd, u.getId());
        while (flag) {          //知识点20:equals方法
            System.out.println("您的原密码输入正确,请录入您要修改的新密码:");
            String newPwd = sc.next();
            Connection conn = null;
            PreparedStatement pstmt = null;

            try{
                conn = JDBCUtils.getConnection();
                //定义sql语句
                String sql="update user set password=? where username=?";
                pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,newPwd);
                pstmt.setString(2,u.getName());

//            pstmt.executeUpdate();
                //获取执行对象
//            stmt = conn.createStatement();
                //执行查询
//            rs = stmt.executeQuery(sql);
                //判断
                int count = pstmt.executeUpdate();
                if (count>0){
                    System.out.println("修改成功");
                    login1();
                }else{
                    System.out.println("修改失败");
                    return;
                }
            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                JDBCUtils.close(pstmt,conn);
            }

        }
            System.out.println("您的原密码输入错误,密码修改失败");

    }

    //当前账户个人信息查询
    public static void updateOne (bank.User u){
//        System.out.println("查询信息如下:");
//        System.out.println("用户名       手机号        密码          住址         余额");
//        System.out.println(u.getName() + "        " + u.getNumber() + "        " + u.getPassword() + "        " + u.getAddress()+ "        " + u.getBalance());
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

//        User user = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "select * from user where id = '"+u.getId()+"'";
//            pstmt.setInt(1,u.getId());
            //获取执行对象
            pstmt = conn.prepareStatement(sql);
            //执行查询
            rs = pstmt.executeQuery();
            while(rs.next()) {
                //获取数据
                String name = rs.getString("username");
                int id = rs.getInt("id");
                String number = rs.getString("phonenumber");
                String password = rs.getString("password");
                String address = rs.getString("address");
                double balance = rs.getDouble("balance");


                System.out.println("用户名       id      手机号        密码          住址         余额");
                System.out.println(name + "        " + id + "        " + number + "        " + password + "        " + address + "        " + balance);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }

    }

    //所有用户个人信息查询
    public static void checkAllUsers() {

        ArrayList<User> list = new Bankmanagement().findall();
        System.out.println("用户名        手机号          密码           住址          余额");
        for (bank.User u : list) {       //知识点21:遍历

            System.out.println(u.getName() + "      " + u.getNumber() + "        " + u.getPassword() + "       " + u.getAddress()+ "        " +u.getBalance());
        }

    }

    public ArrayList<User> findall(){
        //数据库连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<User> list = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "select * from user";
            //获取执行对象
            pstmt = conn.prepareStatement(sql);
            //执行查询
            rs = pstmt.executeQuery();
            User user = null;
            list = new ArrayList<User>();

            //便利
            while(rs.next()){
                //获取数据
                String name = rs.getString("username");
                int id = rs.getInt("id");
                String number = rs.getString("phonenumber");
                String address = rs.getString("address");
                double balance = rs.getDouble("balance");
                //创建User对象
                user = new User();
                user.setName(name);
                user.setId(id);
                user.setNumber(number);
                user.setAddress(address);
                user.setBalance((float) balance);
                //装载集合
                list.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }
        return list;
    }//存钱
    public static void saveMoney(bank.User u){
        u.setOperate("存钱");
        System.out.println("请输入您要存入的金额:");
        float a = sc.nextFloat();
        u.setBalance2(a);
        Calendar calendar = Calendar.getInstance();    //知识点23:获取时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    //知识点24:更改格式
        u.setTime(formatter.format(calendar.getTime()));

        Connection conn = null;
        PreparedStatement pstmt1 = null;

        try{
            //获取连接
            conn = JDBCUtils.getConnection();
            //定义sql
            String sql1 = "update user set balance = balance + ? where username = ?";
            //-
            pstmt1 = conn.prepareStatement(sql1);
            //+
            pstmt1.setDouble(1,a);
            pstmt1.setString(2,u.getName());
            pstmt1.executeUpdate();
            r_operate(u);
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt1,conn);
        }
        System.out.println("存钱成功");
    }
    //取钱
    public static void drawMoney(bank.User u){
        u.setOperate("取钱");
        System.out.println("请输入您要取的金额:");
        float a = sc.nextFloat();
        u.setBalance2(a);
        Calendar calendar = Calendar.getInstance();    //知识点23:获取时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    //知识点24:更改格式
        u.setTime(formatter.format(calendar.getTime()));
        Connection conn = null;
        PreparedStatement pstmt1 = null;
        try{
            //获取连接
            conn = JDBCUtils.getConnection();
            //定义sql
            String sql1 = "update user set balance = balance - ? where username = ?";
            //-
            pstmt1 = conn.prepareStatement(sql1);
            //+
            pstmt1.setDouble(1,a);
            pstmt1.setString(2,u.getName());
            pstmt1.executeUpdate();
            r_operate(u);
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt1,conn);
        }
        System.out.println("取钱成功");
    }
    //转账
    public static void transferMoney(bank.User u){
        u.setOperate("转账");
        Calendar calendar = Calendar.getInstance();    //知识点23:获取时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");    //知识点24:更改格式

        u.setTime(formatter.format(calendar.getTime()));
        float money;
        System.out.println("请输入您要转账的对方账号:");
        String c = sc.next();
        System.out.println("请输入您要转账的金额:");
        float d = sc.nextFloat();
        u.setBalance2(d);
        boolean flag = new Bankmanagement().judge(c);
        if (flag){
            Connection conn = null;
            PreparedStatement pstmt1 = null;
            PreparedStatement pstmt2 = null;
            try{
                //获取连接
                conn = JDBCUtils.getConnection();
                //定义sql
                String sql1 = "update user set balance = balance - ? where username = ?";
                String sql2 = "update user set balance = balance + ? where username = ?";
                //-
                pstmt1 = conn.prepareStatement(sql1);
                //+
                pstmt2 = conn.prepareStatement(sql2);
                pstmt1.setDouble(1,d);
                pstmt1.setString(2,u.getName());
                pstmt2.setDouble(1,d);
                pstmt2.setString(2,c);

                pstmt1.executeUpdate();
                pstmt2.executeUpdate();

            }catch (SQLException e){
                e.printStackTrace();
            }finally {
                JDBCUtils.close(pstmt1,conn);
                JDBCUtils.close(pstmt2,null);
            }
            System.out.println("转账成功");
            r_operate(u);
            return;
        }else {
            System.out.println("您输入的帐号不存在请重新输入!");
            transferMoney(u);
        }
    }
    public static void r_operate(bank.User u){
        Connection conn = null;
        Statement stmt = null;
        PreparedStatement pstmt = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "insert into record(time,id,username,operate,balance2) values(?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, u.getTime());
            pstmt.setInt(2,u.getId());
            pstmt.setString(3,u.getName());
            pstmt.setString(4,u.getOperate());
            pstmt.setDouble(5,u.getBalance2());
//            pstmt.executeUpdate();
            //获取执行对象
//            stmt = conn.createStatement();
            //执行查询
//            rs = stmt.executeQuery(sql);
            //判断
            pstmt.executeUpdate();

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
        }

    }

    public boolean judge(String username){
        if (username == null){
            return false;
        }
        //数据库连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "select * from user where username = ?";
            //获取执行对象
            pstmt = conn.prepareStatement(sql);
            //给问号赋值
            pstmt.setString(1,username);
            //执行查询
            rs = pstmt.executeQuery();
            //判断
            return rs.next();
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }
        return false;
    }
    //打印清单
    public static void print(bank.User u){
        ArrayList<User> list = new Bankmanagement().findalluser(u);
        System.out.println("信息如下:");
        System.out.println("时间          id            用户名               操作               操作金额");
             //知识点21:遍历
        for (bank.User m : list) {
            System.out.println(m.getTime() + "       " +m.getId() + "          " +m.getName()+"              " +m.getOperate() + "               " + m.getBalance2());
        }
    }
    public ArrayList<User> findalluser(bank.User u){
        //数据库连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<User> list = null;
        try{
            conn = JDBCUtils.getConnection();
            //定义sql语句
            String sql = "select * from record where id = '"+u.getId()+"'";
            //获取执行对象
            pstmt = conn.prepareStatement(sql);
            //执行查询
            rs = pstmt.executeQuery();
            User user = null;
            list = new ArrayList<User>();

            //便利
            while(rs.next()){
                //获取数据
                String time = String.valueOf(rs.getTime("time"));
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String operate = rs.getString("operate");
                double balance2 = rs.getDouble("balance2");

                //创建User对象
                user = new User();
                user.setTime(time);
                user.setId(id);
                user.setName(name);
                user.setOperate(operate);
                user.setBalance2((float) balance2);

                //装载集合
                list.add(user);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pstmt,conn);
        }
        return list;
    }//存钱
    //注销账户
    public static void cancel(bank.User u){
        System.out.println("您是否要注销:  Y/N");
        String a = sc.next();
        //知识点:构造方法
        switch(a){
            case "Y":
                Connection conn = null;
                Statement stmt = null;
                PreparedStatement pstmt = null;
                try{
                    conn = JDBCUtils.getConnection();
                    //定义sql语句
                    String sql = "delete from user where username = ?";
                    pstmt = conn.prepareStatement(sql);
                    pstmt.setString(1,u.getName());
                    //判断
                    int count =pstmt.executeUpdate();
                    if (count>0){
                        System.out.println("注销成功");
                        menu();
                    }else{
                        System.out.println("注销失败,请重试!");
                        return;
                    }
                }catch (SQLException e){
                    e.printStackTrace();
                }finally {
                    JDBCUtils.close(pstmt,conn);
                }
//                }
                break;
            case "N":
                break;
        }
    }
}

User

package bank;


public class User {

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public float getBalance() {
        return balance;
    }

    public void setBalance(float balance) {
        this.balance = balance;
    }

    public String getOperate() {
        return operate;
    }

    public void setOperate(String operate) {
        this.operate = operate;
    }

    public float getBalance2() {
        return balance2;
    }

    public void setBalance2(float balance2) {
        this.balance2 = balance2;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public User(String name, int id, String password) {
        this.name = name;
        this.id = id;
        this.password = password;
    }

    public User() {

    }

    public User(String name, int id, String number, String address, float balance) {
        this.name = name;
        this.id = id;
        this.number = number;
        this.address = address;
        this.balance = balance;
    }

    private String name;     //知识点26:声明变量
    private int id;
    private String number;
    private String password;
    private String address;
    private float balance;
    private String operate;

    private String time;
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", number='" + number + '\'' +
                ", password='" + password + '\'' +
                ", address='" + address + '\'' +
                ", balance=" + balance +
                ", operate='" + operate + '\'' +
                ", balance2=" + balance2 +
                ", time='" + time + '\'' +
                '}';
    }

    private float balance2;

    public User(String name, int id, String number, String password, String address, float balance, String operate, float balance2, String time) {
        this.name = name;
        this.id = id;
        this.number = number;
        this.password = password;
        this.address = address;
        this.balance = balance;
        this.operate = operate;
        this.balance2 = balance2;
        this.time = time;
    }

    public User(String name, int id, float balance, String operate, float balance2, String time) {
        this.name = name;
        this.id = id;
        this.balance = balance;
        this.operate = operate;
        this.balance2 = balance2;
        this.time = time;
    }


}

JDBCUtils

package bank;
import javax.lang.model.element.NestingKind;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Properties;

/**
 * 文件的读取,只需要读取一次即可拿到这些值
 *
 * **/
public class JDBCUtils {
//    private  static String url;
//    private static String user;
//    private static String password;
//    private static String driver;
    static {
        //读取文件资源

        try {
//            //1.创建Properties集合类
//            Properties pro = new Properties();
//            //获取src路径下的文件方式-->ClassLoader
//            ClassLoader classLoader = JDBCUtils.class.getClassLoader();
//            URL res = classLoader.getResource("jdbc.properties");
//            String path = res.getPath();
//            System.out.println(path);
//            //2.加载文件
//            pro.load(new FileReader(path));
//            //3.获取数据,赋值
//            url = pro.getProperty("url");
//            user = pro.getProperty("user");
//            password = pro.getProperty("password");
//            driver = pro.getProperty("driver");
            //4.注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/bank","root","gqdqcc21");
    }

    public static void close(ResultSet rs,Statement stmt, Connection conn){

        if (rs !=null){
            try{
                rs.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (stmt !=null){
            try{
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (conn !=null){
            try{
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement stmt, Connection conn){
        if (stmt !=null){
            try{
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        if (conn !=null){
            try{
                conn.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }
}

你可能感兴趣的:(java,mysql,数据库)