用java帮我写一个基于数据库的宿舍管理系统
时间: 2024-05-12 09:12:51 浏览: 16
好的,以下是一个基于数据库的宿舍管理系统的Java代码示例:
```java
// 导入所需的包
import java.sql.*;
import java.util.Scanner;
public class DormitoryManagementSystem {
// 定义数据库连接信息
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/dormitory?useSSL=false";
static final String USER = "root";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
Scanner input = new Scanner(System.in);
try {
// 注册 JDBC 驱动器
Class.forName(JDBC_DRIVER);
// 打开一个连接
System.out.println("连接到数据库...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// 执行 SQL 查询
stmt = conn.createStatement();
String sql = "SELECT * FROM student";
ResultSet rs = stmt.executeQuery(sql);
// 输出查询结果
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
String gender = rs.getString("gender");
String dorm = rs.getString("dorm");
System.out.println("ID: " + id + ", 姓名: " + name + ", 年龄: " + age + ", 性别: " + gender + ", 宿舍: " + dorm);
}
// 添加新学生
System.out.println("请输入要添加的学生姓名:");
String name = input.nextLine();
System.out.println("请输入要添加的学生年龄:");
int age = input.nextInt();
input.nextLine(); // 清空输入缓冲区
System.out.println("请输入要添加的学生性别:");
String gender = input.nextLine();
System.out.println("请输入要添加的学生宿舍:");
String dorm = input.nextLine();
sql = "INSERT INTO student (name, age, gender, dorm) VALUES ('" + name + "', " + age + ", '" + gender + "', '" + dorm + "')";
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("添加学生成功!");
} else {
System.out.println("添加学生失败!");
}
// 更新学生信息
System.out.println("请输入要更新信息的学生ID:");
int id = input.nextInt();
input.nextLine(); // 清空输入缓冲区
System.out.println("请输入要更新的学生宿舍:");
dorm = input.nextLine();
sql = "UPDATE student SET dorm='" + dorm + "' WHERE id=" + id;
rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("更新学生信息成功!");
} else {
System.out.println("更新学生信息失败!");
}
// 删除学生
System.out.println("请输入要删除的学生ID:");
id = input.nextInt();
input.nextLine(); // 清空输入缓冲区
sql = "DELETE FROM student WHERE id=" + id;
rows = stmt.executeUpdate(sql);
if (rows > 0) {
System.out.println("删除学生成功!");
} else {
System.out.println("删除学生失败!");
}
// 关闭资源
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
// 处理异常
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("程序执行完毕!");
}
}
```
这个示例程序可以实现以下功能:
- 显示所有学生的信息
- 添加新学生
- 更新学生信息(只更新宿舍信息)
- 删除学生
需要注意的是,为了让这个程序正常工作,需要先在MySQL数据库中创建一个名为`dormitory`的数据库,并在其中创建一个名为`student`的表,表结构如下:
```sql
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` int(11) NOT NULL,
`gender` varchar(10) NOT NULL,
`dorm` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
同时,需要将代码中的`USER`和`PASS`变量修改为MySQL数据库的用户名和密码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)