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

学生がここに見つからない場合にどうしたいかによって異なりますが、それを行う方法は 2 つあります。

例外をスローします (学生が存在しない場合はエラーになります)

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 に答える
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 に答える