0

I've inserted awt choice class for choices(Admin, User) and using if-else I wanna engage table admins(with columns 'username' and 'password') when c=="Admin"(or c.equals(Admin)) and table users(with columns 'username' and 'pass') when c is anything else.

while everything's good with admin login, user login will always give an error. error: Invalid Username or Password which comes from else block that's inside try block of user login part.

any suggestions on this?

   if(c.equals("Admin")){
       users.admin=1;
    try{
        String sql="select * from admins where username=? and password=?";
        pst=conn.prepareStatement(sql);
        pst.setString(1, TF_USER.getText());
        pst.setString(2, PW.getText());
        rs=pst.executeQuery();
        if(rs.next()){

            WelcomeUser j = new WelcomeUser();
               j.setVisible(true);
               this.dispose();
        }
        else{
            JOptionPane.showMessageDialog(null,"Invalid Username or Password","Action Deinied",JOptionPane.ERROR_MESSAGE);

        }
    }
     catch(Exception e){
       JOptionPane.showMessageDialog(null, e); 
    }
    finally {

        try{
            rs.close();
            pst.close();

        }
        catch(Exception e){
        JOptionPane.showMessageDialog(null, e);    
        }
    }
   }
   else{
       users.admin=0;
   try{
        String Sql="select * from users where username=? and pass=?";
        pst=conn.prepareStatement(Sql);
        pst.setString(1, TF_USER.getText());
        pst.setString(2, PW.getText());
        rs=pst.executeQuery();
        if(rs.next()){

            WelcomeUser j = new WelcomeUser();
               j.setVisible(true);
               this.dispose();
        }
        else{
            JOptionPane.showMessageDialog(null,"Invalid Username or Password","Action Deinied",JOptionPane.ERROR_MESSAGE);

        }
    }
     catch(Exception e){

    }
   finally {

        try{
            rs.close();
            pst.close();

        }
        catch(Exception e){
        JOptionPane.showMessageDialog(null, e);    
        }
    }
   }
  • Don't compare String as c=="Admin" check on https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – Rakesh KR Jul 02 '17 at 09:19
  • i checked value of c and it was fine. i know .equals() is preferred but == is working just fine. Even .equals() gives back same error!! – BackyarDEV Jul 02 '17 at 09:33
  • @BackyarDEV `.equals` is not *preferred*, it's *the only way* to check for string equality here. If `==` is working you are just being lucky, it won't work in most cases because it is *not* intended for value equality checking and should not be used. – BackSlash Jul 02 '17 at 09:39
  • One of the catch blocks is empty. It should never be empty. Because if control ever comes inside this, you would never know. – Vijay Jul 02 '17 at 09:55
  • @vijay done that still nothing,s changed – BackyarDEV Jul 02 '17 at 10:26
  • @BackSlash okay – BackyarDEV Jul 02 '17 at 10:26
  • spelling of equals is incorrect. if(c.eqauls("Admin")) should be if(c.equals("Admin")). And including the System.out.println(e) inside catch blocks may help in getting to know the error. – Vijay Jul 02 '17 at 10:27
  • @vijay yeah sorry! wasnt in the code though – BackyarDEV Jul 02 '17 at 10:29
  • What kind of errors do you get? is it database related or java exception? If you could share the complete code then definitely suggestion would be more appropriate. – Vijay Jul 02 '17 at 10:33
  • @vijay only if i knew that! database is just fine, query is correct column names are fine, too. So, my guess is that problem is at java's end! – BackyarDEV Jul 02 '17 at 10:40
  • do you get any output or not? I mean any error message or exception? if not chances are that your catch block is empty. So plz ensure that catch block has System.out.println(e) and run the code again. note down the exception if any and let us know. – Vijay Jul 02 '17 at 10:44
  • @vijay catch block has been filled(see edits) error message is what it should display when username and password combo is incorrect! please re-read the whole story again first!!! – BackyarDEV Jul 02 '17 at 13:07

0 Answers0