用java和mysql写学生宿舍管理系统
时间: 2023-12-02 12:02:43 浏览: 106
好的,下面是一个简单的学生宿舍管理系统的Java代码示例,使用MySQL作为数据库。
首先,需要创建一个MySQL数据库,包含以下表:
- 学生表(id,姓名,性别,年龄,宿舍号)
- 宿舍表(宿舍号,楼号,床位数)
- 入住记录表(id,学生id,宿舍号,入住时间)
然后,可以使用Java连接MySQL数据库,并实现以下功能:
1. 添加学生信息
```java
public void addStudent(String name, String gender, int age, int dormitoryNumber) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO students (name, gender, age, dormitory_number) VALUES (?, ?, ?, ?)");
stmt.setString(1, name);
stmt.setString(2, gender);
stmt.setInt(3, age);
stmt.setInt(4, dormitoryNumber);
stmt.executeUpdate();
stmt.close();
conn.close();
System.out.println("添加成功");
} catch (SQLException e) {
System.out.println("添加失败:" + e.getMessage());
}
}
```
2. 查询学生信息
```java
public void searchStudent(String name) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM students WHERE name LIKE ?");
stmt.setString(1, "%" + name + "%");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println("学号:" + rs.getInt("id") + ",姓名:" + rs.getString("name") + ",性别:" + rs.getString("gender") + ",年龄:" + rs.getInt("age") + ",宿舍号:" + rs.getInt("dormitory_number"));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.println("查询失败:" + e.getMessage());
}
}
```
3. 添加宿舍信息
```java
public void addDormitory(int dormitoryNumber, int buildingNumber, int bedNumber) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO dormitories (dormitory_number, building_number, bed_number) VALUES (?, ?, ?)");
stmt.setInt(1, dormitoryNumber);
stmt.setInt(2, buildingNumber);
stmt.setInt(3, bedNumber);
stmt.executeUpdate();
stmt.close();
conn.close();
System.out.println("添加成功");
} catch (SQLException e) {
System.out.println("添加失败:" + e.getMessage());
}
}
```
4. 查询宿舍信息
```java
public void searchDormitory(int dormitoryNumber) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM dormitories WHERE dormitory_number = ?");
stmt.setInt(1, dormitoryNumber);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println("宿舍号:" + rs.getInt("dormitory_number") + ",楼号:" + rs.getInt("building_number") + ",床位数:" + rs.getInt("bed_number"));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.println("查询失败:" + e.getMessage());
}
}
```
5. 学生入住宿舍
```java
public void checkIn(int studentId, int dormitoryNumber) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("INSERT INTO check_in_records (student_id, dormitory_number, check_in_time) VALUES (?, ?, NOW())");
stmt.setInt(1, studentId);
stmt.setInt(2, dormitoryNumber);
stmt.executeUpdate();
stmt.close();
conn.close();
System.out.println("入住成功");
} catch (SQLException e) {
System.out.println("入住失败:" + e.getMessage());
}
}
```
6. 查询宿舍入住情况
```java
public void searchCheckIn(int dormitoryNumber) {
try {
Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
PreparedStatement stmt = conn.prepareStatement("SELECT students.name, students.gender, students.age FROM check_in_records JOIN students ON check_in_records.student_id = students.id WHERE check_in_records.dormitory_number = ?");
stmt.setInt(1, dormitoryNumber);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
System.out.println("姓名:" + rs.getString("name") + ",性别:" + rs.getString("gender") + ",年龄:" + rs.getInt("age"));
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
System.out.println("查询失败:" + e.getMessage());
}
}
```
这只是一个简单的示例,实际情况可能更加复杂,需要根据具体需求进行适当的修改和扩展。
阅读全文