学生管理系统(登录,增删改查)java

package student_system.entity;

import java.io.Serializable;

public class LoginInformation implements Serializable {
	private String name;
	private String sex;
	private String id;
	private String pwd;
	public String getName() {
		return name;
	}
	public LoginInformation(String id, String pwd) {
		super();
		this.id = id;
		this.pwd = pwd;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((pwd == null) ? 0 : pwd.hashCode());
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		LoginInformation other = (LoginInformation) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (pwd == null) {
			if (other.pwd != null)
				return false;
		} else if (!pwd.equals(other.pwd))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}
	@Override
	public String toString() {
		return "LoginInformation [name=" + name + ", sex=" + sex + ", id=" + id + ", pwd=" + pwd + "]";
	}
	public LoginInformation(String name, String sex, String id, String pwd) {
		super();
		this.name = name;
		this.sex = sex;
		this.id = id;
		this.pwd = pwd;
	}
	
	public LoginInformation() {
		// TODO Auto-generated constructor stub
	}
	
	
}


package student_system.entity;

import java.io.Serializable;

public class Student implements	Serializable {
	private String name;
	private String sex;
	private int age;
	private String num;
	private double score;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		if(sex.equals("男")||sex.equals("女")) {
			this.sex = sex;
		}else {
			System.out.println("你输入的性别有误,默认系统会默认为:男");
			this.sex = "男";
		}
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		if(age>=0&&age<120) {
		this.age = age;
		}else {
			System.out.println("你输入的年龄超出了人类的认知范围,系统默认为是:18");
			this.age = 18;
//			while(true) {
//				if(age>=0&&age<120) {
//					this.age=age;
//					return;
//				}else{
//					System.out.println("你输入的年龄绝对有误,请重新输入");
//				}
//			}
		}
	}
	public String getNum() {
		return num;
	}
	public void setNum(String num) {
		this.num = num;
	}
	public double getScore() {
		return score;
	}
	public void setScore(double score) {
		if(score>=0&&score<=100) {
		this.score = score;
		}else {
			System.out.println("你输入的成绩有误,系统默认为60");
			this.score = 60.0;
		}
	}
	@Override
	public String toString() {
		return "" + name + "\t" + sex + "\t" + age + "\t" + num + "\t" + score + "";
	}
	public Student(String name, String sex, int age, String num, double score) {
		super();
		this.name = name;
		this.sex = sex;
		this.age = age;
		this.num = num;
		this.score = score;
	}
	public Student() {
		super();
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((num == null) ? 0 : num.hashCode());
		long temp;
		temp = Double.doubleToLongBits(score);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((sex == null) ? 0 : sex.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student other = (Student) obj;
		if (age != other.age)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (num == null) {
			if (other.num != null)
				return false;
		} else if (!num.equals(other.num))
			return false;
		if (Double.doubleToLongBits(score) != Double.doubleToLongBits(other.score))
			return false;
		if (sex == null) {
			if (other.sex != null)
				return false;
		} else if (!sex.equals(other.sex))
			return false;
		return true;
	}
}

创建两个文件夹,用来保存账号密码和学生信息用的

 

package student_system.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import student_system.entity.Student;

public class FileUtil {
	static public final String PATH = "E:\\java资料\\2105\\demo02\\info.txt";
	
