Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By mDennis10

Thread: Validating log in using database

  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Validating log in using database

    I am validating a user log in to a application here is the code:
    Java Code:
     //gets username & password user entered
           String username = request.getParameter ("username");
           String password = request.getParameter ("password");
           
           Statement statement = null;
           ResultSet result = null;
    
           try //connects to database to get password & username
           {
               statement = con.createStatement ();
               result = statement.executeQuery ("SELECT * FROM users WHERE userName='" + username +"' && password='" + password + "'");
               
               //cycles through result
               while (result.next ())
               {
                   String storedUsername = result.getString ("userName");
                   String storedPassword = result.getString ("password");
                                 
                   if (storedUsername.equals (username) && storedPassword.equals (password))
                       response.getWriter().println ("Hello " + username);
    
                   else
                      response.getWriter().println ("invalid login");
                     
               }//end while
              
           }
           catch (Exception e)
           {
               response.getWriter ().println (e.getMessage());
           }
           finally // closes Statement & ResultSet instances
           {
               try
               {
                   statement.close ();
                   result.close ();
               }
               catch (Exception e)
               {
                   response.getWriter ().println (e.getMessage ());
               }//end catch 
           }//end finally block
    the problem i am having is that due to the fact that i directly use the username and password in the query statement if the username and
    password does not exist in the database the else statement that tell the user that login is invalid will not execute. How can i fix this problem so that once the username or password is not contained in the database i can execute the appropriate code to notify the user

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Validating log in using database

    Use validation to make sure the username String and password String is not empty and long enough before you do the SQL query, if it doesn't pass the validation go directly to the error message.

  3. #3
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: Validating log in using database

    Quote Originally Posted by ozzyman View Post
    Use validation to make sure the username String and password String is not empty and long enough before you do the SQL query, if it doesn't pass the validation go directly to the error message.
    I think the problem that mDennis10 is having is that even if the username and password are supplied to the method, the database query may return no result. He wants "invalid login" to display when no records are found in the database.

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Validating log in using database

    Doesn't that happen anyway? If no records are found the conditional statement will return to the else bracket.

  5. #5
    subzero1012 is offline Member
    Join Date
    Feb 2012
    Location
    Phoenix, AZ
    Posts
    26
    Rep Power
    0

    Default Re: Validating log in using database

    Quote Originally Posted by ozzyman View Post
    Doesn't that happen anyway? If no records are found the conditional statement will return to the else bracket.
    The following is a quotation from the original problem statement.
    "if the username and
    password does not exist in the database the else statement that tell the user that login is invalid will not execute"

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Validating log in using database

    I read that but the problem is if that doesn't execute it means that:
    Java Code:
    response.getWriter().println ("Hello " + username);
    is executing instead, but the answer to that is too obvious - storedUsername can't equal username

    So mDennis10 must be getting a NullPointerException or something that he hasn't told us about, which means that the program is crashing when he tries to use the String.equals() method, so neither conditional statement is executing.

  7. #7
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Re: Validating log in using database

    Quote Originally Posted by ozzyman View Post
    Doesn't that happen anyway? If no records are found the conditional statement will return to the else bracket.
    Actually it doesn't because the resultset is empty when i call the next method in the while loop it doesn't go into the while block at all therefore the else doesn't get executed
    ozzyman likes this.

Similar Threads

  1. Help with validating input into an array
    By kcoltharp in forum New To Java
    Replies: 1
    Last Post: 02-25-2011, 09:05 AM
  2. Validating Jtable
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 02:54 PM
  3. validating account
    By ServletEst in forum Java Servlet
    Replies: 3
    Last Post: 08-24-2009, 01:11 PM
  4. Validating email address!
    By rameshraj in forum Advanced Java
    Replies: 1
    Last Post: 02-18-2008, 09:39 AM
  5. Validating email address
    By ravian in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •