我有一些代码,我希望能够在允许学生注册之前运行以检查课程是否已满。我使用String currentNumberInClass创建了classFullCheck()函数,该函数从sql表中获取计数并将其与另一个表中的class_size进行比较。我想也许我正在返回class_id而不是count(*)号,所以这不能正常工作。我也不确定将classFullCheck()函数插入addClass()函数的位置,以使此检查正常工作。
任何建议都将不胜感激!
public static void classFullCheck() {
try {
String currentNumberInClass = ("SELECT class_id, COUNT(*) FROM ClassSelector.student_x_class WHERE class_id = " + selectedClass);
rs = myStmt.executeQuery(currentNumberInClass);
String classSize = ("SELECT class_size FROM ClassSelector.classes WHERE class_id = " + selectedClass);
rs = myStmt.executeQuery(classSize);
if (currentNumberInClass.compareTo(classSize) >= 0){
System.out.println("Sorry, this class is Full!");
}
} catch (java.sql.SQLException SQL) {
SQL.printStackTrace();
}
}
public static void addClass() {
try {
rs = myStmt.executeQuery("SELECT * FROM ClassSelector.classes");
while (rs.next()) {
String availableClasses = rs.getString("class_id") + "\t" + rs.getString("class_name") + "\t" + rs.getString("description");
System.out.println(availableClasses);
}
System.out.println("Enter Class ID from Classes Listed Above to Join: ");
selectedClass = sc.nextLine();
rs = myStmt.executeQuery("SELECT * FROM ClassSelector.classes WHERE class_id = " + selectedClass);
while (rs.next()) {
String innerJoin = (userEnterIdAsName + " has been added to " + rs.getString("class_name") + " " + rs.getString("class_id"));
System.out.println(innerJoin);
String student_x_classJoin = "INSERT INTO student_x_class" + "(student_id, student_name, class_id, class_name)" + "VALUES (?, ?, ?, ?)";
PreparedStatement pStmt = con.prepareStatement(student_x_classJoin);
pStmt.setString(1, user_entered_student_id);
pStmt.setString(2, userEnterIdAsName);
pStmt.setString(3, rs.getString("class_id"));
pStmt.setString(4, rs.getString("class_name"));
pStmt.executeUpdate();
System.out.println("Would you like to enroll " + userEnterIdAsName + " into another class? (Y/N)");
String addAdditionalClass = sc.nextLine();
if (addAdditionalClass.equalsIgnoreCase("Y")) {
addClass();
} else if (addAdditionalClass.equalsIgnoreCase("N")) {
return;
}
}
}
catch (java.sql.SQLException SQL) {
System.out.println("Wait, This Student is already enrolled in this class!");
}
}
答案 0 :(得分:0)
你不需要在classFullCheck()中做两个Query 您可以通过以下查询执行此操作:
"SELECT claclass, class_size, (SELECT COUNT(1)
FROM ClassSelector.student_x_class WHERE class_id = c.class_id ) as current_student_count
FROM ClassSelector.classes c WHERE class_id = " + selectedClass
当current_student_count< class_size。 但更好的解决方案是打印列表类,其中包含class_size>的学生数量。 current_student_count做一个选择。
答案 1 :(得分:0)
因此在java jdbc中,您可以通过以下方式从sql中检索返回的结果。
public boolean isClassFull(int classId) {
// Sql to get count
String classCountSql = "SELECT Count(*) as classCount FROM ClassSelector.student_x_class WHERE class_id = ?;";
// Sql to get max count for a class
String classSizeSql = "SELECT class_size as classSize FROM ClassSelector.classes WHERE class_id = ?;";
int classCount = 0;
int classSize = 0;
try(Connection connection = databaseConnection.getConnection()) {
try(PreparedStatement preparedStatement = connection.prepareStatement(classCountSql)) {
preparedStatement.setInt(1, classId);
try(ResultSet resultSet = preparedStatement.executeQuery()) {
if(resultSet.next()){
classCount = resultSet.getInt("classCount");
}
}
}
try(PreparedStatement preparedStatement = connection.prepareStatement(classSizeSql)) {
preparedStatement.setInt(1, classId);
try(ResultSet resultSet = preparedStatement.executeQuery()) {
if(resultSet.next()){
classCount = resultSet.getInt("classSize");
}
}
}
} catch(SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return classCount >= classSize;
}