	/**
	 * 保存文件的方法
	 * @param list
	 */
	public static void saveFile(List<Student> list) {
		
		ObjectOutputStream oos = null;
		
		try {
			oos = new ObjectOutputStream(new FileOutputStream(PATH));
			if(list==null&&list.size()<1) {
				System.out.println("没有该集合");
			}
			oos.writeObject(list);
			oos.flush();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				oos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * 获取文件的方法
	 * @return
	 */
	public static List<Student> getFile() {
		ObjectInputStream ois = null;
		
		try {
			ois = new ObjectInputStream(new FileInputStream(PATH));
			try {
				List<Student> list = (List<Student>)ois.readObject();
				return list;
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				ois.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	
}



package student_system.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import student_system.entity.LoginInformation;

public class FileUtil2 {
	static public final String PATH1 = "E:\\java资料\\2105\\demo02\\client.txt";
	
	
	/**
	 * 保存信息方法
	 * @param list
	 */
	public static void saveFile(List<LoginInformation> list) {
		ObjectOutputStream oos = null;
		
		try {
			oos = new ObjectOutputStream(new FileOutputStream(PATH1));
			if(list==null&&list.size()<1) {
				System.out.println("集合为空");
			}
			oos.writeObject(list);
			oos.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				oos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}	
	}
	/**
	 * 获取信息的方法
	 * @return
	 */
	public static List<LoginInformation> getFile(){
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(PATH1));
			try {
				List<LoginInformation> list = (List<LoginInformation>)ois.readObject();
				return list;
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				ois.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

}

登录方法

package student_system.update;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import student_system.entity.LoginInformation;
import student_system.util.FileUtil2;


/**
 * 用户
 * @author Lenovo
 *
 */
public class Client {
	static Scanner input = new Scanner(System.in);
//	static public final String PATH = "E:\\java资料\\2105\\demo02\\client.txt";
	static List<LoginInformation> list = new ArrayList<LoginInformation>();
	/*
	 * static初始化一次就不需要了,所以注释掉
	 */
//	static {
//		list.add(new LoginInformation("小猫", "女", "100", "127"));
//		list.add(new LoginInformation("小猫", "女", "101", "126"));
//		list.add(new LoginInformation("小猫", "女", "102", "125"));
//		list.add(new LoginInformation("小猫", "女", "103", "124"));
//		FileUtil2.saveFile(list);
//		FileUtil2.getFile();
//	}
	
	public void showMenu() {
		while(true) {
			list = FileUtil2.getFile();
			System.out.println("1.登录2.注册3.退出");
			String chose = input.next();
			switch (chose) {
			case "1":
				login();
				break;
			case "2":
				//注册
				register();
				break;
			case "3":
				System.out.println("退出成功");
				return;

			default:
				System.out.println("没有这个选项,请重新选择");
				break;
			}
			FileUtil2.saveFile(list);
		}
	}
	/**
	 * 注册方法
	 */
	private void register() {
		System.out.println("请输入你的信息");
		System.out.println("请输入你的姓名:");
		String name = input.next();
		System.out.println("请输入你的性别:");
		String sex = input.next();
		System.out.println("请输入你的账号:");
		String id = input.next();
		System.out.println("请输入你的密码:");
		String pwd = input.next();
		LoginInformation li = new LoginInformation(name, sex, id, pwd);
		boolean flag = list.stream().anyMatch(x->x.getId().equals(id));
		if(flag==true) {
			System.out.println("你注册的账号已经存在,注册失败");
			return;
		}else {
			System.out.println("注册成功");
			list.add(li);
		}
		FileUtil2.saveFile(list);
//		System.out.println("ssss");
	}
	/**
	 * 登录
	 */
	private void login() {
		FileUtil2.getFile();
		System.out.println("请输入账号和密码");
		System.out.println("请输入账号:");
		String id = input.next();
		System.out.println("请输入密码:");
		String pwd = input.next();
//		list.add(new LoginInformation(id, pwd));
		//判断账号密码是否正确			anyMatch和list集合的id逐个寻找,有相同的返回true
		boolean flag = list.stream().anyMatch(x->x.getId().equals(id)&&x.getPwd().equals(pwd));
		if(flag==false) {
			System.out.println("账号密码或错误");
			return;
		}
		System.out.println("登录成功");
		
//		FileUtil2.saveFile(list);
		
		//登录成功后进入学生管理系统
		StudentStream ss = new StudentStream();
		ss.showMenu();
	}
}

 学员系统

package student_system.update;
/**
 * 学员系统
 * 1.查 2.新增 3.修改 4.删除 5.退出系统
 * @author Lenovo
 *
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import student_system.entity.Student;
import student_system.util.FileUtil;

public class StudentStream {
	static Scanner input = new Scanner(System.in);
	static List<Student> list = new ArrayList<Student>();
	
	
	//静态代码块初始化集合
//	static {
//		list.add(new Student("张三", "男", 23, "101", 89));
//		list.add(new Student("李四", "女", 22, "102", 99));
//		list.add(new Student("王五", "男", 32, "103", 88));
//		list.add(new Student("赵六", "男", 56, "104", 89));
//		list.add(new Student("田七", "男", 15, "105", 96));
//		list.add(new Student("小狗", "女", 18, "106", 98));
//		list.add(new Student("小白", "女", 19, "107", 99));
//	}
//	list = FileUtil.getFile();
	/**
	 * 展示系统菜单
	 */
	public void showMenu() {
		while (true) {
			//文件进行数据同步,保证数据一致
			list = FileUtil.getFile();
			System.out.println("==============欢迎使用xxx的管理系统===================");
			System.out.println("===========1.查 2.新增 3.修改 4.删除 5.退出系统===========");
			System.out.println("==============请输入你要选择的操作:====================");
			String chose = input.next();
			switch (chose) {
			case "1":
				//查看学员信息
				showStudentInfoMenu();
				break;
			case "2":
				//添加新学员
				addStudent();
				break;
			case "3":
				//修改学员信息
				undateStudentInfo();
				break;
			case "4":
				//删除学员
				delMenu();
				break;
			case "5":
				System.out.println("退出成功");
				return;

			default:
				System.out.println("你输入的没有这个选项哦");
				break;
			}
		}
	}
	
	/**
	 * 删除菜单
	 */
	public void delMenu() {
		while(true) {
			//文件进行数据同步,保证数据一致
			list = FileUtil.getFile();
			System.out.println("=========请你输入你要删除的操作===========");
			System.out.println("====1.单个删除 	2.批量删除		3.其他键返回上一级====");
			String chose = input.next();
			switch (chose) {
			case "1":
				//单个删除
				delStudent();
				break;
			case "2":
				//批量删除
				delMuitStudent();
				break;

			default:
				return;
			}
			FileUtil.saveFile(list);
		}
	}
	/**
	 * 单个删除
	 */
	public void delStudent() {
		System.out.println("请你输入你要删除学员的学号:");
		String num = input.next();
		//从list里获得num来和输入的学号比较,如果list里面有就返回这个学号的全部信息,没有就返回空null
		Student s = list.stream().filter(x->x.getNum().equals(num)).findAny().orElse(null);
		if(s==null) {
			System.out.println("没有这个学员,不能删除哦");
			return;
		}
		list.remove(s);
		FileUtil.saveFile(list);
		System.out.println("删除成功");
	}
	/**
	 * 批量删除
	 */
	public void delMuitStudent() {
		System.out.println("请输入你要删除学员的学号,逗号分割");
		String num = input.next();
		String[] str = num.split(",");
		//创建一个集合,来存放要删除的数
		List<Student> delStudent = new ArrayList<Student>();
		for (String string : str) {
			Student s = list.stream().filter(x->x.getNum().equals(string)).findAny().orElse(null);
			if(s!=null) {
				//找到这个学号
				//吧这个学号加入到要删除的集合中
				delStudent.add(s);
			}else {
				System.out.println("没有找到你要删除的学号:"+string);
			}
		}	
		list.removeAll(delStudent);
		System.out.println("删除成功");
	}
	
	/*
	 * 修改学员信息
	 */
	public void undateStudentInfo() {
		System.out.println("请输入你要修改学员的学号:");
		String num = input.next();
		//.findAny()表示将其中任意一个返回;
		//orElse(null)表示如果一个都没找到返回null。
		Student s = list.stream().filter(x->x.getNum().equals(num)).findAny().orElse(null);
		if(s==null) {
			System.out.println("没有这个学员哦");
			return;
		}
		System.out.println("请输入要修改的学员信息");
		System.out.println("请输入学员的姓名:");
		String name = input.next();
		System.out.println("请输入学员的性别:");
		String sex = input.next();
		System.out.println("请输入学员的年龄:");
		int age = input.nextInt();
		System.out.println("请输入学员的分数:");
		double score = input.nextDouble();
		s.setName(name);
		s.setSex(sex);
		s.setAge(age);
		s.setScore(score);
		System.out.println("修改成功");
		FileUtil.saveFile(list);
	}
	/*
	 * 新增学员
	 */
	public void addStudent() {
		Student s = new Student();
		System.out.println("请输入新增学员的信息");
		System.out.println("请输入学员的姓名:");
		String name = input.next();
		s.setName(name);
		System.out.println("请输入学员的性别:");
		String sex = input.next();
		s.setSex(sex);
		System.out.println("请输入学员的年龄:");
		int age = input.nextInt();
		s.setAge(age);
//		if(age<0||age>120) {
//			System.out.println("年龄不符合,请重新输入");
//			return;
//		}
	
		System.out.println("请输入学员的学号:");
		String num = input.next();
		s.setNum(num);
		System.out.println("请输入学员的分数:");
		double score = input.nextDouble();
		s.setScore(score);
//		list.add(new Student(name, sex, age, num, score));
//		Student s = new Student(name, sex, age, num, score);
		
		boolean flag = list.stream().anyMatch(x->x.getName().equals(s.getName())&&x.getNum().equals(s.getNum()));
		if(flag==true) {
			System.out.println("已有这个学员");
			
		}else {
			list.add(s);
			System.out.println("添加成功");
		}
		FileUtil.saveFile(list);
	}
	/*
	 * 查看学员信息
	 */
	public void showStudentInfoMenu() {
//		for (Student student : list) {
//			System.out.println(student.toString());
//		}
//		System.out.println(list);
		FileUtil.saveFile(list);
		while(true) {
			System.out.println("=================查询学员信息菜单========================");
			System.out.println("====1.成绩排序2.学号排序 3.性别排序 4.输入其他返回上一级菜单=====");
			String chose = input.next();
			switch (chose) {
			case "1":
				//成绩排序
				showStudentMessage();
				break;
			case "2":
				//学号排序
				showListByNum();
				break;
			case "3":
				//性别排序
				showStudentBySex();
				break;
			default:
				return;
			}
		}
	}
	/**
	 * 性别排序
	 */
	public void showStudentBySex() {
		// TODO Auto-generated method stub
		System.out.println("请输入你要排序的性别:");
		String s = input.next();
		System.out.println("姓名\t性别\t年龄\t学号\t分数");
		if(!s.equals("男")&&!s.equals("女")) {
			System.out.println("你输入的性别有误,系统会默认为:男");
			s="男";
		}
		String sex = s;
		list.stream().filter(x->x.getSex().equals(sex)).forEach(System.out::println);
//		while(true) {
//			System.out.println("====1.成绩排序2.学号排序 3.输入其他返回上一级菜单=====");
//			System.out.println("你还要进行排序吗?");
//			String chose = input.next();
//			switch (chose) {
//			case "1":
//				showStudentSexMessage();
//				break;
//			case "2":
//				
//				break;
//			case "3":
//				
//				return;
//
//			default:
//				System.out.println("没有这个选项");
//				break;
//			}
//		}
	}

	/**
	 * 学号排序
	 */
	public void showListByNum() {
		// TODO Auto-generated method stub
		System.out.println("姓名\t性别\t年龄\t学号\t分数");
		//使用排序放法
		list.stream().sorted(
				//传入s1和s2叁数,返回比较结果
				(s1,s2)->Integer.compare(
						Integer.valueOf(s1.getNum()), 
						Integer.valueOf(s2.getNum())))
		.forEach(System.out::println);
		
	}

	/**
	 * 成绩排序
	 */
	public void showStudentMessage() {
//		FileUtil.saveFile(list);
		System.out.println("姓名\t性别\t年龄\t学号\t分数");
		list.stream().sorted((s1,s2)->Double.compare(s1.getScore(), s2.getScore())).forEach(System.out::println);
		
	}
}

 

package student_system.update;

import student_system.entity.Student;

public class Demo {
	public static void main(String[] args) {
//		StudentStream ss = new StudentStream();
		Client c = new Client();
		c.showMenu();
//		ss.showMenu();
	}

}

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Java学生管理系统增删改查的示例代码: 1. 添加学生信息: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class StudentManager { public static void addStudent(Student student) { try { // 连接数据库 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); // 准备SQL语句 String sql = "INSERT INTO students (id, name, age) VALUES (?, ?, ?)"; PreparedStatement stmt = conn.prepareStatement(sql); // 设置参数 stmt.setInt(1, student.getId()); stmt.setString(2, student.getName()); stmt.setInt(3, student.getAge()); // 执行SQL语句 stmt.executeUpdate(); // 关闭连接 stmt.close(); conn.close(); System.out.println("学生信息添加成功!"); } catch (SQLException e) { e.printStackTrace(); } } } ``` 2. 删除学生信息: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class StudentManager { public static void deleteStudent(int id) { try { // 连接数据库 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); // 准备SQL语句 String sql = "DELETE FROM students WHERE id = ?"; PreparedStatement stmt = conn.prepareStatement(sql); // 设置参数 stmt.setInt(1, id); // 执行SQL语句 stmt.executeUpdate(); // 关闭连接 stmt.close(); conn.close(); System.out.println("学生信息删除成功!"); } catch (SQLException e) { e.printStackTrace(); } } } ``` 3. 修改学生信息: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class StudentManager { public static void updateStudent(Student student) { try { // 连接数据库 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); // 准备SQL语句 String sql = "UPDATE students SET name = ?, age = ? WHERE id = ?"; PreparedStatement stmt = conn.prepareStatement(sql); // 设置参数 stmt.setString(1, student.getName()); stmt.setInt(2, student.getAge()); stmt.setInt(3, student.getId()); // 执行SQL语句 stmt.executeUpdate(); // 关闭连接 stmt.close(); conn.close(); System.out.println("学生信息修改成功!"); } catch (SQLException e) { e.printStackTrace(); } } } ``` 4. 查询学生信息: ```java import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class StudentManager { public static void queryStudent(int id) { try { // 连接数据库 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/database_name", "username", "password"); // 准备SQL语句 String sql = "SELECT * FROM students WHERE id = ?"; PreparedStatement stmt = conn.prepareStatement(sql); // 设置参数 stmt.setInt(1, id); // 执行SQL语句 ResultSet rs = stmt.executeQuery(); // 处理查询结果 if (rs.next()) { int studentId = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("学生信息:"); System.out.println("学号:" + studentId); System.out.println("姓名:" + name); System.out.println("年龄:" + age); } else { System.out.println("未找到该学生信息!"); } // 关闭连接 rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值