使用数据库改造学生管理系统

发布时间:2023年12月18日

202221336029--网安2211

目录

1.准备工作

1.构建MYSQL数据库

2.实现功能

2.连接到数据库

3.完整代码实现

1.项目结构

2.StudentDao接口编写

3.功能实现

添加学生

删除学生

根据姓名查询学生

根据gpa,major查找学生

查找所有的学生

4.最终测试


1.准备工作

1.构建MYSQL数据库

2.实现功能

需要实现:

  1. 向数据库添加学生
  2. 向数据库移除学生
  3. 通过major,name,GPA寻找学生
  4. 展示数据库的所有学生

2.连接到数据库

JDBCUtil类,连接上数据库



测试:

成功插入

3.完整代码实现

1.项目结构

2.StudentDao接口编写

3.功能实现

添加学生

删除学生

通过id删除学生

根据姓名查询学生

测试结果:

根据gpa,major查找学生

实际操作与根据姓名查找学生类似

直接运行测试代码:


查找所有的学生

String sql="Select * from students";返回所有内容

4.最终测试

Main代码修改

package student;

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

public class Main {
	public static void main(String[] args) {

		/**
		 * This program is a student management system that allows users to add, remove,
		 * and search for students by name, major, and GPA. It uses the Student and
		 * StudentManagementSystem classes to store and manage student data.
		 */
		// Initialize the scanner
		Scanner scanner = new Scanner(System.in);
		StudentManagementSystem sms = new StudentManagementSystem();
		boolean running = true;
		while (running) {
			// Prompt user for input
			System.out.println("Enter 1 to add a student");
			System.out.println("Enter 2 to remove a student");
			System.out.println("Enter 3 to search for a student by name");
			System.out.println("Enter 4 to search for a student by major");
			System.out.println("Enter 5 to search for a student by GPA");
			System.out.println("Enter 6 to show all students");
			System.out.println("Enter 7 to exit");

			// Get user input
			int choice = scanner.nextInt();

			// Process user input
			switch (choice) {
			case 1:
				// Add a student
				System.out.println("Enter student name:");
				String name = scanner.next();
				System.out.println("Enter student age:");
				int age = scanner.nextInt();
				System.out.println("Enter student gender:");
				String gender = scanner.next();
				System.out.println("Enter student ID:");
				String id = scanner.next();
				System.out.println("Enter student major:");
				String major = scanner.next();
				System.out.println("Enter student GPA:");
				double gpa = scanner.nextDouble();
				Student student = new Student(name, age, gender, id, major, gpa);
				sms.writeStudent(student);
				System.out.println("Student added successfully!\n");
				break;
			case 2:
				// Remove a student
				System.out.println("Enter student ID to remove:");
				String removeId = scanner.next();
                boolean removed = sms.removeStudent(removeId);
				if (!removed) {
					System.out.println("Student not found!");
				}
                else{
                    System.out.println("Student removed successfully!\n");
                }
				break;
			case 3:
				// Search for a student by name
				System.out.println("Enter student name to search:");
				String searchName = scanner.next();
				List<Student> searchResults = sms.getStudentsByName(searchName);
				if (searchResults.isEmpty()) {
					System.out.println("No students found!");
				} else {
					System.out.println("Search results:");
					for (Student s : searchResults) {
						System.out.println(s);
					}
				}
				break;
			case 4:
				// Search for a student by major
				System.out.println("Enter student major to search:");
				String searchMajor = scanner.next();
				searchResults = sms.getStudentsByMajor(searchMajor);
				if (searchResults.isEmpty()) {
					System.out.println("No students found!");
				} else {
					System.out.println("Search results:");
					for (Student s : searchResults) {
						System.out.println(s);
					}
				}
				break;
			case 5:
				// Search for a student by GPA
				System.out.println("Enter student GPA to search:");
				double searchGpa = scanner.nextDouble();
				searchResults = sms.getStudentsByGpa(String.valueOf(searchGpa));
				if (searchResults.isEmpty()) {
					System.out.println("No students found!");
				} else {
					System.out.println("Search results:");
					for (Student s : searchResults) {
						System.out.println(s);
					}
				}
				break;
			case 6:
				// Show all Students
				List<Student> studentList = sms.getAllStudents();
				if (studentList.size() == 0) {
					System.out.println("The System Data is empty Now!");
				}else {
					for (Student studentItem : studentList) {
						System.out.println(studentItem.toString());
					}
				}
				break;
			case 7:
				// Exit the program
				running = false;
				System.out.println("Exit Successfully!");
				break;
			default:
				// Invalid input
				System.out.println("Invalid choice!");
				break;
			}
		}
		// Close scanner
		scanner.close();
	}
}

添加

查找

gpa与major查找

展示所有

删除

最终数据库:

001被删去,fjh被添加

文章来源:https://blog.csdn.net/qq_73721069/article/details/134960955
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。