帮我写一个Java学生信息管理系统
时间: 2023-08-20 16:30:11 浏览: 107
好的,以下是一个简单的 Java 学生信息管理系统的实现。本系统使用 MySQL 数据库作为数据存储,使用 JDBC 连接数据库。实现的功能包括学生信息添加、查询、修改、删除等。
```
// Student.java
public class Student {
private int id;
private String name;
private int age;
private String gender;
private String major;
private String grade;
// 构造方法
public Student(int id, String name, int age, String gender, String major, String grade) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.major = major;
this.grade = grade;
}
// getter 和 setter 方法
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
```
```
// StudentDao.java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
private Connection conn = null;
// 构造方法,连接数据库
public StudentDao() {
String url = "jdbc:mysql://localhost:3306/student";
String user = "root";
String password = "123456";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 添加学生信息
public void addStudent(Student student) {
String sql = "INSERT INTO student_info (id, name, age, gender, major, grade) VALUES (?, ?, ?, ?, ?, ?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, student.getId());
ps.setString(2, student.getName());
ps.setInt(3, student.getAge());
ps.setString(4, student.getGender());
ps.setString(5, student.getMajor());
ps.setString(6, student.getGrade());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 根据学生 ID 查询学生信息
public Student getStudentById(int id) {
Student student = null;
String sql = "SELECT * FROM student_info WHERE id = ?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
student = new Student(rs.getInt("id"), rs.getString("name"), rs.getInt("age"),
rs.getString("gender"), rs.getString("major"), rs.getString("grade"));
}
rs.close();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
// 查询所有学生信息
public List<Student> getAllStudents() {
List<Student> students = new ArrayList<>();
String sql = "SELECT * FROM student_info";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Student student = new Student(rs.getInt("id"), rs.getString("name"), rs.getInt("age"),
rs.getString("gender"), rs.getString("major"), rs.getString("grade"));
students.add(student);
}
rs.close();
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
// 修改学生信息
public void updateStudent(Student student) {
String sql = "UPDATE student_info SET name = ?, age = ?, gender = ?, major = ?, grade = ? WHERE id = ?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setInt(2, student.getAge());
ps.setString(3, student.getGender());
ps.setString(4, student.getMajor());
ps.setString(5, student.getGrade());
ps.setInt(6, student.getId());
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 删除学生信息
public void deleteStudent(int id) {
String sql = "DELETE FROM student_info WHERE id = ?";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 关闭数据库连接
public void close() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
```
// StudentManagementSystem.java
import java.util.List;
import java.util.Scanner;
public class StudentManagementSystem {
private StudentDao dao = new StudentDao();
public void start() {
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag) {
System.out.println("学生信息管理系统");
System.out.println("1. 添加学生信息");
System.out.println("2. 查询学生信息");
System.out.println("3. 修改学生信息");
System.out.println("4. 删除学生信息");
System.out.println("5. 退出系统");
System.out.print("请选择操作:");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addStudent(scanner);
break;
case 2:
getStudent(scanner);
break;
case 3:
updateStudent(scanner);
break;
case 4:
deleteStudent(scanner);
break;
case 5:
flag = false;
break;
default:
System.out.println("输入错误,请重新选择");
break;
}
}
scanner.close();
dao.close();
System.out.println("学生信息管理系统已退出");
}
// 添加学生信息
private void addStudent(Scanner scanner) {
System.out.print("请输入学生 ID:");
int id = scanner.nextInt();
System.out.print("请输入学生姓名:");
String name = scanner.next();
System.out.print("请输入学生年龄:");
int age = scanner.nextInt();
System.out.print("请输入学生性别:");
String gender = scanner.next();
System.out.print("请输入学生专业:");
String major = scanner.next();
System.out.print("请输入学生年级:");
String grade = scanner.next();
Student student = new Student(id, name, age, gender, major, grade);
dao.addStudent(student);
System.out.println("添加学生信息成功");
}
// 查询学生信息
private void getStudent(Scanner scanner) {
System.out.print("请输入要查询的学生 ID:");
int id = scanner.nextInt();
Student student = dao.getStudentById(id);
if (student != null) {
System.out.println("学生信息如下:");
System.out.println("ID:" + student.getId());
System.out.println("姓名:" + student.getName());
System.out.println("年龄:" + student.getAge());
System.out.println("性别:" + student.getGender());
System.out.println("专业:" + student.getMajor());
System.out.println("年级:" + student.getGrade());
} else {
System.out.println("未找到该学生信息");
}
}
// 修改学生信息
private void updateStudent(Scanner scanner) {
System.out.print("请输入要修改的学生 ID:");
int id = scanner.nextInt();
Student student = dao.getStudentById(id);
if (student != null) {
System.out.println("原学生信息如下:");
System.out.println("ID:" + student.getId());
System.out.println("姓名:" + student.getName());
System.out.println("年龄:" + student.getAge());
System.out.println("性别:" + student.getGender());
System.out.println("专业:" + student.getMajor());
System.out.println("年级:" + student.getGrade());
System.out.println("请输入新的学生信息:");
System.out.print("姓名:");
String name = scanner.next();
System.out.print("年龄:");
int age = scanner.nextInt();
System.out.print("性别:");
String gender = scanner.next();
System.out.print("专业:");
String major = scanner.next();
System.out.print("年级:");
String grade = scanner.next();
student.setName(name);
student.setAge(age);
student.setGender(gender);
student.setMajor(major);
student.setGrade(grade);
dao.updateStudent(student);
System.out.println("修改学生信息成功");
} else {
System.out.println("未找到该学生信息");
}
}
// 删除学生信息
private void deleteStudent(Scanner scanner) {
System.out.print("请输入要删除的学生 ID:");
int id = scanner.nextInt();
dao.deleteStudent(id);
System.out.println("删除学生信息成功");
}
public static void main(String[] args) {
StudentManagementSystem system = new StudentManagementSystem();
system.start();
}
}
```
注意:本程序只是一个简单的示例,实际应用中还需要加入更多的异常处理、输入校验、界面美化等功能。
阅读全文