Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Question Login Authentication with MYSQL

    hi I'm using Jframe form what code if you logged-in as admin show menu1 and logged-in as staff show menu2??



    here's the table

    tblusers

    username
    password


    and the code




    Java Code:
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
       try{
           String sql="SELECT * FROM tblusers WHERE username=? AND password=?";
           pst=conn.prepareStatement(sql);
           pst.setString(1,txtUsername.getText());
           pst.setString(2,txtPass.getText());
           rs=pst.executeQuery();
           
           if(rs.next()){
               sql ="SELECT * FROM tblusers WHERE username=? AND password=? AND usertype=Administrator";   
              JOptionPane.showMessageDialog(null,"Welcome Admin");
              frmMenu show =  new frmMenu();
              show.setVisible(true);
              this.setVisible(false);
          }
           
           else if(rs.next()){
               sql ="SELECT * FROM tblusers WHERE username=? AND password=? AND usertype=Staff";   
              JOptionPane.showMessageDialog(null,"Welcome Admin");
              frmMenu2 show =  new frmMenu2();
              show.setVisible(true);
              this.setVisible(false);
               
           }
           
           else{
               
               JOptionPane.showMessageDialog(null,"The Username and Password is Incorrect");
               
             txtPass.setText(""); 
             txtUsername.setText("");
             
           }
         
       }catch(Exception e){
           JOptionPane.showMessageDialog(null, e);
       }
       
        }
    Last edited by Tolls; 03-23-2017 at 10:43 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Login Authentication with MYSQL

    You just need the one 'if (rs.next())', as the else for that means they failed to log in.
    Inside that if statement, check the value of 'usertype' in the ResultSet and use that to determine what to display.
    Java Code:
    String userType = rs.get("usertype");
    ... check here, if admin then show admin, else show staff
    Note, make sure you close your result sets, and statements if they aren't intended to be cached.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Login Authentication with MYSQL

    There is no obvious question in your post, so I'll just throw some helpful tips out there.

    I think it could be shorter but more clearer, more maintainable and error proof if written like this:
    Java Code:
    try{
      String sql="SELECT * FROM tblusers WHERE username=? AND password=?";
      pst=conn.prepareStatement(sql);
      pst.setString(1,txtUsername.getText());
      pst.setString(2,txtPass.getText());
      rs=pst.executeQuery();
      
      if( rs.next() ) {
        String usertype = rs.getString("usertype");
        boolean isAdmin = usertype != null &&  usertype.equals("Administrator");
        
        if (isAdmin) {
            //show dialog 1
        } 
        else {
            //show dialog 2
        }
      }
      else {
        //bad username/password
      }
    }
    ...

    Next, your code implies that your database stores plain text passwords. If this is an exercise it's fine, but as soon as you start doing ANYTHING serious with Java and authentication, make sure all passwords are salted and hashed with at least SHA-2. Read the interwebs about secure password storage. Lots of article to be found. You don't want to be mentioned by @TroyHunt and his @haveibeenpwned.

    Oh, and don't prefix your tables with tbl. It's slower to read and awkward to use in autocompletion. On top of that, if someone decides to rename the table and a view should be used instead, you're stuck with the tbl prefix in your code.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Join Date
    Mar 2017
    Posts
    2
    Rep Power
    0

    Default Re: Login Authentication with MYSQL

    wow it works thank you

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: Login Authentication with MYSQL

    Quote Originally Posted by amargagaminghd View Post
    wow it works thank you
    You sound surprised :)

    And please remember my advice about the hashing of the passwords.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Login Authentication Using Bean and Servlet in JSP
    By proksy in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 08-17-2014, 08:26 AM
  2. Single user Login authentication
    By vinoopraj in forum Web Frameworks
    Replies: 0
    Last Post: 07-20-2010, 08:19 AM
  3. Replies: 2
    Last Post: 05-18-2010, 03:12 PM
  4. Biometric Thumb Impression Authentication for login in jsp
    By kishore rajkumar in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-12-2009, 02:30 PM
  5. Replies: 1
    Last Post: 08-05-2008, 10:18 AM

Tags for this Thread

Posting Permissions

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