202221336029--网安2211
目录
需要实现:
JDBCUtil类,连接上数据库
测试:
成功插入
通过id删除学生
测试结果:
实际操作与根据姓名查找学生类似
直接运行测试代码:
String sql="Select * from students";返回所有内容
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被添加