学生信息管理系统(集合与数据库合并)

2021/11/17 19:09:44

本文主要是介绍学生信息管理系统(集合与数据库合并),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

定义类

private String name;
private int age;
private String sex;
private String phone;
@Override
public String toString() {
	return "学生信息: [姓名:" + name + ", 年龄:" + age + ", 性别:" + sex + ", 电话:" + phone + "]";
}
public Student(String name, int age, String sex, String phone) {
	super();
	this.name = name;
	this.age = age;
	this.sex = sex;
	this.phone = phone;
}
public Student() {
	super();
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public String getPhone() {
	return phone;
}
public void setPhone(String phone) {
	this.phone = phone;
}

Jdbc

static Connection connection = null;
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = (Connection) DriverManager.getConnection(
					"jdbc:mysql://127.0.0.1:3306/student_table?useUnicode=true&characterEncoding=utf-8", "root",
					"xuyifan991225");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//1.添加学生信息
	public static void insert(Student student) throws SQLException {
		String sql = "insert into student_table values(null,?,?,?,?)";
		//获得执行对象
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		statement.setString(1, student.getName());
		statement.setInt(2, student.getAge());
		statement.setString(3, student.getSex());
		statement.setString(4, student.getPhone());
		statement.executeUpdate();
	}
	//2.查看所有学生信息
	public static List<Student> selectAll() throws SQLException {
		String sql = "select * from student_table";
		//获得执行对象
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		ResultSet set = statement.executeQuery();
		List<Student>list = new ArrayList<Student>();
		Student student = null;
		while (set.next()) {
			student = new Student();
			student.setName(set.getString("student_name"));
			student.setAge(set.getInt("student_age"));
			student.setSex(set.getString("student_sex"));
			student.setPhone(set.getString("student_phone"));
			list.add(student);
		}
		return list;
	}
	//3.根据姓名查看学生信息
	public static Student selectOne(String name) throws SQLException {
		String sql = "select * from student_table where student_name=?";
		//获得执行对象
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		statement.setString(1, name);
		ResultSet set = statement.executeQuery();
		Student student = null;
		while (set.next()) {
			student = new Student();
			student.setName(set.getString("student_name"));
			student.setAge(set.getInt("student_age"));
			student.setSex(set.getString("student_sex"));
			student.setPhone(set.getString("student_phone"));
		}
		return student;
	}
	//4.根据姓名修改学生年龄
	public static int update(String name,int age) throws SQLException {
		String sql = "update student_table set student_age=? where student_name=?";
		//获得执行对象
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		statement.setInt(1, age);
		statement.setString(2, name);
		return statement.executeUpdate();
	}
	//5.根据姓名删除学生信息
	public static int delete(String name) throws SQLException {
		String sql = "delete from student_table where student_name=? ";
		//获得执行对象
		PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
		statement.setString(1, name);
		return statement.executeUpdate();
	}

方法类

static Scanner scanner = new Scanner(System.in);
	//1.添加学生信息
	public static void insert() throws SQLException {
		System.out.println("请输入姓名");
		String name = scanner.next();
		System.out.println("请输入年龄");
		int age = scanner.nextInt();
		System.out.println("请输入性别");
		String sex = scanner.next();
		System.out.println("请输入电话");
		String phone = scanner.next();
		Student student = new Student(name, age, sex, phone);
		StudentJdbc.insert(student);
	}
	//2.查看所有学生信息
	public static void selectAll() throws SQLException {
		List<Student>list = StudentJdbc.selectAll();
		for (Student student : list) {
			System.out.println(student);
		}
	}
	//3.根据姓名查看学生信息
	public static void selectOne() throws SQLException {
		System.out.println("请输入你要查询的名字");
		String name = scanner.next();
		Student student = StudentJdbc.selectOne(name);
		System.out.println(student);
	}
	//4.根据姓名修改学生年龄
	public static void update() throws SQLException {
		System.out.println("请输入姓名");
		String name = scanner.next();
		System.out.println("请输入修改后年龄");
		int age = scanner.nextInt();
		System.out.println(StudentJdbc.update(name, age));
	}
	//5.根据姓名删除学生信息
	public static void delete() throws SQLException {
		System.out.println("请输入姓名");
		String name = scanner.next();
		System.out.println(StudentJdbc.delete(name));
	}

实现类

Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.println("学生信息管理系统");
			System.out.println("请输入选项1.登录2.退出");
			int cc = scanner.nextInt();
			switch (cc) {
			case 1:
				System.out.println("请输入用户名");
				String used = scanner.next();
				System.out.println("请输入密码");
				int pased = scanner.nextInt();
				if (used.equals("admin") && pased == 123) {
					System.out.println("登陆成功");
					while (true) {
						System.out.println("请输入选项1.添加学生信息2.查看所有学生信息3.根据姓名查看学生信息4.根据姓名修改学生年龄5.根据姓名删除学生信息6.退出");
						int ch = scanner.nextInt();
						switch (ch) {
						case 1:
							StudentWay.insert();
							break;
						case 2:
							StudentWay.selectAll();;
							break;
						case 3:
							StudentWay.selectOne();
							break;
						case 4:
							StudentWay.update();
							break;
						case 5:
							StudentWay.delete();
							break;
						case 6:
							System.out.println("拜拜");
							return;
						default:
							System.out.println("眼睛不要咱就捐了吧");
							break;
						}
					}
				} else {
					System.out.println("用户名密码不匹配");
				}
				break;
			case 2:
				System.out.println("拜拜");
				return;
			default:
				System.out.println("你是假如给你三天光明的主角?");
				break;
			}
		}



这篇关于学生信息管理系统(集合与数据库合并)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程