0

正如标题所说。当我尝试输入一个不在范围内的值时,我无法返回一个显示“未找到”的值。

PS:我是新来的,所以要温柔。呵呵

public static void checkStatus(String ID_No) throws SQLException{
    try{
    ResultSet rs;
    String validStatus = "SELECT * FROM validation";
    st = connection.createStatement();
    rs = st.executeQuery(validStatus);

        while(rs.next()){
            getStudValid = rs.getString("ID_No");
            getValidStatus = rs.getString("Validation");
                if (!getValidStatus.equals("Accepted") && getStudValid.equals(ID_No)){
                    System.out.println("Student " + getStudValid + " Please complete the required pre-requisite processes.");
                } else if (getValidStatus.equals("Accepted") && getStudValid.equals(ID_No)){
                    System.out.println("Student " + getStudValid + " You are Enrolled!");
                } 
        } 

    rs.close();
    } catch(SQLException e){
        System.out.println("Updated "+ ID_No);

    }
}
4

4 回答 4

0

把它if-else condition放在一个返回 a 的方法中String "NOT FOUND"

改变

public static void checkStatus(String ID_No) throws SQLException{

public static String checkStatus(String ID_No) throws SQLException{

然后return a String whether successful or "Not Found"

于 2013-08-29T17:15:12.087 回答
0

你可以开发像这样的逻辑 -

boolean found = false;
while(rs.next()){
   getStudValid = rs.getString("ID_No");
   getValidStatus = rs.getString("Validation");
   if(getStudValid.equals(ID_No)){
        if(getValidStatus.equals("Accepted"))
            System.out.println("Student " + getStudValid + " You are Enrolled!");
        else
            System.out.println("Student " + getStudValid + " Please complete the required pre-requisite processes.");
        found =true;
   }
}
if(!found)
   System.out.println("Student "+ID_No+" Not found");

但我建议您通过优化查询来优化此代码 -SELECT * FROM validation where ID_No=?并使用PrepareStatement.

String validStatus = "SELECT * FROM validation where ID_No=?";
pst = connection.prepareStatement(validStatus);
pst.setString(1,Id_No);
rs = pst..executeQuery();

if(rs.next()){
    if(getValidStatus.equals("Accepted"))
          System.out.println("Student " + getStudValid + " You are Enrolled!");
    else
          System.out.println("Student " + getStudValid + " Please complete the required pre-requisite processes.");
}else
    System.out.println("Student "+ID_No+" Not found");

一些相关的话题——

于 2013-08-29T17:17:25.043 回答
0

因此,基本上当 rs.next 成立时,这意味着在执行查询时会找到某种值。

RS.next 返回一个布尔值,使用该布尔值你可以做这样的事情。

boolean isFound = rs.next();

if(isFound)
{
   //  your while loop
   //  at the end of your while loop add isFound = rs.next()
}
else
{
   return "Not Found";
}
于 2013-08-29T17:17:52.313 回答
0

这取决于您在此处找不到学生时想要发生的事情,有两种方法可以做到。

抛出异常(如果学生不存在,这将是一个错误)

try {
    checkStatus("studentA");
} catch(StudentNotFoundException e) {
    System.out.println("Student wasnt found!");
}

public static void checkStatus(String ID_No) throws SQLException, StudentNotFoundException {
    try{
        ResultSet rs;
        String validStatus = "SELECT * FROM validation";
        st = connection.createStatement();
        rs = st.executeQuery(validStatus);

            while(rs.next()){
                getStudValid = rs.getString("ID_No");
                getValidStatus = rs.getString("Validation");
                    if (!getValidStatus.equals("Accepted") && getStudValid.equals(ID_No)){
                        System.out.println("Student " + getStudValid + " Please complete the required pre-requisite processes.");
                        return;
                    } else if (getValidStatus.equals("Accepted") && getStudValid.equals(ID_No)){
                        System.out.println("Student " + getStudValid + " You are Enrolled!");
                        return;
                    } 
            } 

        throw new StudentNotFoundException(); // create this exception eg: class StudentNotFoundException extends Exception
        rs.close();
    } catch(SQLException e){
        System.out.println("Updated "+ ID_No);

    }
}

或者你可以返回一个值

int exists = checkStatus("studentA");
if(exists == 0)
        System.out.println("Student wasnt found");

public static int checkStatus(String ID_No) throws SQLException{
    try{
        ResultSet rs;
        String validStatus = "SELECT * FROM validation";
        st = connection.createStatement();
        rs = st.executeQuery(validStatus);

            while(rs.next()){
                getStudValid = rs.getString("ID_No");
                getValidStatus = rs.getString("Validation");
                    if (!getValidStatus.equals("Accepted") && getStudValid.equals(ID_No)){
                        System.out.println("Student " + getStudValid + " Please complete the required pre-requisite processes.");
                        return 1; // found
                    } else if (getValidStatus.equals("Accepted") && getStudValid.equals(ID_No)){
                        System.out.println("Student " + getStudValid + " You are Enrolled!");
                        return 1; // found
                    } 
            } 

        return 0; // no student found
        rs.close();
    } catch(SQLException e){
        System.out.println("Updated "+ ID_No);
        return -1; // error occured
    }
}
于 2013-08-29T17:19:00.610 回答