0

Here is a piece of code which I have written to perform login operation using Web-Application in java
Here is how I am taking input from user...

String name = req.getParameter("nm");
String passwd = req.getParameter("pswd");

after that I am validating username and password using such piece of code but though I am providing valid username and password (which exists in database) it's telling that's invalid

con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/java_test", "root", "");
            Statement statement = con.createStatement();
            ResultSet results = statement.executeQuery("SELECT * FROM registration");
            int count = 0;
            while (results.next()) {
                String db_name =results.getString("firstName");
                String db_password = results.getString("password");
                if ( db_name==name && db_password==passwd ) {
                    // user is valid
                    out.print(results.getString("firstName")+"->");
                    out.println(results.getString("password"));
                    count++;
                }
            }
            if ( count==0 ) {
                out.println("Invalid User name or Password");
            }

Everything is going right, no exception is there, while loop is also iterating but somehow if condition (which validates username and password) is not getting satisfied.
Can anyone tell me where and how it's going wrong?

joy
  • 47
  • 1
  • 9
  • When comparing two strings, use [`.equals()`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#equals(java.lang.Object)) and don't use `==`, as the latter compares references, not string values. Also, [What is the difference between == and equals() in Java?](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-and-equals-in-java) answers your question. – Giorgi Tsiklauri Feb 21 '21 at 12:51
  • @GiorgiTsiklauri I got my mistake, Thank You :) – joy Feb 21 '21 at 13:17

0 Answers0