0

Why is it not able to compare my input and the values from the database ??

I have also printed the values I got from the database after taking the input so that I can compare the input and the database value .

This is the java code :

public static void clogin(String username, String password) throws Exception  {
    String url = "jdbc:mysql://localhost:3306/Bank";
    String query = "select * from admin_credentials";
    String uname = "###";
    String passw = "###";
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection con = DriverManager.getConnection(url,uname,passw);
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(query);
    while (rs.next()) {
        String un = rs.getString(1); 
        String pw = rs.getString(2);
        System.out.println(un + " " + pw);
        if (un == username && pw == password) {
            System.out.println("Login Successful!!");
        }
        else {
            System.out.println("Invalid Credentials");
        }
    }
    

    st.close();
    con.close();

    
}

public static void main(String[] args) throws Exception {
    Scanner input = new Scanner(System.in);
    System.out.println();
    System.out.println("Username : ");
    String username = input.next();
    System.out.println("Password : ");
    String password = input.next();
    clogin(username,password);

    
}

}

My Database :

Username : kushaltodi
Password : epson

My Database

My Output :

Username :

kushaltodi

Password :

epson

kushaltodi epson

Invalid Credentials

My Output

Kushal
  • 1
  • 3

0 Answers0