-3

I'm trying to make a simple login activity using email and password from my Mysql Database.

Now, i set the email value to "e" and the password to "e1" (to test it). And i import it to my app into a string, i check if the password and the email are true:

res = st.executeQuery("SELECT * FROM Users WHERE Email = '"+emailVar+"' AND Password = '"+pswdVar+"' ");
         while (res.next()) {
              String pswdDb = res.getString("Password");
              String emailDb = res.getString("Email");
              if (emailVar == emailDb && pswdVar == pswdDb)
              {

                  Intent I = new Intent (this, MainActivity.class);
                  startActivity(I);
              }

but the if statement always returns false.

After debugging i saw that the value from the database and the input value are equal.

How do i fix this??

323go
  • 14,143
  • 6
  • 33
  • 41
  • 2
    Don't compare Strings using `==`. Use the `.equals(..)` method. Your if-statement then becomes `if (emailVar.equals(emailDb) && pswdVar.equals(pswdDb))` – Kon Nov 18 '13 at 05:40
  • are you getting the values from the db ? did you check in logcat which if the values are equal ? – jayeshkv Nov 18 '13 at 05:41

2 Answers2

0

In Java and Android == is used to compare two objects. To compare two variable you need to use .equals() method as follows,

if (emailVar.equals(emailDb) && pswdVar.equals(pswdDb))
{
     Intent I = new Intent (this, MainActivity.class);
     startActivity(I);
}
Vigbyor
  • 2,568
  • 4
  • 21
  • 35
0

Strings in Java are compared using equals method and not with == operator.

    if (emailVar.equals(emailDb) && pswdVar.equals(pswdDb)){

              Intent I = new Intent (this, MainActivity.class);
              startActivity(I);
    } 
Sayed Jalil Hassan
  • 2,535
  • 7
  • 30
  • 42