0

As the title says. I have trouble in Returning a value that says "Not Found", When i try to input a value that is not in range.

PS: I'm new here so be gentle. hehe

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);

    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    I don't understand the question – Kevin DiTraglia Aug 29 '13 at 17:15
  • I'm sorry for the confusion, I am too bewildered by the problem. I tried Inserting some code and this is what it looks like.: Input Student No: 201200002 Student Not Found Student 201200002 You are Enrolled! Student Not Found Student Not Found Student Not Found – JustAnotherLeech Aug 29 '13 at 17:17
  • 'catch(SQLException e){ System.out.println("Updated "+ ID_No); }' thats weird . it is caught means UPDATE NOT DONE – Srinath Ganesh Aug 29 '13 at 17:19
  • @Srinath i did some tweaks in the main code so that's no problem. I only have problem on the If-Else statement :> but thanks for caring – JustAnotherLeech Aug 29 '13 at 17:25
  • I rolled back your edit that 'fixed' your code. Including the solution into your question does not help people looking at this question. If an answer solved your problem, then either accept that answer, or post your own solution as an answer. In this case as you credited _Subhrajyoti Majumder_ in your edit, you should accept his answer. – Mark Rotteveel Aug 30 '13 at 07:20

4 Answers4

0

Take that if-else condition in a method which returns a String "NOT FOUND".

Change

public static void checkStatus(String ID_No) throws SQLException{

to

public static String checkStatus(String ID_No) throws SQLException{

then return a String whether successful or "Not Found"

JNL
  • 4,683
  • 18
  • 29
0

You could develop logic like -

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");

But I'd recommend you to optimize this code by optimizing your query - SELECT * FROM validation where ID_No=? and also use 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");

Some related topics -

Community
  • 1
  • 1
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0

So, essentially when rs.next holds, then that means that there is some sort of value that is found when the query is executed.

RS.next returns a boolean, and using that boolean you can do something like this.

boolean isFound = rs.next();

if(isFound)
{
   //  your while loop
   //  at the end of your while loop add isFound = rs.next()
}
else
{
   return "Not Found";
}
Kevin
  • 3,509
  • 4
  • 31
  • 45
0

It depends on what you want to happen when a student is not found here is 2 ways you could do it.

Throw an exception (this would be if it is an error if a student doesnt exist)

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);

    }
}

Or you could return a value

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
    }
}
ug_
  • 11,267
  • 2
  • 35
  • 52