赞
踩
自定义导出
,另一种是默认导出
注销当前登录的管理者信息
,注销成功后退出登录状态,返回到未登录界面字段名 | 说明 | 类型 | 主键 | 外键 | 非空 | 唯一 | 自增 |
---|---|---|---|---|---|---|---|
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_math | math成绩 | double | 否 | 否 | 是 | 否 | 否 |
student_java | java成绩 | double | 否 | 否 | 是 | 否 | 否 |
字段名 | 说明 | 类型 | 主键 | 外键 | 非空 | 唯一 | 自增 |
---|---|---|---|---|---|---|---|
id | 管理员编号 | int(10) | 是 | 否 | 是 | 是 | 是 |
admin_id | 管理员账号 | varchar(11) | 否 | 否 | 是 | 是 | 否 |
admin_password | 管理员密码 | varchar(32) | 否 | 否 | 是 | 否 | 否 |
admin_name | 管理员姓名 | varchar(20) | 否 | 否 | 是 | 否 | 否 |
admin_card | 管理员身份证号 | varchar(18) | 否 | 否 | 是 | 是 | 否 |
admin_phone | 管理员手机号 | varchar(15) | 否 | 否 | 是 | 是 | 否 |
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); }
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); }
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; } }
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 + '\'' + '}'; } }
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; } }
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); } }
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; } }
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); } }
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); } }
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; } }
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; } }
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(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。