客户信息管理系统--数据库实训项目

package com.客户信息管理系统;

/**
 * @author yu
 * @date 2022/1/6 - 22:22
 */
public class Main {
    public static void main(String[] args) {
        main();
    }

    private static void main()  {
        try {
            //初始化数据库
            Mysql mysql = new Mysql();
            //连接数据
            mysql.ConnectMysql();
           while (true){
               System.out.println("----客户信息管理系统---");
               System.out.println("\t1 添 加 客 户");
               System.out.println("\t2 修 改 客 户");
               System.out.println("\t3 删 除 客 户");
               System.out.println("\t4 客 户 列 表");
               System.out.println("\t5 退       出");
               System.out.println();
               System.out.print("\t请选择(1-5):");
               switch (Tools.getMenu()) {
                   case 1:
                       Menu.addCustomer();
                       break;
                   case 2:
                       Menu.modifyCustomer();
                       break;
                   case 4:
                       Menu.queryCustomerList();
                       break;
                   case 3:
                       Menu.delCustomer();
                       break;
                   case 5:
                       mysql.CloseMysql();
                       System.out.println("掰掰!!!");
                       return;
                   default:
                       break;
               }
           }
        }catch (Exception e){
            System.out.println(e);
        }


    }
}

// ----------------------------------------
package com.客户信息管理系统;


import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * @author yu
 * @date 2022/1/6 - 22:25
 */

public class Menu {
    private static Mysql mysql;
    private static  Customer customer = new Customer();
    /*
    修改客户信息
     */
    public static void modifyCustomer() throws Exception {
        System.out.println("-------修 改 客 户--------");
        System.out.print("请选择待修改客户编号(-1退出):");
        int id = Tools.getNum();
        if(id == -1){
            System.out.println("\t取消刪除!");
            return;
        }
        Mysql mysql = new Mysql();
        ResultSet rs = mysql.getResultSetNum(id);
        int num = 0;
        while (rs.next()){
            num++;
            customer.setId(id);
            System.out.print("姓名("+rs.getString("name")+"):");
            customer.setName(Tools.getStr(rs.getString("name")));

            System.out.print("性别("+rs.getString("gender")+"):");
            customer.setGender(Tools.getStr(rs.getString("gender")));

            System.out.print("年龄("+rs.getInt("age")+"):");
            customer.setAge(Tools.getInt(rs.getInt("age")));

            System.out.print("电话("+rs.getString("phone")+"):");
            customer.setPhone(Tools.getStr(rs.getString("phone")));

            System.out.print("邮箱("+rs.getString("email")+"):");
            customer.setEmail(Tools.getStr(rs.getString("email")));

            int num1 = 0;
            num1 = mysql.modifyCustomer(customer);
            if(num1 == 1){
                System.out.println("\t修 改 成 功!");
                return;
            }
        }
        if(num == 0){
            System.out.println();
            System.out.println("\t错误!该编号不存在!");
        }
        System.out.println();
        System.out.println("-------修 改 完 成--------");
        System.out.println();
    }

    /*
    添 加 客 户
     */
    public static void addCustomer() throws Exception {
        System.out.println("--------添 加 客 户--------");
        System.out.print("姓名:");
        customer.setName(Tools.getStr());
        System.out.print("性别:");
        customer.setGender(Tools.getGdenderStr());
        System.out.print("年龄:");
        customer.setAge(Tools.getNum());
        System.out.print("电话:");
        customer.setPhone(Tools.getStr());
        System.out.print("邮箱:");
        customer.setEmail(Tools.getStr());
        Mysql mysql = new Mysql();
        int num03 = mysql.addCustomer(customer);
        if(num03 == 1){
            System.out.println("\t添 加 成 功!");
        }else
            System.out.println("\t添 加 失 败");

        System.out.println("--------添 加 完 成--------");
    }

    /*
    删 除 客 户
     */
    public static  void delCustomer() throws SQLException {
        System.out.println("------刪 除 客 戶------");
        System.out.print("请选择待删除客户编号(-1退出):");
        int id = Tools.getNum();
        if(id == -1){
            System.out.println("取消刪除!");
            return;
        }
        System.out.println("确认是否删除(Y/N):");
        if(Tools.getYNstr().equals("N")) {
            System.out.println("取消刪除!");
            return;
        }
        Mysql mysql = new Mysql();
        int num01 = mysql.delCustomer(id);
        if(num01 == 1){
            System.out.println("\t删除成功!");
        }else
            System.out.println("\t删除失败!数据不存在!");
        System.out.println("-------刪 除 完 成-----");
        System.out.println();
    }
    /*
    客 户 列 表
     */
    public static void queryCustomerList()throws Exception{
        System.out.println("---------------------查询客户信息----------------------");
        Mysql mysql = new Mysql();
        ResultSet rs = mysql.getResultSet();
        System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t邮箱");
        int num01 = 0;
        // 展开结果集数据库
        while(rs.next()){
            num01++;
            // 输出数据
            System.out.print(rs.getInt("id")+"\t\t");
            System.out.print(rs.getString("name")+"\t");
            System.out.print(rs.getString("gender")+"\t\t");
            System.out.print(rs.getInt("age")+"\t\t");
            System.out.print(rs.getString("phone")+"\t\t");
            System.out.print(rs.getString("email"));
            System.out.println();
        }
        if(num01 == 0){
            System.out.println("查询失败!没有数据!");
        }
        // 完成后关闭
        rs.close();
        System.out.println("----------------------查询完成--------------------------");
        System.out.println();
    }



}
// -------------------------------------------------------
package com.客户信息管理系统;

