假定有n个实验室,每个实验室有不同类型和不同数量的仪器。 功能要求: 1) 能够对实验室、仪器信息进行增、改、查、浏览; 2) 教师根据需要按照条件进行教室查询; 3) 教室根据需要能够按照条件进行实验室申请; 4) 使用者可以对仪器使用时的故障进行登记。
时间: 2024-03-15 19:44:20 浏览: 147
软件需求课设高校实验室信息标准管理系统需求分析.doc
好的,根据您的需求,以下是一个简单的实验室管理系统的Java代码示例,供您参考:
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class LabManagementSystem {
private Connection conn;
public LabManagementSystem() {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/lab_management_system";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
// 增加实验室
public boolean addLab(Lab lab) {
String sql = "INSERT INTO lab (name, location, capacity) VALUES (?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, lab.getName());
pstmt.setString(2, lab.getLocation());
pstmt.setInt(3, lab.getCapacity());
int result = pstmt.executeUpdate();
pstmt.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 修改实验室
public boolean updateLab(Lab lab) {
String sql = "UPDATE lab SET location=?, capacity=? WHERE name=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, lab.getLocation());
pstmt.setInt(2, lab.getCapacity());
pstmt.setString(3, lab.getName());
int result = pstmt.executeUpdate();
pstmt.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 删除实验室
public boolean deleteLab(String name) {
String sql = "DELETE FROM lab WHERE name=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
int result = pstmt.executeUpdate();
pstmt.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 查询实验室
public Lab getLabByName(String name) {
String sql = "SELECT * FROM lab WHERE name=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
Lab lab = null;
if (rs.next()) {
lab = new Lab();
lab.setName(rs.getString("name"));
lab.setLocation(rs.getString("location"));
lab.setCapacity(rs.getInt("capacity"));
}
rs.close();
pstmt.close();
return lab;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
// 浏览实验室
public List<Lab> getAllLabs() {
String sql = "SELECT * FROM lab";
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
List<Lab> labs = new ArrayList<>();
while (rs.next()) {
Lab lab = new Lab();
lab.setName(rs.getString("name"));
lab.setLocation(rs.getString("location"));
lab.setCapacity(rs.getInt("capacity"));
labs.add(lab);
}
rs.close();
stmt.close();
return labs;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
// 查询实验室
public List<Lab> getLabsByCondition(String location, int minCapacity) {
String sql = "SELECT * FROM lab WHERE location=? AND capacity>=?";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, location);
pstmt.setInt(2, minCapacity);
ResultSet rs = pstmt.executeQuery();
List<Lab> labs = new ArrayList<>();
while (rs.next()) {
Lab lab = new Lab();
lab.setName(rs.getString("name"));
lab.setLocation(rs.getString("location"));
lab.setCapacity(rs.getInt("capacity"));
labs.add(lab);
}
rs.close();
pstmt.close();
return labs;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
// 实验室申请
public boolean applyLab(String name, String teacher, Date date, Time startTime, Time endTime) {
String sql = "INSERT INTO lab_application (lab_name, teacher, date, start_time, end_time) VALUES (?, ?, ?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, name);
pstmt.setString(2, teacher);
pstmt.setDate(3, date);
pstmt.setTime(4, startTime);
pstmt.setTime(5, endTime);
int result = pstmt.executeUpdate();
pstmt.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
// 登记故障
public boolean addFault(String deviceName, String description, Date date) {
String sql = "INSERT INTO device_fault (device_name, description, date) VALUES (?, ?, ?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, deviceName);
pstmt.setString(2, description);
pstmt.setDate(3, date);
int result = pstmt.executeUpdate();
pstmt.close();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public static void main(String[] args) {
LabManagementSystem lms = new LabManagementSystem();
// 增加实验室
Lab lab1 = new Lab("Lab1", "Building A, Room 101", 30);
lms.addLab(lab1);
// 修改实验室
Lab lab2 = new Lab("Lab2", "Building B, Room 201", 50);
lms.updateLab(lab2);
// 删除实验室
lms.deleteLab("Lab3");
// 查询实验室
Lab lab3 = lms.getLabByName("Lab1");
System.out.println(lab3.getName() + " " + lab3.getLocation() + " " + lab3.getCapacity());
// 浏览实验室
List<Lab> labs = lms.getAllLabs();
for (Lab lab : labs) {
System.out.println(lab.getName() + " " + lab.getLocation() + " " + lab.getCapacity());
}
// 按条件查询实验室
List<Lab> labs2 = lms.getLabsByCondition("Building A, Room 101", 40);
for (Lab lab : labs2) {
System.out.println(lab.getName() + " " + lab.getLocation() + " " + lab.getCapacity());
}
// 实验室申请
lms.applyLab("Lab1", "张三", Date.valueOf("2022-06-01"), Time.valueOf("08:00:00"), Time.valueOf("12:00:00"));
// 登记故障
lms.addFault("Device1", "设备无法启动", Date.valueOf("2022-06-01"));
}
}
class Lab {
private String name;
private String location;
private int capacity;
public Lab() {
}
public Lab(String name, String location, int capacity) {
this.name = name;
this.location = location;
this.capacity = capacity;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
}
```
上述代码演示了如何使用Java语言连接MySQL数据库,实现实验室的增删改查、按条件查询、实验室申请和故障登记等功能。您可以根据自己的需求和技术水平进行修改和扩展。
阅读全文