当前位置:   article > 正文

基于 JavaSE + MySQL+ JDBC 的学生信息管理系统(简易版)_java和mysql写it管理系统

java和mysql写it管理系统


一、技术栈

  • javaSE
  • MySQL
  • JDBC

二、功能分析

2.1 登录菜单

  • 登录功能
  • 注册功能
  • 找回密码
  • 关于我的
  • 关闭系统

2.2 主菜单

  • 添加学生信息
  • 删除学生信息
    • 根据学号来删除指定的学生信息
  • 修改学生信息(指定字段)
    • 根据学号来修改指定的学生信息
  • 修改学生信息(所有信息)
    • 根据学号来修改指定的学生的所有信息
  • 查找学生信息
    • 根据学号来查询指定的学生信息
  • 显示学生信息(全体学生)
  • 清空学生信息
  • 导出学生信息
    • 有两种导出方式,一种是自定义导出,另一种是默认导出
  • 注销账户
    • 识别当前管理者信息,只能注销当前登录的管理者信息,注销成功后退出登录状态,返回到未登录界面
  • 退出登录
    • 退出主菜单,返回登录菜单

三、数据库设计

3.1 学生表(student)设计

字段名说明类型主键外键非空唯一自增
id学生编号int(10)
student_id学号varchar(11)
student_name姓名varchar(20)
student_sex性别varchar(4)
student_age年龄int(5)
student_phone电话varchar(15)
student_location住址varchar(45)
student_card身份证号码varchar(18)
student_english英语成绩double
student_mathmath成绩double
student_javajava成绩double

3.2管理员表(admin)设计

字段名说明类型主键外键非空唯一自增
id管理员编号int(10)
admin_id管理员账号varchar(11)
admin_password管理员密码varchar(32)
admin_name管理员姓名varchar(20)
admin_card管理员身份证号varchar(18)
admin_phone管理员手机号varchar(15)

四、涉及到的类和接口

4.1 接口示例:

4.1.1 AdminDAO接口:用来规范 针对于 admin 表的一些常用操作
package com.student.dao;

import com.student.bean.Admin;

import java.sql.Connection;

/**
 * 此接口是用来规范 针对于 admin 表的一些常用操作
 */
public interface AdminDAO {

    /**
     * @author wk
     * @Description 注册功能(向数据库插入管理者信息)
     * @Date 15:34 2022/3/3
     * @Param
     * @Return
     */

    int register(Connection connection, Admin admin);

    /**
     * @author wk
     * @Description 登录功能 (验证密码和账号)
     * @Date 15:36 2022/3/3
     * @Param
     * @Return
     */

    Admin login(Connection connection, String adminId, String password);


    /**
     * @author wk
     * @Description 找回密码
     * @Date 19:36 2022/3/3
     * @Param
     * @Return
     */

    String recover(Connection connection, String card, String phone);

    /**
     * @author wk
     * @Description 注销账户
     * @Date 21:00 2022/3/4
     * @Param
     * @Return
     */

    int unsubscribe(Connection connection, String card, String phone);

    /**
     * 通过账号id,检查管理员是否存在
     *
     * @param connection 连接
     * @param adminId    管理员id
     * @return boolean
     */
    boolean checkAdminIsExistById(Connection connection, String adminId);

    /**
     * 通过身份证号,检查管理员是否存在
     *
     * @param connection 连接
     * @param card       身份证号
     * @return boolean
     */
    boolean checkAdminIsExistByCard(Connection connection, String card);

    /**
     * 通过手机号,检查管理员是否存在
     *
     * @param connection 连接
     * @param phone      电话
     * @return boolean
     */
    boolean checkAdminIsExistByPhone(Connection connection, String phone);

    /**
     * @author wk
     * @Description 根据管理员身份证号,获取管理员的信息
     * @Date 20:39 2022/3/4
     * @Param
     * @Return
     */

    Admin getAdminByCard(Connection connection, String card);

    /**
     * @author wk
     * @Description 根据管理员手机号,获取管理员的信息
     * @Date 20:39 2022/3/4
     * @Param
     * @Return
     */

    Admin getAdminByPhone(Connection connection, String phone);

}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
4.1.2 StudentDAO:用于规范针对于student表的常用操作
package com.student.dao;

import com.student.bean.Student;

import java.sql.Connection;
import java.util.List;

/**
 * 此接口用于规范针对于student表的常用操作
 */

public interface StudentDAO {

    /**
     * @author wk
     * @Description 将 student 对象添加到数据库中
     * @Date 21:12 2022/3/2
     * @Param
     * @Return
     */

    int insert(Connection connection, Student student);

    /**
     * @author wk
     * @Description 根据学生的学号,删除学生信息
     * @Date 22:01 2022/3/2
     * @Param
     * @Return
     */

    int delete(Connection connection, String studentId);

    /**
     * @author wk
     * @Description 根据学号和字段修改指定学生的指定字段信息
     * @Date 10:22 2022/3/3
     * @Param
     * @Return
     */

    int update(Connection connection, String studentId, String key, Object value);

    /**
     * @author wk
     * @Description 针对内存中的 Student 对象,去修改数据库中指定的学生的全部数据
     * @Date 22:03 2022/3/2
     * @Param
     * @Return
     */

    int updateAll(Connection connection, String oldStudentId, Student student);

    /**
     * 通过账号id,检查学生是否存在
     *
     * @param connection 连接
     * @param studentId  学生id
     * @return boolean
     */
    boolean checkStudentIsExistById(Connection connection, String studentId);

    /**
     * 通过身份证号,检查学生是否存在
     *
     * @param connection 连接
     * @param card       身份证号
     * @return boolean
     */
    boolean checkStudentIsExistByCard(Connection connection, String card);

    /**
     * 通过手机号,检查学生是否存在
     *
     * @param connection 连接
     * @param phone      电话
     * @return boolean
     */
    boolean checkStudentIsExistByPhone(Connection connection, String phone);


    /**
     * @author wk
     * @Description 根据学生学号,查询学生信息
     * @Date 22:04 2022/3/2
     * @Param
     * @Return
     */

    Student getStudentByStudentId(Connection connection, String studentId);

    /**
     * @author wk
     * @Description 根据手机号查询学生信息
     * @Date 20:57 2022/3/3
     * @Param
     * @Return
     */

    Student getStudentByPhone(Connection connection, String phone);

    /**
     * @author wk
     * @Description 根据身份证号查询学生信息
     * @Date 20:58 2022/3/3
     * @Param
     * @Return
     */

    Student getStudentByCard(Connection connection, String card);


    /**
     * @author wk
     * @Description 查询表中所有记录构成的集合
     * @Date 22:05 2022/3/2
     * @Param
     * @Return
     */

    List<Student> getStudentAll(Connection connection);


    /**
     * @author wk
     * @Description 查询数据库中 Student 数据总数目
     * @Date 22:06 2022/3/2
     * @Param
     * @Return
     */

    Long getCount(Connection connection);

    /**
     * @author wk
     * @Description 清空所有学生信息
     * @Date 20:06 2022/3/4
     * @Param
     * @Return
     */

