Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: PROBLEM CHECKING IF RECORDS EXIST OR DO NOT EXIST IN DATABASE

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PROBLEM CHECKING IF RECORDS EXIST OR DO NOT EXIST IN DATABASE

    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?


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: PROBLEM CHECKING IF RECORDS EXIST OR DO NOT EXIST IN DATABASE

    To be honest, I haven't understood what you're asking but...
    If studentID is stored in the DB as a String, try the following:
    statement.executeQuery("SELECT * FROM Results WHERE studentID = '" + studentID + "';");

    if (results.next())
    number = results.getInt(1);
    else
    number = -1;

    Just put an error message or flag in the else clause, as it means no result was found.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

Similar Threads

  1. Urgent - File exist nor working
    By Bagzli in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 2nd, 2011, 04:09 AM
  2. The Problem With My Code Is That It Doesn't Exist Yet. (Help w/ Retarded Teacher)
    By DylanWilliams92 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 27th, 2010, 09:10 PM
  3. Should validation code exist in set methods or in a validate method
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 4
    Last Post: May 27th, 2010, 09:37 AM
  4. org.apache.derby does not exist?
    By disclaimer in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 30th, 2010, 05:58 PM
  5. How to handle quadratic roots that don't exist?
    By kairojya in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2009, 01:21 PM