/**
 * @author yu
 * @date 2022/1/6 - 21:27
 */
public class Customer {

    private int id;
    private String name;
    private String gender;
    private int age;
    private String phone;
    private String email;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
//----------------------------------------
package com.客户信息管理系统;

/**
 * @author yu
 * @date 2022/1/6 - 22:07
 */


import java.sql.*;

public class Mysql {
    final String DB_URL = "jdbc:mysql://localhost:3306/cms_hisoft?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
    final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";

    // 数据库的用户名与密码,需要根据自己的设置
    final String USER = "root";
    final String PASS = "root";
    static Statement stmt = null;
    static Connection conn = null;

    /*
    修改客户信息
     */
    public int modifyCustomer(Customer customer) throws Exception {
        String sql = "update users set name = '" + customer.getName() + "',gender = '" + customer.getGender()
                + "',age = " + customer.getAge() + ",phone = '" + customer.getPhone() + "',email = '"
                + customer.getEmail() + "' where id = " + customer.getId();
        return stmt.executeUpdate(sql);
    }

    /*
    添 加 客 户
     */
    public int addCustomer(Customer customer) throws Exception {
        String sql = "insert into users (name, gender, age, phone, email) " +
                "value ('" + customer.getName() + "','" + customer.getGender() + "'," +
                "" + customer.getAge() + ",'" + customer.getPhone() + "','" + customer.getEmail() + "') ";
        return stmt.executeUpdate(sql);
    }

    /*
    连 接 数 据 库
     */
    public void ConnectMysql() throws Exception {

        // 注册 JDBC 驱动
        Class.forName(JDBC_DRIVER);

        // 打开链接
        System.out.println("连接数据库...");
        conn = DriverManager.getConnection(DB_URL, USER, PASS);

        // 执行查询
        System.out.println(" 实例化Statement对象...");
        stmt = conn.createStatement();
        System.out.println("Goodbye!");

    }

    /*
    关闭数据库
     */
    public void CloseMysql() throws Exception {
        if (stmt != null)
            stmt.close();
        if (conn != null)
            conn.close();
    }

    /*
    获取客户信息
     */
    public ResultSet getResultSet() throws Exception {
        String sql = "select * from users";
        ResultSet rs = stmt.executeQuery(sql);
        return rs;

    }

    /*
    获取指定客户信息 修改客户
     */
    public ResultSet getResultSetNum(int id) throws Exception {
        String sql = "select * from users where id = " + id;
        ResultSet rs = stmt.executeQuery(sql);
        return rs;
    }

    /*
    删除客户
     */
    public int delCustomer(int id) throws SQLException {
        String sql = "delete from users where id = " + id;
        return stmt.executeUpdate(sql);
    }

}
//------------------------------------------
package com.客户信息管理系统;


import java.util.Scanner;

/**
 * @author yu
 * @date 2022/1/6 - 22:23
 */
public class Tools {
    private static Scanner sc = new Scanner(System.in);

    /*
    获取整数 可以为空
     */
    public static int getInt(int num) {
        int num1;
        String str;
        while (true){
            str = Tools.getStr("");
            if(str.length() == 0){
                return num;
            }
            try {
                num1 = Integer.parseInt(str);
                return num1;
            }catch (Exception e){
                System.out.print("输入错误!请输入数字:");
            }


        }



    }

    /*
    获取字符 可以为空
     */
    public static String getStr(String str) {
        String str1;
        str1 = sc.nextLine();
        if (str1.length() == 0) {
            return str;
        } else {
            return str1;
        }


    }

    /*
    获取男/女字符
     */
    public static String getGdenderStr() {
        String str4;
        while (true) {
            str4 = Tools.getStr();
            if (str4.equals("男") || str4.equals("女")) {
                return str4;
            } else {
                System.out.print("输入错误!请重新输入(男/女):");
            }
        }

    }

    /*
    获取Y/N字符
     */
    public static String getYNstr() {
        String str3;
        while (true) {
            str3 = Tools.getStr();
            if (str3.equals("Y") || str3.equals("N")) {
                return str3;
            } else {
                System.out.print("输入错误!请重新输入(Y/N):");
            }
        }
    }

    /*
    获取非空字符
     */
    public static String getStr() {
        String str2;
        while (true) {
            str2 = sc.nextLine();
            if (str2.length() == 0) {
                System.out.print("输入内容不能为空!请重新输入:");
                continue;
            }
            return str2;
        }

    }

    /*
    获取数字范围1-5
     */
    public static int getMenu() {

        int num1;
        while (true) {
            num1 = Tools.getNum();
            if (num1 > 0 && num1 < 6) {
                return num1;
            }
            System.out.print("请输入数字(1-5):");

        }
    }

    /*
    获取非空数字
     */
    public static int getNum() {
        String str2;
        int num2;
        while (true) {
            try {
                str2 = sc.nextLine();
                if (str2.length() == 0) {
                    System.out.print("错误!输入内容不能为空:");
                    continue;
                }
                num2 = Integer.parseInt(str2);
                return num2;
            } catch (Exception e) {
                System.out.print("错误!请输入数字:");
            }
        }
    }
}




 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值