    int clearAll(Connection connection);

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145

4.2 类的示例:

4.2.1 Student类:对应数据库中的student表
package com.student.bean;

import com.student.tools.Tools;

import javax.tools.Tool;
import java.math.BigDecimal;

/**
 * @ClassName Student
 * @Description 学生信息
 * @Author wk
 * @Date 2022/3/2 9:14
 * @Version 1.0
 */
public class Student {
    private String studentId;
    private String name;
    private String sex;
    private int age;
    private String phone;
    private String location;
    private String card;
    private double english;
    private double math;
    private double java;

    public Student() {
    }

    public Student(String studentId, String name, String sex, int age, String phone, String location, String card, double english, double math, double java) {
        this.studentId = studentId;
        this.name = name;
        this.sex = sex;
        this.age = age;
        this.phone = phone;
        this.location = location;
        this.card = card;
        this.english = english;
        this.math = math;
        this.java = java;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    public String getName() {
        return name;
    }

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

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    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 getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getCard() {
        return card;
    }

    public void setCard(String card) {
        this.card = card;
    }

    public double getEnglish() {
        return english;
    }

    public void setEnglish(double english) {
        this.english = english;
    }

    public double getMath() {
        return math;
    }

    public void setMath(double math) {
        this.math = math;
    }

    public double getJava() {
        return java;
    }

    public void setJava(double java) {
        this.java = java;
    }

    @Override
    public String toString() {
        return studentId + '\t' + name + "\t\t" + sex + "\t\t" + age + "\t\t" + Tools.alignment(phone) + Tools.alignment(location) + Tools.alignment(card) + english + "\t\t" + math + "\t\t" + java;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
4.2.2 Admin类:对应数据库中的admin表
package com.student.bean;

/**
 * @ClassName Admin
 * @Description 管理员信息
 * @Author wk
 * @Date 2022/3/2 21:03
 * @Version 1.0
 */
public class Admin {
    private String adminId;
    private String password;
    private String name;
    private String card;
    private String phone;

    public Admin() {
    }

    public Admin(String adminId, String password, String name, String card, String phone) {
        this.adminId = adminId;
        this.password = password;
        this.name = name;
        this.card = card;
        this.phone = phone;
    }

    public String getAdminId() {
        return adminId;
    }

    public void setAdminId(String adminId) {
        this.adminId = adminId;
    }

    public String getPassword() {
        return password;
    }

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

    public String getName() {
        return name;
    }

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

    public String getCard() {
        return card;
    }

    public void setCard(String card) {
        this.card = card;
    }

    public String getPhone() {
        return phone;
    }

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

    @Override
    public String toString() {
        return "Admin{" +
                "adminId='" + adminId + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                ", card='" + card + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
4.2.3 Tools类:用于用户进行输入的工具类
package com.student.tools;

import org.junit.jupiter.api.Test;

import java.util.Scanner;

/**
 * @ClassName Tools
 * @Description 提供了 Tools.java类,可用来方便的实现键盘访问
 * @Author wk
 * @Date 2022/3/3 16:31
 * @Version 1.0
 */
public class Tools {

    private static Scanner input = new Scanner(System.in);

    /**
     * @author wk
     * @Description 该方法读取键盘,如果用户键入 ‘1’-‘5’中的任意字符,则方法返回,返回值为用户键入的字符
     * @Date 21:13 2022/3/3
     * @Param
     * @Return
     */

    public static char readRegisterMenuSelection() {
        char c;
        while (true) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5') {
                System.out.println("选择错误,请重新输入:");
            } else break;
        }
        return c;
    }


    /**
     * @author wk
     * @Description 该方法读取键盘,如果用户键入 ‘0’-‘9’中的任意字符,则方法返回,返回值为用户键入的字符
     * @Date 22:14 2021/11/22
     * @Param
     * @Return
     */

    public static char readMainMenuSelection() {
        char c;
        while (true) {
            String str = readKeyBoard(1, false);
            c = str.charAt(0);
            if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9') {
                System.out.println("选择错误,请重写输入:");
            } else break;
        }
        return c;
    }

    /**
     * @author wk
     * @Description 该方法提示并等待,直到用户按回车键后返回
     * @Date 22:17 2021/11/22
     * @Param
     * @Return
     */

    public static void readReturn() {
        System.out.println("按回车键继续....");
        readKeyBoard(100, true);
    }

    /**
     * @author wk
     * @Description 该方法从键盘读取一个长度不超过 2 位的整数,并将其作为方法的返回值
     * @Date 22:21 2021/11/22
     * @Param
     * @Return
     */

    public static int readInt() {
        int n;
        while (true) {
            String str = readKeyBoard(2, false);
            try {
                n = Integer.parseInt(str);
                break;
            } catch (NumberFormatException e) {
                System.out.println("数字输入错误,请重新输入:");
            }
        }
        return n;
    }

    /**
     * @author wk
     * @Description 该方法从键盘读取一个长度不超过 18 位的字符串,并将其作为方法的返回值
     * @Date 23:56 2022/3/3
     * @Param
     * @Return
     */

    public static String readString() {
        String str = readKeyBoard(18, false);
        return str;
    }

    /**
     * @author wk
     * @Description 对齐输出的学生信息(由 com.student.bean.student 来调用),默认指定字段长度为18
     * @Date 10:41 2022/3/8
     * @Param
     * @Return
     */
    public static String alignment(String str){
        int length = 18;
        if(str.length() < 18){
            while(str.length() < length){
                str += " ";
            }
        }
        return str;
    }

    /**
     * @author wk
     * @Description 从键盘读取‘Y’ 或 ‘N’,并将其作为方法的返回值
     * @Date 22:18 2021/11/22
     * @Param
     * @Return
     */
    public static char readConfirmSelection() {
        char c;
        while (true) {
            String str = readKeyBoard(1, false).toUpperCase();
            c = str.charAt(0);
            if (c == 'Y' || c == 'N') {
                break;
            } else {
                System.out.println("选择错误,请重写输入:");
            }
        }
        return c;
    }

    /**
     * @author wk
     * @Description 从键盘获取数据
     * @Date 22:24 2021/11/22
     * @Param
     * @Return
     */

    private static String readKeyBoard(int limit, boolean blankReturn) {
        String line = "";
        while (input.hasNextLine()) {
            line = input.nextLine();
            if (line.length() == 0) {
                if (blankReturn) {
                    return line;
                } else {
                    continue;
                }
            }
            if (line.length() < 1 || line.length() > limit) {
                System.out.println("输入长度(不大于" + limit + ")错误,请重新输入:");
                continue;
            }
            break;
        }
        return line;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
4.2.4 JDBCUtiles类:封装的数据库连接通用操作
package com.student.dao.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @ClassName JDBCUtils
 * @Description TODO
 * @Author wk
 * @Date 2022/2/24 20:31
 * @Version 1.0
 */
public class JDBCUtils {

    /**
    * @author wk
    * @Description 使用Druid数据库连接池技术
    * @Date 20:38 2022/3/1
    * @Param
    * @Return
    */
    private static DataSource source1;
    static{
        try {
            Properties properties = new Properties();
            InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("student.properties");

            // 加载配置文件
            properties.load(resourceAsStream);

            source1 = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        Connection connection = source1.getConnection();
        return connection;
    }


    /**
     * @author wk
     * @Description 获取数据库的连接
     * @Date 20:40 2022/2/24
     * @Param
     * @Return
     */

    public static Connection getConnection1() throws Exception {
        // 获取数据库连接

        // 1. 读取配置文件中的四个基本信息
        InputStream resourceAsStream = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);

        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driverClass = properties.getProperty("driverClass");

        // 2. 加载驱动
        Class.forName(driverClass);

        // 3. 获取连接
        Connection connection = DriverManager.getConnection(url, user, password);

        return connection;
    }

    /**
     * @author wk
     * @Description 关闭连接 和 Statement的操作
     * @Date 20:39 2022/2/24
     * @Param
     * @Return
     */
    public static void closeResource(Connection connection, Statement ps, ResultSet resultSet) {
        // 7.关闭资源
        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(resultSet);
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
4.2.5 BaseDAO类:封装了针对于数据表的通用的操作
package com.student.dao;

import com.student.bean.Student;
import com.student.dao.util.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import javax.management.Query;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName BaseDAO
 * @Description 封装了针对于数据表的通用的操作
 * @Author wk
 * @Date 2022/3/2 21:06
 * @Version 1.0
 */
public abstract class BaseDAO<T> {

    private Class<T> clazz = null;

    // 获取当前BaseDAO的子类继承的父类的泛型
    {
        // 获取当前BaseDAO的子类继承的父类中的泛型
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;

        Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();// 获取了父类的泛型数组
        clazz = (Class<T>) actualTypeArguments[0];// 泛型的第一个参数
    }

    /**
     * @author wk
     * @Description 通用的增删改操作
     * @Date 21:34 2022/3/2
     * @Param
     * @Return
     */
    public int update(Connection connection, String sql, Object... args) {
        try {
            QueryRunner queryRunner = new QueryRunner();
            int update = queryRunner.update(connection, sql, args);
            return update;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 通用的查询操作,返回一条数据
     * @Date 21:41 2022/3/2
     * @Param
     * @Return
     */
    public T getInstance(Connection connection, String sql, Object... args) {
        try {
            QueryRunner queryRunner = new QueryRunner();
            BeanHandler<T> studentBeanHandler = new BeanHandler<>(clazz);
            T t = queryRunner.query(connection, sql, studentBeanHandler, args);
            return t;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 通用的查询操作,返回多条数据
     * @Date 21:48 2022/3/2
     * @Param
     * @Return
     */

    public List<T> getForList(Connection connection, String sql, Object... args) {
        try {
            QueryRunner queryRunner = new QueryRunner();
            BeanListHandler<T> studentBeanListHandler = new BeanListHandler<T>(clazz);
            List<T> list = queryRunner.query(connection, sql, studentBeanListHandler, args);
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }


    /**
     * @author wk
     * @Description 通用的查询特殊值的方法
     * @Date 21:53 2022/3/2
     * @Param
     * @Return
     */

    public <E> E getValue(Connection connection, String sql, Object... args) {
        try {
            QueryRunner queryRunner = new QueryRunner();
            ScalarHandler scalarHandler = new ScalarHandler();
            Object value = queryRunner.query(connection, sql, scalarHandler, args);
            return (E) value;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
4.2.6 AdminDAO接口的实现类:
package com.student.dao;

import com.student.bean.Admin;

import java.sql.Connection;

/**
 * @ClassName AdminDAOImpl
 * @Description AdminDAOImpl接口的实现类
 * @Author wk
 * @Date 2022/3/3 15:38
 * @Version 1.0
 */
public class AdminDAOImpl extends BaseDAO<Admin> implements AdminDAO {

    @Override
    public int register(Connection connection, Admin admin) {
        String sql = "insert into admin(admin_id,admin_password,admin_name,admin_card,admin_phone) values(?,?,?,?,?)";
        return this.update(connection, sql, admin.getAdminId(), admin.getPassword(), admin.getName(), admin.getCard(), admin.getPhone());
    }

    @Override
    public Admin login(Connection connection, String adminId, String password) {
        String sql = "select admin_id adminId,admin_password password,admin_name name,admin_card card,admin_phone phone from admin where admin_id = ? and admin_password = ?";
        return this.getInstance(connection, sql, adminId, password);
    }

    @Override
    public String recover(Connection connection, String card, String phone) {
        String sql = "select admin_password password from admin where admin_card = ? and admin_phone = ?";
        return this.getValue(connection, sql, card, phone);
    }

    @Override
    public int unsubscribe(Connection connection, String card, String phone) {
        String sql = "delete from admin where admin_card = ? and admin_phone = ?";
        return this.update(connection, sql, card, phone);
    }

    @Override
    public boolean checkAdminIsExistById(Connection connection, String adminId) {
        String sql = "select count(*) from admin where admin_id = ?";
        long count = this.getValue(connection, sql, adminId);
        return count > 0;
    }

    @Override
    public boolean checkAdminIsExistByCard(Connection connection, String card) {
        String sql = "select count(*) from admin where admin_card = ?";
        long count = this.getValue(connection, sql, card);
        return count > 0;
    }

    @Override
    public boolean checkAdminIsExistByPhone(Connection connection, String phone) {
        String sql = "select count(*) from admin where admin_phone = ?";
        long count = this.getValue(connection, sql, phone);
        return count > 0;
    }

    @Override
    public Admin getAdminByCard(Connection connection, String card) {
        String sql = "select admin_id adminId,admin_password password,admin_name name,admin_card card,admin_phone phone from admin where admin_card = ?";
        return this.getInstance(connection, sql, card);
    }

    @Override
    public Admin getAdminByPhone(Connection connection, String phone) {
        String sql = "select admin_id adminId,admin_password password,admin_name name,admin_card card,admin_phone phone from admin where admin_phone = ?";
        return this.getInstance(connection, sql, phone);
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
4.2.7 StudentDAO接口的实现类:
package com.student.dao;

import com.student.bean.Student;

import java.sql.Connection;
import java.util.List;

/**
 * @ClassName StudentDAOImpl
 * @Description StudentDAO接口的实现类
 * @Author wk
 * @Date 2022/3/2 22:09
 * @Version 1.0
 */
public class StudentDAOImpl extends BaseDAO<Student> implements StudentDAO {

    /**
     * @author wk
     * @Description 将 student 对象添加到数据库中
     * @Date 10:09 2022/3/3
     * @Param
     * @Return
     */
    @Override
    public int insert(Connection connection, com.student.bean.Student student) {
        String sql = "insert into student(student_id,student_name,student_sex,student_age,student_phone," +
                "student_location,student_card,student_english,student_math,student_java) values(?,?,?,?,?,?,?,?,?,?)";
        return this.update(connection, sql, student.getStudentId(), student.getName(), student.getSex(),
                student.getAge(), student.getPhone(), student.getLocation(),
                student.getCard(), student.getEnglish(), student.getMath(), student.getJava());
    }

    /**
     * @author wk
     * @Description 根据学生的学号,删除学生信息
     * @Date 10:10 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public int delete(Connection connection, String studentId) {
        String sql = "delete from student where student_id = ?";
        return this.update(connection, sql, studentId);
    }

    /**
     * @author wk
     * @Description 根据学号和字段修改指定学生的指定字段信息
     * @Date 10:22 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public int update(Connection connection, String studentId, String key, Object value) {
        String sql = "update student set " + key + "= ? where student_id = ?";
        return this.update(connection, sql, value, studentId);
    }

    /**
     * @author wk
     * @Description 针对内存中的 Student 对象,去修改数据库中指定的学生的全部数据
     * @Date 10:10 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public int updateAll(Connection connection, String oldStudentId, Student student) {
        String sql = "update student set student_id = ?,student_name = ?,student_sex = ?," +
                "student_age = ?,student_phone = ?,student_location = ?," +
                "student_card = ?,student_english = ?,student_math = ?,student_java = ? where student_id = ?";
        return this.update(connection, sql, student.getStudentId(), student.getName(), student.getSex(), student.getAge(), student.getPhone(), student.getLocation(), student.getCard(),
                student.getEnglish(), student.getMath(), student.getJava(), oldStudentId);
    }

    /**
     * 通过账号id,检查学生是否存
     *
     * @param connection 连接
     * @param studentId  学生证
     * @return boolean
     */
    @Override
    public boolean checkStudentIsExistById(Connection connection, String studentId) {
        String sql = "select count(*) from student where student_id = ?";
        long count = this.getValue(connection, sql, studentId);
        return count > 0;
    }

    /**
     * 通过身份证号,检查学生是否存
     *
     * @param connection 连接
     * @param card       身份证号
     * @return boolean
     */
    @Override
    public boolean checkStudentIsExistByCard(Connection connection, String card) {
        String sql = "select count(*) from student where student_card = ?";
        long count = this.getValue(connection, sql, card);
        return count > 0;
    }

    /**
     * 通过手机号,检查学生是否存在
     *
     * @param connection 连接
     * @param phone      手机号
     * @return boolean
     */
    @Override
    public boolean checkStudentIsExistByPhone(Connection connection, String phone) {
        String sql = "select count(*) from student where student_phone = ?";
        long count = this.getValue(connection, sql, phone);
        return count > 0;
    }

    /**
     * @author wk
     * @Description 根据学生学号,查询学生信息
     * @Date 10:11 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public com.student.bean.Student getStudentByStudentId(Connection connection, String studentId) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student where student_id = ?";
        return this.getInstance(connection, sql, studentId);
    }

    /**
    * @author wk
    * @Description 通过手机号查询学生信息
    * @Date 16:58 2022/3/8
    * @Param
    * @Return
    */

    @Override
    public Student getStudentByPhone(Connection connection, String phone) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student where student_phone = ?";
        return this.getInstance(connection, sql, phone);
    }

    /**
    * @author wk
    * @Description 通过身份证号查询学生信息
    * @Date 16:59 2022/3/8
    * @Param
    * @Return
    */

    @Override
    public Student getStudentByCard(Connection connection, String card) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student where student_card = ?";
        return this.getInstance(connection, sql, card);
    }

    /**
     * @author wk
     * @Description 查询表中所有记录构成的集合
     * @Date 10:11 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public List<com.student.bean.Student> getStudentAll(Connection connection) {
        String sql = "select student_id studentId,student_name name,student_age age,student_sex sex,student_phone phone,student_card card,student_location location," +
                "student_english english,student_math math,student_java java from student";
        return this.getForList(connection, sql);
    }

    /**
     * @author wk
     * @Description 查询数据库中 Student 数据总数目
     * @Date 10:11 2022/3/3
     * @Param
     * @Return
     */

    @Override
    public Long getCount(Connection connection) {
        String sql = "select count(*) from student";
        return this.getValue(connection, sql);
    }

    /**
    * @author wk
    * @Description 清空所有学生信息
    * @Date 16:58 2022/3/8
    * @Param
    * @Return
    */

    @Override
    public int clearAll(Connection connection) {
        String sql = "delete from student";
        return this.update(connection, sql);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
4.2.8 接收用户传递的参数和数据库中的admin表进行交互:
package com.student.service;

import com.student.bean.Admin;
import com.student.bean.Student;
import com.student.dao.AdminDAOImpl;
import com.student.dao.util.JDBCUtils;

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

/**
 * @ClassName AdminService
 * @Description TODO
 * @Author wk
 * @Date 2022/3/3 17:00
 * @Version 1.0
 */
public class AdminService {
   private AdminDAOImpl adminDAO = new AdminDAOImpl();

    /**
     * @author wk
     * @Description 注册功能
     * @Date 17:01 2022/3/3
     * @Param
     * @Return
     */

    public int register(Admin admin) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.register(connection, admin);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 登录功能
     * @Date 17:04 2022/3/3
     * @Param
     * @Return
     */

    public Admin login(String adminId, String password) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.login(connection, adminId, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }


    /**
     * @author wk
     * @Description 找回密码
     * @Date 19:49 2022/3/3
     * @Param
     * @Return
     */

    public String recover(String card, String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.recover(connection, card, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 注销账户
     * @Date 21:03 2022/3/4
     * @Param
     * @Return
     */

    public int unsubscribe(String card, String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.unsubscribe(connection, card, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * 通过账号id,检查管理员是否存在
     *
     * @param id id
     * @return boolean
     */
    public boolean checkAdminIsExistById(String id){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.checkAdminIsExistById(connection,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过身份证号,检查管理员是否存在
     *
     * @param card 身份证号
     * @return boolean
     */
    public boolean checkAdminIsExistByCard(String card){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.checkAdminIsExistByCard(connection,card);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过手机号,检查管理员是否存在
     *
     * @param phone 手机号
     * @return boolean
     */
    public boolean checkAdminIsExistByPhone(String phone){
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.checkAdminIsExistByPhone(connection,phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * @author wk
     * @Description 通过身份证号,获取管理员信息
     * @Date 20:44 2022/3/4
     * @Param
     * @Return
     */

    public Admin getAdminByCard(String card) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.getAdminByCard(connection, card);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 根据手机号,获取管理员信息
     * @Date 20:56 2022/3/4
     * @Param
     * @Return
     */

    public Admin getAdminByPhone(String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return adminDAO.getAdminByPhone(connection, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
4.2.9 接收用户传递的参数和数据库中的student表进行交互:
package com.student.service;

import com.student.bean.Student;
import com.student.dao.StudentDAOImpl;
import com.student.dao.util.JDBCUtils;
import org.junit.jupiter.api.Test;

import java.sql.*;
import java.util.List;
import java.util.Properties;

/**
 * @ClassName studentService
 * @Description 对学生表的增、删、查、改 等操作
 * @Author wk
 * @Date 2022/3/3 16:14
 * @Version 1.0
 */
public class StudentService {

    private StudentDAOImpl studentDAO = new StudentDAOImpl();

    /**
     * @author wk
     * @Description 添加学生信息
     * @Date 16:27 2022/3/3
     * @Param
     * @Return
     */
    public int addStudent(Student student) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return  studentDAO.insert(connection, student);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 删除学生信息
     * @Date 16:44 2022/3/3
     * @Param
     * @Return
     */
    public int deleteStudent(String studentId) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.delete(connection, studentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 根据学号,修改学生部分信息
     * @Date 16:48 2022/3/3
     * @Param
     * @Return
     */

    public int update(String studentId, String key, Object value) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.update(connection, studentId, key, value);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 根据修改全部信息
     * @Date 16:50 2022/3/3
     * @Param
     * @Return
     */

    public int updateAll(String oldStudentId, Student student) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.updateAll(connection, oldStudentId, student);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * @author wk
     * @Description 根据学号查询学生信息
     * @Date 16:56 2022/3/3
     * @Param
     * @Return
     */

    public Object search(String studentId) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.getStudentByStudentId(connection, studentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 查找全部学生信息
     * @Date 16:58 2022/3/3
     * @Param
     * @Return
     */

    public List<Student> searchAll() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.getStudentAll(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }

    /**
     * @author wk
     * @Description 获取学生总数
     * @Date 16:59 2022/3/3
     * @Param
     * @Return
     */

    public Long getCount() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.getCount(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return null;
    }


    /**
     * @author wk
     * @Description 清空所有学生的信息
     * @Date 20:09 2022/3/4
     * @Param
     * @Return
     */

    public int clearAll() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.clearAll(connection);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return 0;
    }

    /**
     * 通过学生id,检查学生是否存在
     *
     * @param studentId 学生id
     * @return boolean
     */

    public boolean checkStudentIsExistById(String studentId) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.checkStudentIsExistById(connection, studentId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过手机号,检查学生是否存在
     *
     * @param phone 电话
     * @return boolean
     */
    public boolean checkStudentIsExistByPhone(String phone) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.checkStudentIsExistByPhone(connection, phone);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * 通过身份证号,检查学生是否有重复
     *
     * @param card 身份证号
     * @return boolean
     */
    public boolean checkStudentIsExistByCard(String card) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            return studentDAO.checkStudentIsExistByCard(connection, card);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * @author wk
     * @Description 检查用户输入的字段是否存在
     * @Date 22:04 2022/3/3
     * @Param
     * @Return
     */

    public boolean checkExist(String value) {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnection();
            String sql = "select student_id,student_name,student_age,student_sex,student_phone,student_card,student_location," +
                    "student_english,student_math,student_java from student";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 获取结果集
            ResultSet resultSet = preparedStatement.executeQuery();
            // 获取结果集的元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            // 获取每一行的列数
            int columnCount = metaData.getColumnCount();
            String columnLabel = "";
            for (int i = 0; i < columnCount; i++) {
                // 获取每一行的每一列的别名
                columnLabel = metaData.getColumnLabel(i + 1);
                if (value.equals(columnLabel)) {
                    return true;
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(connection, null, null);
        }
        return false;
    }

    /**
     * @author wk
     * @Description 如果名字为两个字,则在名字中间添加空格,使其长度为3
     * @Date 23:42 2022/3/3
     * @Param
     * @Return
     */

    public String changeName(String name) {
        if (name.length() == 2) {
            name = name.substring(0, 1) + " " + name.substring(name.length() - 1);
        }
        return name;
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
4.2.10 StudentView类:用于菜单展示和用户交互
package com.student.view;

import com.student.bean.Admin;
import com.student.bean.Student;
import com.student.dao.AdminDAOImpl;
import com.student.service.AdminService;
import com.student.service.StudentService;
import com.student.tools.Tools;
import com.sun.corba.se.impl.orbutil.ObjectWriter;
import org.junit.platform.commons.util.CollectionUtils;

import javax.tools.Tool;
import java.awt.*;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * @ClassName StudentView
 * @Description 学生信息管理系统的菜单界面
 * @Author wk
 * @Date 2022/3/3 16:08
 * @Version 1.0
 */
public class StudentView {

    private String tempAdminId = null;   // 临时保存当前登录的管理员的账号
    private String tempAdminName = null; // // 临时保存当前登录的管理员的姓名
    private String tempAdminCard = null; // 临时保存当前登录的管理员的身份证号
    private String tempAdminPhone = null; // 临时保存当前登录的管理员的手机号

    AdminService adminService = new AdminService();
    StudentService studentService = new StudentService();

    /**
     * @author wk
     * @Description 登录主菜单
     * @Date 16:11 2022/3/3
     * @Param
     * @Return
     */
    public void enterRegisterMenu() {
        // 是否退出系统的标识
        boolean flag = true;
        while (flag) {
            System.out.println("------------------------------------【学生信息管理系统】---------------------------------------");
            System.out.println("\t\t\t\t\t\t\t\t\t1.登录\t\t2.注册");
            System.out.println("\t\t\t\t\t\t\t\t\t3.忘记密码\t4.关于我的");
            System.out.println("\t\t\t\t\t\t\t\t\t5.关闭系统");
            System.out.println("-------------------------------------------------------------------------------------------");
            System.out.println("请输入你的选择:");
            char choice = Tools.readRegisterMenuSelection();
            switch (choice) {
                case '1':
                    login();
                    break;
                case '2':
                    register();
                    break;
                case '3':
                    recover();
                    break;
                case '4':
                    about();
                    break;
                case '5':
                    System.out.println("是否要关闭系统(Y/N)?");
                    char exit = Tools.readConfirmSelection();
                    if (exit == 'Y' || exit == 'y') {
                        flag = false;
                        System.out.println("成功关闭系统");
                    }
                    break;
            }
        }
    }

    /**
     * @author wk
     * @Description 功能区主菜单
     * @Date 16:12 2022/3/3
     * @Param
     * @Return
     */
    public void enterMainMenu() {
        boolean flag = true;
        while (flag) {
            System.out.println("------------------------------------【学生信息管理系统】---------------------------------------");
            System.out.println("\t\t\t\t\t\t\t\t\t1.添加学生信息");
            System.out.println("\t\t\t\t\t\t\t\t\t2.删除学生信息");
            System.out.println("\t\t\t\t\t\t\t\t\t3.修改学生信息(指定部分)");
            System.out.println("\t\t\t\t\t\t\t\t\t4.修改学生信息(所有信息)");
            System.out.println("\t\t\t\t\t\t\t\t\t5.查找学生信息");
            System.out.println("\t\t\t\t\t\t\t\t\t6.显示学生信息(全体学生)");
            System.out.println("\t\t\t\t\t\t\t\t\t7.清空学生信息");
            System.out.println("\t\t\t\t\t\t\t\t\t8.导出学生信息");
            System.out.println("\t\t\t\t\t\t\t\t\t9.注销账户");
            System.out.println("\t\t\t\t\t\t\t\t\t0.退出登录");
            System.out.println("-------------------------------------------------------------------------------------------");
            System.out.println("请输入你的选择:");
            char choice = Tools.readMainMenuSelection();
            switch (choice) {
                case '1':
                    addStudent();
                    break;
                case '2':
                    deleteStudent();
                    break;
                case '3':
                    updateStudent();
                    break;
                case '4':
                    updateStudentAll();
                    break;
                case '5':
                    searchStudentById();
                    break;
                case '6':
                    showStudentAll();
                    break;
                case '7':
                    clearStudentAll();
                    break;
                case '8':
                    export();
                    break;
                case '9':
                    int unsubscribe = unsubscribe();
                    if (unsubscribe > 0) {
                        return;
                    }
                    break;
                case '0':
                    System.out.println("是否要退出登录状态(Y/N)?");
                    char exit = Tools.readConfirmSelection();
                    if (exit == 'Y' || exit == 'y') {
                        flag = false;
                        System.out.println("成功退出登录");
                    }
                    break;
            }
        }

    }

    /**
     * @author wk
     * @Description 登录
     * @Date 17:06 2022/3/3
     * @Param
     * @Return
     */

    public void login() {
        System.out.println("------------------------------------【登录】-------------------------------------------------");
        System.out.println("请输入你的个人账号:");
        String adminId = Tools.readString();
        System.out.println("请输入你的个人密码:");
        String password = Tools.readString();
        Admin login = adminService.login(adminId, password);
        if (login != null) {
            System.out.println("欢迎使用学生信息管理系统!,点击回车键开始使用学生信息管理系统");
            // 临时保存当前管理员的信息
            tempAdminId = login.getAdminId();
            tempAdminName = login.getName();
            tempAdminCard = login.getCard();
            tempAdminPhone = login.getPhone();
            // 按回车键继续
            Tools.readReturn();
            // 登录成功进入主菜单
            enterMainMenu();
        } else {
            System.out.println("账号或密码有误");
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }

    /**
     * @author wk
     * @Description 注册
     * @Date 17:27 2022/3/3
     * @Param
     * @Return
     */

    public void register() {
        System.out.println("------------------------------------【注册】------------------------------------------------");
        System.out.println("请输入你的个人账号:");
        String adminId = Tools.readString();
        while (adminService.checkAdminIsExistById(adminId)) {
            System.out.println("你输入的账号已经存在,请重新输入:");
            adminId = Tools.readString();
            adminService.checkAdminIsExistById(adminId);
        }
        System.out.println("请输入你的个人密码:");
        String password = Tools.readString();
        System.out.println("请输入你的姓名:");
        String name = Tools.readString();
        System.out.println("请输入你的身份证号:");
        String card = Tools.readString();
        while (adminService.checkAdminIsExistByCard(card)) {
            System.out.println("你输入的身份证号已经存在,请重新输入:");
            card = Tools.readString();
            adminService.checkAdminIsExistByCard(card);
        }
        System.out.println("请输入你的手机号:");
        String phone = Tools.readString();
        while (adminService.checkAdminIsExistByPhone(phone)) {
            System.out.println("你输入的手机号已经存在,请重新输入:");
            phone = Tools.readString();
            adminService.checkAdminIsExistByPhone(phone);
        }
        Admin admin = new Admin(adminId, password, name, card, phone);
        int register = adminService.register(admin);
        if (register > 0) {
            System.out.println("注册成功");
        } else {
            System.out.println("注册失败,请重新试一下!");
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }

    /**
     * @author wk
     * @Description 找回密码
     * @Date 19:49 2022/3/3
     * @Param
     * @Return
     */

    public void recover() {
        System.out.println("------------------------------------【找回密码】---------------------------------------------");
        System.out.println("请输入你的身份证号:");
        String card = Tools.readString();
        System.out.println("请输入你的手机号:");
        String phone = Tools.readString();
        String recover = adminService.recover(card, phone);
        if (recover != null) {
            System.out.println("密码找回成功,你的密码为:" + recover);
        } else {
            System.out.println("你输入的身份证号或手机号有误!");
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();

    }

    /**
     * @author wk
     * @Description 关于我的
     * @Date 20:28 2022/3/9
     * @Param
     * @Return
     */

    public void about() {
        try {
            Desktop desktop = Desktop.getDesktop();
            URI uri = new URI("https://blog.csdn.net/m0_47214030?type=blog"); //创建URI统一资源标识符
            desktop.browse(uri);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            System.out.println("欢迎使用学生信息管理系统【作者:☆往事随風☆】");
        }

    }


    /**
     * @author wk
     * @Description 显示学生信息列名
     * @Date 22:40 2022/3/3
     * @Param
     * @Return
     */


    public void showLabel() {
        System.out.println("学号\t\t姓名\t\t\t性别\t\t年龄\t\t\t电话\t\t\t\t住址\t\t\t\t\t身份证号\t\t\t英语\t\t\t数学\t\t\tjava");
    }

    /**
     * @author wk
     * @Description 添加学生信息
     * @Date 20:18 2022/3/3
     * @Param
     * @Return
     */

    public void addStudent() {
        System.out.println("------------------------------------【添加学生信息】------------------------------------------");
        boolean flag = true; // 继续添加学生标识
        int count = 0; // 当前已成功添加的学生数量
        while (flag) {
            System.out.println("请输入学生学号:");
            String studentId = Tools.readString();
            while (studentService.checkStudentIsExistById(studentId)) {
                System.out.println("你要添加的学号已经存在,请重新输入:");
                studentId = Tools.readString();
                studentService.checkStudentIsExistById(studentId);
            }
            System.out.println("请输入学生姓名:");
            String name = Tools.readString();
            name = studentService.changeName(name);
            System.out.println("请输入学生性别:");
            String sex = Tools.readString();
            System.out.println("请输入学生年龄:");
            int age = Tools.readInt();
            System.out.println("请输入学生电话:");
            String phone = Tools.readString();
            while (studentService.checkStudentIsExistByPhone(phone)) {
                System.out.println("你要添加的手机号已经存在,请重新输入:");
                phone = Tools.readString();
                studentService.checkStudentIsExistByPhone(phone);
            }
            System.out.println("请输入学生身份证号码:");
            String card = Tools.readString();
            while (studentService.checkStudentIsExistByCard(card)) {
                System.out.println("你要添加的身份证号已经存在,请重新输入:");
                card = Tools.readString();
                studentService.checkStudentIsExistByCard(card);
            }
            System.out.println("请输入如家庭住址:");
            String location = Tools.readString();
            System.out.println("请输入英语成绩:");
            double english = Tools.readDouble();
            System.out.println("请输入数学成绩:");
            double math = Tools.readDouble();
            System.out.println("请输入Java成绩:");
            double java = Tools.readDouble();
            Student student = new Student(studentId, name, sex, age, phone, location, card, english, math, java);
            int i = studentService.addStudent(student);
            if (i > 0) {
                count++;
                System.out.println("已成功添加" + count + "条学生信息");
                System.out.println("是否继续添加(Y/N)?");
                char choice = Tools.readConfirmSelection();
                if (choice == 'Y' || choice == 'y') {
                    System.out.println("-------------------------------------------------------------------------------------------");
                } else {
                    System.out.println("成功添加" + count + "条学生信息");
                    break;
                }
            } else {
                System.out.println("添加失败");
            }
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }


    /**
     * @author wk
     * @Description 删除学生信息
     * @Date 21:08 2022/3/3
     * @Param
     * @Return
     */

    public void deleteStudent() {
        System.out.println("------------------------------------【删除学生信息】------------------------------------------");
        System.out.println("请输入要删除学生的学号:");
        String studentId = Tools.readString();
        // 先查询一下该学号的学生信息
        Object search = studentService.search(studentId);
        if (search != null) {
            // 输出一下要删除的学生信息
            showLabel();
            System.out.println(search);
            System.out.println("确定要删除吗(Y/N):");
            char choice = Tools.readConfirmSelection();
            if (choice == 'y' || choice == 'Y') {
                int delete = studentService.deleteStudent(studentId);
                if (delete > 0) {
                    System.out.println("删除成功");
                } else {
                    System.out.println("删除失败");
                }
            }
        } else {
            System.out.println("要删除的学生不存在");
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }


    /**
     * @author wk
     * @Description 修改学生信息 (指定部分的信息)
     * @Date 21:17 2022/3/3
     * @Param
     * @Return
     */

    public void updateStudent() {
        System.out.println("------------------------------------【修改学生信息】------------------------------------------");
        System.out.println("请输入学生学号:");
        String studentId = Tools.readString();
        // 先查询一下该学号的学生信息
        Object search = studentService.search(studentId);
        if (search != null) {
            // 输出一下要删除的学生信息
            showLabel();
            System.out.println(search);
            System.out.println("确定要修改该学生的部分信息(Y/N)?");
            char choice = Tools.readConfirmSelection();
            if (choice == 'Y' || choice == 'y') {
                System.out.println("你可在以下字段中选择一个进行修改:");
                System.out.println("[student_id,student_name,student_sex,student_age,student_phone,student_location,student_card,student_english,student_math,student_java]");
                System.out.println("请输入要修改的字段:");
                String key = Tools.readString();
                while (!studentService.checkExist(key)) {
                    System.out.println("你输入的字段不存在,请重新输入:");
                    key = Tools.readString();
                    studentService.checkExist(key);
                }
                System.out.println("请输入修改后的信息:");
                String value = Tools.readString();
                if (key == "student_name") {
                    value = studentService.changeName(value);
                }
                // 修改指定信息
                int update = studentService.update(studentId, key, value);
                if (update > 0) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败,失败原因:新修改的学号或手机号或身份证号与其他学生相同");
                }
            }
        } else {
            System.out.println("你要修改的学生不存在!");
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }


    /**
     * @author wk
     * @Description 修改学生信息 (所有信息)
     * @Date 21:51 2022/3/3
     * @Param
     * @Return
     */

    public void updateStudentAll() {
        System.out.println("------------------------------------【修改学生全部信息】---------------------------------------");
        System.out.println("请输入学生学号:");
        String oldStudentId = Tools.readString();
        // 先查询一下该学号的学生信息
        Object search = studentService.search(oldStudentId);
        if (search != null) {
            // 输出一下要删除的学生信息
            showLabel();
            System.out.println(search);
            System.out.println("确定要修改该学生的全部信息(Y/N)?");
            char choice = Tools.readConfirmSelection();
            if (choice == 'Y' || choice == 'y') {
                System.out.println("请输入修改后的学生学号:");
                String studentId = Tools.readString();
                System.out.println("请输入修改后的学生姓名:");
                String name = Tools.readString();
                name = studentService.changeName(name);
                System.out.println("请输入修改后的学生性别:");
                String sex = Tools.readString();
                System.out.println("请输入修改后的学生年龄:");
                int age = Tools.readInt();
                System.out.println("请输入修改后的学生电话:");
                String phone = Tools.readString();
                System.out.println("请输入修改后的学生身份证号码:");
                String card = Tools.readString();
                System.out.println("请输入修改后的如家庭住址:");
                String location = Tools.readString();
                System.out.println("请输入修改后的英语成绩:");
                double english = Tools.readDouble();
                System.out.println("请输入修改后的数学成绩:");
                double math = Tools.readDouble();
                System.out.println("请输入修改后的Java成绩:");
                double java = Tools.readDouble();
                Student student = new Student(studentId, name, sex, age, phone, location, card, english, math, java);
                int updateAll = studentService.updateAll(oldStudentId, student);
                if (updateAll > 0) {
                    System.out.println("修改成功");
                } else {
                    System.out.println("修改失败,失败原因:新修改的学号或手机号或身份证号与其他学生相同");
                }
            }
        } else {
            System.out.println("你要修改的学生不存在!");
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }


    public void searchStudentById() {
        System.out.println("------------------------------------【查询学生信息】------------------------------------------");
        System.out.println("请输入要查找的学生的学号:");
        String studentId = Tools.readString();
        Object student = studentService.search(studentId);
        if (student != null) {
            System.out.println("查询结果如下:");
            showLabel();
            System.out.println(student);
        } else {
            System.out.println("该学生信息不存在");
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        Tools.readReturn();
    }


    /**
     * @author wk
     * @Description 显示全部学生信息
     * @Date 20:26 2022/3/3
     * @Param
     * @Return
     */

    public void showStudentAll() {
        System.out.println("------------------------------------【显示全体学生信息】---------------------------------------");
        Long count = studentService.getCount();
        System.out.println("一共查询到" + count + "条记录");
        showLabel();
        List<Student> students = studentService.searchAll();
        for (Student key : students) {
            System.out.println(key);
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }

    /**
     * @author wk
     * @Description 导出学生信息
     * @Date 19:53 2022/3/6
     * @Param
     * @Return
     */

    public void export() {
        RandomAccessFile randomAccessFile = null;
        try {
            System.out.println("------------------------------------【导出学生信息】------------------------------------------");
            System.out.println("1.自定义路径文件名\t\t2.默认保存路径文件名(D:\\\\student.csv)");
            System.out.println("3.暂不导出");
            String fileURL = null;
            System.out.println("请输入你的选择:");
            char choice = Tools.readRegisterMenuSelection();
            switch (choice) {
                case '1':
                    System.out.println("请输入要保存的文件路径(例如:D:\\\\student.csv):");
                    fileURL = Tools.readString();
                    break;
                case '2':
                    fileURL = "D:\\student.csv";
                    break;
                case '3':
                    System.out.println("-------------------------------------------------------------------------------------------");
                    Tools.readReturn();
                    return;
            }
            randomAccessFile = new RandomAccessFile(new File(fileURL), "rw");
            List<Student> students = studentService.searchAll();
            if (students.size() > 0 && null != students) {
                // 读入表头
                String title[] = new String[]{"学号,", "姓名,", "性别,", "年龄,", "手机号,", "身份证号,", "家庭住址,", "英语,", "数学,", "java,\n"};
                for (String key : title) {
                    randomAccessFile.write(("\uFEFF" + key).getBytes());
                }
                for (Student student : students) {
                    randomAccessFile.write((student.getStudentId() + ",").getBytes());
                    randomAccessFile.write((student.getName() + ",").getBytes());
                    randomAccessFile.write((student.getSex() + ",").getBytes());
                    randomAccessFile.write((student.getAge() + ",").getBytes());
                    randomAccessFile.write((student.getPhone() + ",").getBytes());
                    randomAccessFile.write((student.getCard() + ",").getBytes());
                    randomAccessFile.write((student.getLocation() + ",").getBytes());
                    randomAccessFile.write((student.getEnglish() + ",").getBytes());
                    randomAccessFile.write((student.getMath() + ",").getBytes());
                    randomAccessFile.write((student.getJava() + ",").getBytes());
                    randomAccessFile.write("\n".getBytes());
                }
                System.out.println("学生信息导出完毕");
            } else {
                System.out.println("尚未查询到学生信息,无法进行导出操作");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (randomAccessFile != null) {
                    randomAccessFile.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }

    /**
     * @author wk
     * @Description 清空所有学生信息
     * @Date 20:05 2022/3/4
     * @Param
     * @Return
     */

    public void clearStudentAll() {
        System.out.println("------------------------------------【清空所有学生信息】---------------------------------------");
        System.out.println("确定要清空所有学生信息(Y/N),执行此操作将丢失所有学生的信息:");
        char choice = Tools.readConfirmSelection();
        if (choice == 'Y' || choice == 'y') {
            List<Student> students = studentService.searchAll();
            if (students.size() > 0 && null != students) {
                System.out.println("请输入你的身份证号:");
                String card = Tools.readString();
                boolean isExist = adminService.checkAdminIsExistByCard(card);
                if (isExist) {
                    int clearAll = studentService.clearAll();
                    if (clearAll > 0) {
                        System.out.println("学生信息清空成功");
                    } else {
                        System.out.printf("学生信息清空失败");
                    }
                } else {
                    System.out.println("你输入的身份证号不存在");
                }
            }else {
                System.out.println("尚未存入学生信息,无法进行清空操作");
            }
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
    }

    /**
     * @author wk
     * @Description 注销账户
     * @Date 20:53 2022/3/4
     * @Param
     * @Return
     */

    public int unsubscribe() {
        System.out.println("------------------------------------【注销账户】----------------------------------------------");
        System.out.println("当前登录的管理员账号:" + tempAdminId);
        System.out.println("当前登录的管理员姓名:" + tempAdminName);
        System.out.println("确定要注销账户(Y/N)?");
        char choice = Tools.readConfirmSelection();
        if (choice == 'Y' || choice == 'y') {
            System.out.println("请输入你的身份证号:");
            String card = Tools.readString();
            System.out.println("请输入你的手机号:");
            String phone = Tools.readString();
            Admin adminByCard = adminService.getAdminByCard(card);
            Admin adminByPhone = adminService.getAdminByPhone(phone);
            if (adminByCard != null && adminByPhone != null) {
                if (tempAdminCard.equals(adminByCard.getCard()) && tempAdminPhone.equals(adminByPhone.getPhone())) {
                    int unsubscribe = adminService.unsubscribe(card, phone);
                    if (unsubscribe > 0) {
                        System.out.println("账户注销成功,已成功退出登录状态");
                        return unsubscribe;
                    } else {
                        System.out.println("账户注销失败");
                    }
                } else {
                    System.out.println("你输入的身份证号或手机号有误");
                }
            } else {
                System.out.println("你的身份证号或手机号不存在");
            }
        }
        System.out.println("-------------------------------------------------------------------------------------------");
        // 按回车键继续
        Tools.readReturn();
        return 0;
    }

    public static void main(String[] args) {
        StudentView studentView = new StudentView();
        studentView.enterRegisterMenu();
    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480
  • 481
  • 482
  • 483
  • 484
  • 485
  • 486
  • 487
  • 488
  • 489
  • 490
  • 491
  • 492
  • 493
  • 494
  • 495
  • 496
  • 497
  • 498
  • 499
  • 500
  • 501
  • 502
  • 503
  • 504
  • 505
  • 506
  • 507
  • 508
  • 509
  • 510
  • 511
  • 512
  • 513
  • 514
  • 515
  • 516
  • 517
  • 518
  • 519
  • 520
  • 521
  • 522
  • 523
  • 524
  • 525
  • 526
  • 527
  • 528
  • 529
  • 530
  • 531
  • 532
  • 533
  • 534
  • 535
  • 536
  • 537
  • 538
  • 539
  • 540
  • 541
  • 542
  • 543
  • 544
  • 545
  • 546
  • 547
  • 548
  • 549
  • 550
  • 551
  • 552
  • 553
  • 554
  • 555
  • 556
  • 557
  • 558
  • 559
  • 560
  • 561
  • 562
  • 563
  • 564
  • 565
  • 566
  • 567
  • 568
  • 569
  • 570
  • 571
  • 572
  • 573
  • 574
  • 575
  • 576
  • 577
  • 578
  • 579
  • 580
  • 581
  • 582
  • 583
  • 584
  • 585
  • 586
  • 587
  • 588
  • 589
  • 590
  • 591
  • 592
  • 593
  • 594
  • 595
  • 596
  • 597
  • 598
  • 599
  • 600
  • 601
  • 602
  • 603
  • 604
  • 605
  • 606
  • 607
  • 608
  • 609
  • 610
  • 611
  • 612
  • 613
  • 614
  • 615
  • 616
  • 617
  • 618
  • 619
  • 620
  • 621
  • 622
  • 623
  • 624
  • 625
  • 626
  • 627
  • 628
  • 629
  • 630
  • 631
  • 632
  • 633
  • 634
  • 635
  • 636
  • 637
  • 638
  • 639
  • 640
  • 641
  • 642
  • 643
  • 644
  • 645
  • 646
  • 647
  • 648
  • 649
  • 650
  • 651
  • 652
  • 653
  • 654
  • 655
  • 656
  • 657
  • 658
  • 659
  • 660
  • 661
  • 662
  • 663
  • 664
  • 665
  • 666
  • 667
  • 668
  • 669
  • 670
  • 671
  • 672
  • 673
  • 674
  • 675
  • 676
  • 677
  • 678
  • 679
  • 680
  • 681
  • 682
  • 683
  • 684
  • 685
  • 686
  • 687
  • 688
  • 689
  • 690
  • 691
  • 692
  • 693
  • 694
  • 695
  • 696
  • 697
  • 698
  • 699
  • 700
  • 701
  • 702
  • 703
  • 704
  • 705
  • 706
  • 707

五、菜单页面展示

5.1 登录菜单:

在这里插入图片描述

5.2 功能菜单:

在这里插入图片描述

六、在线演示

功能在线演示

七、项目源码

学生信息管理系统项目源码

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/793185
推荐阅读
相关标签
  

闽ICP备14008679号