10月23日
项目名称:新课程添加
(1)新课程信息必须唯一,如有重复,提示用户“课程名称
重复,重新录入”;
(2)要求判断任课教师为王建民、刘立嘉、刘丹、杨子光、
张云霞、武勇亮、高飞、孙静八位教师的其中一位。
(3)要求上课地点开头为“一教、二教、三教、基教”中的
一种;
(4)将新课程信息添加入库。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class CourseManager {
// MySQL 数据库连接信息
private static final String DB_URL = "jdbc:mysql://localhost:3306/school_db?useSSL=false&serverTimezone=UTC";
private static final String DB_USER = "root"; // 数据库用户名
private static final String DB_PASSWORD = "password"; // 数据库密码
// 有效教师名单
private static final String[] VALID_TEACHERS = {"王建民", "刘立嘉", "刘丹", "杨子光", "张云霞", "武勇亮", "高飞", "孙静"};
// 有效上课地点前缀
private static final String[] VALID_LOCATIONS = {"一教", "二教", "三教", "基教"};
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 录入课程信息
System.out.println("请输入课程名称:");
String courseName = scanner.nextLine();
System.out.println("请输入任课教师:");
String teacherName = scanner.nextLine();
System.out.println("请输入上课地点:");
String location = scanner.nextLine();
scanner.close();
// 验证教师姓名
if (!isValidTeacher(teacherName)) {
System.out.println("错误:任课教师必须是王建民、刘立嘉、刘丹、杨子光、张云霞、武勇亮、高飞、孙静中的一位。");
return;
}
// 验证上课地点
if (!isValidLocation(location)) {
System.out.println("错误:上课地点必须以'一教'、'二教'、'三教'、'基教'开头。");
return;
}
// 验证课程名称唯一性
if (isCourseExists(courseName)) {
System.out.println("课程名称重复,重新录入");
return;
}
// 将新课程信息添加入库
if (addCourseToDB(courseName, teacherName, location)) {
System.out.println("课程信息添加成功!");
} else {
System.out.println("课程信息添加失败!");
}
}
// 验证教师姓名是否合法
private static boolean isValidTeacher(String teacherName) {
for (String validTeacher : VALID_TEACHERS) {
if (validTeacher.equals(teacherName)) {
return true;
}
}
return false;
}
// 验证上课地点是否合法
private static boolean isValidLocation(String location) {
for (String validLocation : VALID_LOCATIONS) {
if (location.startsWith(validLocation)) {
return true;
}
}
return false;
}
// 检查课程名称是否已经存在
private static boolean isCourseExists(String courseName) {
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String query = "SELECT COUNT(*) FROM courses WHERE course_name = ?";
PreparedStatement statement = conn.prepareStatement(query);
statement.setString(1, courseName);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
return resultSet.getInt(1) > 0;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
// 将新课程添加到数据库
private static boolean addCourseToDB(String courseName, String teacherName, String location) {
try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
String insertQuery = "INSERT INTO courses (course_name, teacher_name, location) VALUES (?, ?, ?)";
PreparedStatement statement = conn.prepareStatement(insertQuery);
statement.setString(1, courseName);
statement.setString(2, teacherName);
statement.setString(3, location);
int rowsInserted = statement.executeUpdate();
return rowsInserted > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}