public List<Course> search(String name, String teacher, String classroom) { String sql = "select * from course where "; if (name != "") { sql += "name like '%" + name + "%'"; } if (teacher != "") { sql += "teacher like '%" + teacher + "%'"; } if (classroom != "") { sql += "classroom like '%" + classroom + "%'"; } List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String teacher2 = rs.getString("teacher"); String classroom2 = rs.getString("classroom"); bean = new Course(id, name2, teacher2, classroom2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; }
时间: 2024-04-01 13:33:35 浏览: 76
这段代码是一个搜索课程的方法,根据传入的课程名称、教师、教室信息来查询符合条件的课程记录。方法中使用了SQL语句拼接的方式生成动态查询语句,根据传入的参数来拼接不同的查询条件。最后将查询结果封装成Course对象的集合返回。需要注意的是该代码存在SQL注入的风险,建议使用PreparedStatement来进行参数化查询,避免SQL注入攻击。
相关问题
import java.util.*;public class Schedule { // Map 用于存储课程安排信息 private Map<String, List<String>> schedule; public Schedule() { this.schedule = new HashMap<>(); } // 添加课程信息 public void addSchedule(String time, String course, String classroom) { if (schedule.containsKey(time)) { List<String> courses = schedule.get(time); if (courses.contains(course) || courses.contains(classroom)) { System.out.println("Error: Course or classroom already scheduled at this time."); } else { courses.add(course); courses.add(classroom); schedule.put(time, courses); System.out.println("Schedule added successfully."); } } else { List<String> courses = new ArrayList<>(); courses.add(course); courses.add(classroom); schedule.put(time, courses); System.out.println("Schedule added successfully."); } } // 获取课程安排信息 public List<String> getSchedule(String time) { if (schedule.containsKey(time)) { return schedule.get(time); } else { return null; } } public static void main(String[] args) { Schedule schedule = new Schedule(); schedule.addSchedule("Monday 9:00", "Math", "Room 101"); schedule.addSchedule("Monday 9:00", "Physics", "Room 102"); schedule.addSchedule("Monday 10:00", "English", "Room 103"); List<String> monday_9 = schedule.getSchedule("Monday 9:00"); System.out.println("Monday 9:00 schedule: " + monday_9.toString()); }}请将每一句代码解释一下
这是一个Java程序,名为Schedule,用于存储和获取课程安排信息。下面是代码的解释:
```java
import java.util.*; // 导入Java工具包
public class Schedule { // 定义一个名为Schedule的类
// Map 用于存储课程安排信息
private Map<String, List<String>> schedule;
// 类的构造函数,用于初始化schedule
public Schedule() {
this.schedule = new HashMap<>();
}
// 添加课程信息
public void addSchedule(String time, String course, String classroom) {
if (schedule.containsKey(time)) { // 判断时间是否已经被占用
List<String> courses = schedule.get(time); // 获取该时间段已经占用的课程信息
if (courses.contains(course) || courses.contains(classroom)) {
// 判断该课程或教室是否已经被占用
System.out.println("Error: Course or classroom already scheduled at this time.");
} else {
courses.add(course);
courses.add(classroom);
schedule.put(time, courses); // 将课程信息添加到schedule中
System.out.println("Schedule added successfully.");
}
} else {
List<String> courses = new ArrayList<>();
courses.add(course);
courses.add(classroom);
schedule.put(time, courses); // 将课程信息添加到schedule中
System.out.println("Schedule added successfully.");
}
}
// 获取课程安排信息
public List<String> getSchedule(String time) {
if (schedule.containsKey(time)) {
return schedule.get(time); // 返回该时间段的课程信息
} else {
return null; // 如果该时间段没有课程信息,则返回null
}
}
// 主函数
public static void main(String[] args) {
Schedule schedule = new Schedule(); // 创建一个Schedule对象
schedule.addSchedule("Monday 9:00", "Math", "Room 101"); // 添加课程信息
schedule.addSchedule("Monday 9:00", "Physics", "Room 102");
schedule.addSchedule("Monday 10:00", "English", "Room 103");
List<String> monday_9 = schedule.getSchedule("Monday 9:00"); // 获取课程信息
System.out.println("Monday 9:00 schedule: " + monday_9.toString()); // 打印课程信息
}
}
```
阅读全文