用Java写一个宿舍管理员对学生信息进行增删改查的系统
时间: 2024-01-01 22:05:27 浏览: 84
以下是一个基于Java的简单宿舍管理员系统示例,演示如何使用面向对象的思想实现对学生信息进行增删改查的功能。本系统使用MySQL数据库存储学生信息,需要使用JDBC连接数据库。
1. 创建学生类
```java
public class Student {
private String id; // 学号
private String name; // 姓名
private int age; // 年龄
private String gender; // 性别
private String dorm; // 宿舍号
public Student() {}
public Student(String id, String name, int age, String gender, String dorm) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.dorm = dorm;
}
// getter和setter方法省略
public String toString() {
return "学号:" + id + ",姓名:" + name + ",年龄:" + age + ",性别:" + gender + ",宿舍号:" + dorm;
}
}
```
2. 创建学生DAO类,实现对学生信息的增删改查操作
```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;
public class StudentDAO {
private String url = "jdbc:mysql://localhost:3306/dbname"; // 数据库连接地址
private String username = "root"; // 数据库用户名
private String password = "123456"; // 数据库密码
// 添加学生信息
public void addStudent(Student student) {
Connection conn = null;
PreparedStatement ps = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 插入学生信息
String sql = "INSERT INTO student(id, name, age, gender, dorm) VALUES(?, ?, ?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, student.getId());
ps.setString(2, student.getName());
ps.setInt(3, student.getAge());
ps.setString(4, student.getGender());
ps.setString(5, student.getDorm());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 删除学生信息
public void deleteStudent(String id) {
Connection conn = null;
PreparedStatement ps = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 删除学生信息
String sql = "DELETE FROM student WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 修改学生信息
public void updateStudent(Student student) {
Connection conn = null;
PreparedStatement ps = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 更新学生信息
String sql = "UPDATE student SET name=?, age=?, gender=?, dorm=? WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, student.getName());
ps.setInt(2, student.getAge());
ps.setString(3, student.getGender());
ps.setString(4, student.getDorm());
ps.setString(5, student.getId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 查询学生信息
public List<Student> findStudentById(String id) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Student> students = new ArrayList<Student>();
try {
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 查询学生信息
String sql = "SELECT * FROM student WHERE id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, id);
rs = ps.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
student.setDorm(rs.getString("dorm"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return students;
}
// 查询所有学生信息
public List<Student> findAllStudents() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
List<Student> students = new ArrayList<Student>();
try {
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 查询学生信息
String sql = "SELECT * FROM student";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
Student student = new Student();
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setAge(rs.getInt("age"));
student.setGender(rs.getString("gender"));
student.setDorm(rs.getString("dorm"));
students.add(student);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
try {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return students;
}
}
```
3. 创建宿舍管理员界面,使用控制台进行交互
```java
import java.util.List;
import java.util.Scanner;
public class DormManager {
private StudentDAO studentDAO = new StudentDAO();
public static void main(String[] args) {
DormManager dormManager = new DormManager();
dormManager.showMenu();
}
// 显示主菜单
public void showMenu() {
Scanner scanner = new Scanner(System.in);
while (true) {
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("请选择操作(输入数字):");
String choice = scanner.nextLine();
switch (choice) {
case "1":
addStudent();
break;
case "2":
deleteStudent();
break;
case "3":
updateStudent();
break;
case "4":
findStudent();
break;
case "5":
System.out.println("谢谢使用,再见!");
System.exit(0);
default:
System.out.println("输入有误,请重新输入!");
}
}
}
// 添加学生信息
public void addStudent() {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入学生信息(学号 姓名 年龄 性别 宿舍号):");
String[] infos = scanner.nextLine().split(" ");
Student student = new Student(infos[0], infos[1], Integer.parseInt(infos[2]), infos[3], infos[4]);
studentDAO.addStudent(student);
System.out.println("学生信息添加成功!");
}
// 删除学生信息
public void deleteStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要删除的学生学号:");
String id = scanner.nextLine();
List<Student> students = studentDAO.findStudentById(id);
if (students.size() == 0) {
System.out.println("没有找到该学生信息!");
} else {
studentDAO.deleteStudent(id);
System.out.println("学生信息删除成功!");
}
}
// 修改学生信息
public void updateStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要修改的学生学号:");
String id = scanner.nextLine();
List<Student> students = studentDAO.findStudentById(id);
if (students.size() == 0) {
System.out.println("没有找到该学生信息!");
} else {
System.out.println("请输入要修改的学生信息(姓名 年龄 性别 宿舍号):");
String[] infos = scanner.nextLine().split(" ");
Student student = new Student(id, infos[0], Integer.parseInt(infos[1]), infos[2], infos[3]);
studentDAO.updateStudent(student);
System.out.println("学生信息修改成功!");
}
}
// 查询学生信息
public void findStudent() {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要查询的学生学号(输入0查询所有):");
String id = scanner.nextLine();
List<Student> students;
if (id.equals("0")) {
students = studentDAO.findAllStudents();
} else {
students = studentDAO.findStudentById(id);
}
if (students.size() == 0) {
System.out.println("没有找到该学生信息!");
} else {
for (Student student : students) {
System.out.println(student);
}
}
}
}
```
运行宿舍管理员系统,即可进行学生信息的增删改查操作。
阅读全文