该方法未正确检查值

the method does not check the value properly

static boolean checkCode(String Code, Connection conn) throws SQLException {
        Statement s; 
        String cc = null;
        try {
            String Statement = "SELECT Code from Courses where Code="+ Code;
            s = conn.createStatement();
            ResultSet rs = s.executeQuery(Statement);
            while (rs.next()) {
                cc = rs.getString("Code");   
            }

            if((Code.equalsIgnoreCase(cc)))
                return true;

            else
               return false;

        }
        catch (SQLException e) {} 
        return false;
    } 

我正在使用 switch case 并且 3 个 case 不能正常工作(使用课程代码删除,使用课程代码更新,使用课程代码查看特定课程)所以我认为 checkCode 方法中的错误。有人可以帮忙吗?

您选择的值等于其自身。我只会得到匹配记录的计数。接下来,您不会关闭您在方法中打开的任何资源。而且,您正在默默地吞下发生的任何异常。最后,您没有使用 PreparedStatement,因此您的查询(至少乍一看)容易受到 sql 注入的攻击。

有点像,

static boolean checkCode(String Code, Connection conn) throws SQLException {
    String query = "SELECT count(*) from Courses where Code=?";
    try (PreparedStatement ps = conn.prepareStatement(query)) {
        ps.setString(1, Code);
        try (ResultSet rs = ps.executeQuery()) {
            if (rs.next()) {
                return rs.getInt(1) > 0;
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}