title: Check your login and password (Java Web, Servlets)
body: <p>I'm trying to verify that the user name and password entered in the JSP page with data from a database, all the parameters are coming, but even when matching data, the function returns false. What is my fault?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class LoginService {
      public boolean loginCheck(String username, String password){
          String query;
          String dbUsername, dbPassword;
          boolean login = false;
  
          try {
              Class.forName("com.mysql.jdbc.Driver").newInstance();
              Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/blog_u", "root", "root");
              Statement stmt = (Statement) con.createStatement();
              query = "SELECT username, password FROM user;";
              stmt.executeQuery(query);
              ResultSet rs = stmt.getResultSet();
  
              while(rs.next()){
                  dbUsername = rs.getString("username");
                  dbPassword = rs.getString("password");
  
                  if(dbUsername == username &amp;&amp; dbPassword == password){
                      System.out.println("OK");
                      login = true;
                  }
                  System.out.println(username + password + " " + dbUsername + dbPassword);
              }
          } catch (InstantiationException e) {
              e.printStackTrace();
          } catch (IllegalAccessException e) {
              e.printStackTrace();
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          } catch (SQLException e) {
              e.printStackTrace();
          }
          return login;
      }
  }
  </code></pre>