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


一、技术栈

  • 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);

}


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

}

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

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 + '\'' +
                '}';
    }
}

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

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

}

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

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


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

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


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


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


五、菜单页面展示

5.1 登录菜单:

在这里插入图片描述

5.2 功能菜单:

在这里插入图片描述

六、在线演示

功能在线演示

七、项目源码

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

  • 10
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
JavaSEJava平台的一个版本,它提供了一系列的API和工具,可以用于开发各种类型的应用程序。MySQL是一个开源关系型数据库管理系统,它提供了高效、稳定和可靠的数据存储和检索功能。JDBCJava数据库连接技术,它允许通过Java代码连接和操作关系型数据库。 在一个JavaSE项目中,我们可以使用JDBC来连接MySQL数据库,并完成与数据库的交互。首先,我们需要导入JDBC相关的jar包,例如MySQL Connector/J,这个jar包提供了连接MySQL数据库所需的驱动程序。我们可以从MySQL官网下载并导入这个jar包。 接下来,在Java代码中,我们通过加载JDBC驱动程序,使用合适的URL、用户名和密码连接到MySQL数据库。这个URL包含了数据库的IP地址、端口号和数据库名称等信息。一旦连接成功,我们可以使用JDBC提供的API执行SQL语句,例如插入、更新和删除数据,或者查询数据库中的数据。 在JDBC项目中,我们可以使用PreparedStatement对象来预编译SQL语句,以提高执行效率和安全性。通过设置参数,我们可以动态地构建和执行SQL语句,避免了SQL注入等安全问题。 此外,我们还可以使用JDBC的事务操作来确保数据库的一致性和完整性。通过开始事务、执行SQL语句和提交或回滚事务,我们可以在多个数据库操作之间实现原子性和隔离性。 在开发JDBC项目时,我们还要注意资源的管理和释放,包括数据库连接、Statement和ResultSet等对象的关闭,以避免内存泄漏和性能问题。 总的来说,基于JavaSEMySQL JDBC项目可以通过JDBC技术与MySQL数据库进行连接和交互。开发者可以使用JDBC提供的API执行各种数据库操作,并通过事务来确保数据的一致性。在项目开发过程中要注意合理管理和释放资源,以提高项目的性能和稳定性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

☆*往事随風*☆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值