用eclipse编写学生信息管理系统源代码
时间: 2023-07-18 20:44:04 浏览: 117
以下是一个简单的学生信息管理系统的Java源代码,您可以在Eclipse中创建一个新的Java项目,将以下代码复制到您的Java类中:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private static final String URL = "jdbc:mysql://localhost:3306/students";
private static final String USERNAME = "root";
private static final String PASSWORD = "your_password";
private static Connection connection;
private static PreparedStatement preparedStatement;
public static void main(String[] args) {
try {
// 连接到MySQL数据库
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
System.out.println("Database connected!");
// 显示主菜单
displayMainMenu();
// 关闭数据库连接
connection.close();
System.out.println("Database disconnected!");
} catch (SQLException e) {
System.err.println("Database connection failed!");
e.printStackTrace();
}
}
// 显示主菜单
private static void displayMainMenu() throws SQLException {
Scanner scanner = new Scanner(System.in);
int choice = 0;
do {
System.out.println("********** Student Management System **********");
System.out.println("1. Add a new student");
System.out.println("2. Update an existing student");
System.out.println("3. Delete a student");
System.out.println("4. View all students");
System.out.println("5. Exit");
System.out.print("Enter your choice (1-5): ");
choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent();
break;
case 2:
updateStudent();
break;
case 3:
deleteStudent();
break;
case 4:
viewAllStudents();
break;
case 5:
System.out.println("Exiting the system...");
break;
default:
System.out.println("Invalid choice! Please enter a valid choice (1-5).");
break;
}
} while (choice != 5);
scanner.close();
}
// 添加学生信息
private static void addStudent() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student's name: ");
String name = scanner.nextLine();
System.out.print("Enter the student's age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter the student's gender: ");
String gender = scanner.nextLine();
System.out.print("Enter the student's email: ");
String email = scanner.nextLine();
System.out.print("Enter the student's password: ");
String password = scanner.nextLine();
// 将学生信息添加到数据库
String sql = "INSERT INTO students (name, age, gender, email, password) VALUES (?, ?, ?, ?, ?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, name);
preparedStatement.setInt(2, age);
preparedStatement.setString(3, gender);
preparedStatement.setString(4, email);
preparedStatement.setString(5, password);
int rowsInserted = preparedStatement.executeUpdate();
if (rowsInserted > 0) {
System.out.println("A new student has been added successfully!");
} else {
System.out.println("Failed to add a new student!");
}
scanner.close();
}
// 更新学生信息
private static void updateStudent() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student's email to update: ");
String email = scanner.nextLine();
System.out.print("Enter the student's new name: ");
String name = scanner.nextLine();
System.out.print("Enter the student's new age: ");
int age = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter the student's new gender: ");
String gender = scanner.nextLine();
System.out.print("Enter the student's new password: ");
String password = scanner.nextLine();
// 更新学生信息
String sql = "UPDATE students SET name = ?, age = ?, gender = ?, password = ? WHERE email = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, name);
preparedStatement.setInt(2, age);
preparedStatement.setString(3, gender);
preparedStatement.setString(4, password);
preparedStatement.setString(5, email);
int rowsUpdated = preparedStatement.executeUpdate();
if (rowsUpdated > 0) {
System.out.println("The student's information has been updated successfully!");
} else {
System.out.println("Failed to update the student's information!");
}
scanner.close();
}
// 删除学生信息
private static void deleteStudent() throws SQLException {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the student's email to delete: ");
String email = scanner.nextLine();
// 从数据库中删除学生信息
String sql = "DELETE FROM students WHERE email = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, email);
int rowsDeleted = preparedStatement.executeUpdate();
if (rowsDeleted > 0) {
System.out.println("The student's information has been deleted successfully!");
} else {
System.out.println("Failed to delete the student's information!");
}
scanner.close();
}
// 查看所有学生信息
private static void viewAllStudents() throws SQLException {
List<Student> students = new ArrayList<Student>();
// 从数据库中获取所有学生信息
String sql = "SELECT * FROM students";
preparedStatement = connection.prepareStatement(sql);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
String gender = resultSet.getString("gender");
String email = resultSet.getString("email");
String password = resultSet.getString("password");
students.add(new Student(id, name, age, gender, email, password));
}
if (students.size() > 0) {
System.out.println("ID\tName\tAge\tGender\tEmail\t\t\tPassword");
for (Student student : students) {
System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t"
+ student.getGender() + "\t" + student.getEmail() + "\t" + student.getPassword());
}
} else {
System.out.println("No students found!");
}
}
}
class Student {
private int id;
private String name;
private int age;
private String gender;
private String email;
private String password;
public Student(int id, String name, int age, String gender, String email, String password) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.email = email;
this.password = password;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getEmail() {
return email;
}
public String getPassword() {
return password;
}
}
```
请注意,您需要将“your_password”替换为您的MySQL数据库密码。另外,此代码仅为演示目的,您需要根据您的需求进行修改和改进。
阅读全文