请给我学生宿舍管理系统的代码,用java+mysql,属性有学号,姓名,班级,学院,电话,苑,楼,宿舍号,我希望建Student类来管理属性,同时属性的变量名依次为sno,sname,class,college,phone,gardon,floor,room,功能为增删改查
时间: 2024-05-04 16:18:50 浏览: 100
这是一个简单的学生宿舍管理系统的代码,使用Java和MySQL。我使用了JDBC连接MySQL数据库,Student类用于管理学生属性,同时属性的变量名依次为sno,sname,class,college,phone,gardon,floor,room。系统的功能包括增加、删除、修改和查询学生信息。
```java
import java.sql.*;
public class Student {
private String sno;
private String sname;
private String sclass;
private String college;
private String phone;
private String gardon;
private String floor;
private String room;
public Student(String sno, String sname, String sclass, String college, String phone, String gardon, String floor, String room) {
this.sno = sno;
this.sname = sname;
this.sclass = sclass;
this.college = college;
this.phone = phone;
this.gardon = gardon;
this.floor = floor;
this.room = room;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSclass() {
return sclass;
}
public void setSclass(String sclass) {
this.sclass = sclass;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getGardon() {
return gardon;
}
public void setGardon(String gardon) {
this.gardon = gardon;
}
public String getFloor() {
return floor;
}
public void setFloor(String floor) {
this.floor = floor;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/student_db";
String user = "root";
String password = "password";
Connection conn = null;
Statement stmt = null;
try {
// 注册 JDBC 驱动器
Class.forName("com.mysql.jdbc.Driver");
// 打开连接
conn = DriverManager.getConnection(url, user, password);
// 执行查询
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM student";
ResultSet rs = stmt.executeQuery(sql);
// 处理结果集
while (rs.next()) {
String sno = rs.getString("sno");
String sname = rs.getString("sname");
String sclass = rs.getString("sclass");
String college = rs.getString("college");
String phone = rs.getString("phone");
String gardon = rs.getString("gardon");
String floor = rs.getString("floor");
String room = rs.getString("room");
Student student = new Student(sno, sname, sclass, college, phone, gardon, floor, room);
System.out.println(student);
}
// 关闭资源
rs.close();
stmt.close();
conn.close();
} catch (SQLException se) {
// 处理 JDBC 错误
se.printStackTrace();
} catch (Exception e) {
// 处理 Class.forName 错误
e.printStackTrace();
} finally {
// 关闭资源
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
@Override
public String toString() {
return "Student{" +
"sno='" + sno + '\'' +
", sname='" + sname + '\'' +
", sclass='" + sclass + '\'' +
", college='" + college + '\'' +
", phone='" + phone + '\'' +
", gardon='" + gardon + '\'' +
", floor='" + floor + '\'' +
", room='" + room + '\'' +
'}';
}
}
```
在这个例子中,我们使用了JDBC连接MySQL数据库,并创建了一个Student类来管理学生的属性,包括学号、姓名、班级、学院、电话、苑、楼和宿舍号。我们还实现了一个main方法来演示如何从数据库中查询学生信息,并打印出结果。
阅读全文