0

I'm new student with Java and I'm trying to make an app to see if the username and password do exist so I wrote this code and there in the main class I call this method and see if a = true or false.

i tried putting System.out.print statement in the while loop and it didn't show up which means the while loop doesn't works.

public void checkUserName(String username,String password) throws SQLException, ClassNotFoundException
{


openconnection();
String query ="SELECT * FROM hema.employee";
Statement stm=(Statement) con.createStatement();
ResultSet rs;
rs = stm.executeQuery(query);

  while(rs.next()){

         String un,pass;

         un=rs.getString("UserName");
         pass=rs.getString("PassWord");
          System.out.println("hi your answer is "+ a);
          if(un==username&&pass==password){

              System.out.println("if Works");
           a=true;
           break;
                                            } 

   }



}

1 Answers1

0

You don't want to compare your strings with == but with .equals.

 if(un.equals(username) && pass.equals(password))

Apar from that, this is very slow for a big userbase. Instead you could query for username and password in the database and just check if you get back a result or not.

fsperrle
  • 1,262
  • 1
  • 14
  • 26
  • i tried putting System.out.print statement in the while loop and it didn't show up which means the while loop doesn't works. – Ibrahim Keshek Oct 05 '14 at 15:19
  • Well and your database actually has content in `hema.employee`? and there was no SQLException thrown?! – fsperrle Oct 05 '14 at 15:22
  • Actually it works now thank you ... but a new problem came up now if a=true the app works well but if a= false it give me errors – Ibrahim Keshek Oct 05 '14 at 15:24