Hi everyone,
I'm trying to make a client - server program where the server goes through a java bean which processes queries to access the the database for the releveant query information entered by the client. I have had no trouble creating the bean, client or server its just I cant seem to figure out how to see if a student ID exists in the database or not. Ive attempted to create this but its not working and i cant seem to figure out why. I want the client to enter a student ID it wants to update so that it is then passed onto the server which then accesses the bean method to check if the student ID exists in the database or not. This information should then be passed back from the server to the client stating if it exists or not. If it doesnt exist display an error message or if it does, enable the client to update the mark of that valid student ID
Here is my code for both client and server as well as the bean. These methods are just for updating a student ID
public static void updateMark() throws ClassNotFoundException, SQLException { System.out.print("\n** Option 4 selected **\n"); System.out.print( "\nEnter a valid Student ID to update: "); studentID = keyboard.nextInt(); keyboard.nextLine(); networkOutput.println(studentID); if(studentID == -1) { response = networkInput.nextLine(); System.out.println("\n" + response); } else { System.out.print("\nEnter new mark: "); int mark = keyboard.nextInt(); keyboard.nextLine(); networkOutput.println(mark); response = networkInput.nextLine(); System.out.println("\n" + response); } }
Server
public void updateMark() { try { System.out.println("\nSERVER: Option 4 selected!"); examBean = new ExamBean(); studentID = networkInput.nextInt(); networkInput.nextLine(); mark = networkInput.nextInt(); networkInput.nextLine(); number = examBean.checkStudentID(studentID); if(number == -1) { networkOutput.println( "** Student ID does not exist! **"); } else { networkOutput.println( "** Student ID has been updated **"); examBean.setUpdateStudent(studentID, mark); } } catch(ClassNotFoundException X) { } catch(SQLException X) { } }
JavaBean
public int checkStudentID(int studentID) throws SQLException, ClassNotFoundException { int number; query = ("SELECT * FROM Results" + " WHERE studentID = " + studentID); connectAndCreateStatement(); results = statement.executeQuery(query); if (results.next()) number = results.getInt(1); else number = -1; connection.close(); return number; }
What it does at the minute is it lets the client enter a mark even if it does not exist but displays that it doesnt exist after the client has entered a mark. I want it to display an error message after the student ID has been entered and return to the option menu not allowing them to enter a mark if it does not exist. Please help?