I am working on a project for school which requires a very basic log in function (nothing too fancy, seeing as how the passwords will remain in plain text). I have a test database and have thoroughly tested that all values that are being referenced and pulled from the database can also be displayed properly. You will see in the code that the type for each variable being compared is of the String type. My question to you is, why aren't the variables userOut and un equal? (I have even tried using the .toString() method on each of them individually, and well as together to try and get them "equal").
public static void main(String[] args) throws SQLException {
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/users", "root", "Hsiri0758");
System.out.println("Database connected...");
Statement stat = conn.createStatement();
Statement pwstat = conn.createStatement();
Scanner scan = new Scanner(System.in);
System.out.println("Username: ");
String un = scan.nextLine();
System.out.println("Password: ");
String pw = scan.nextLine();
ResultSet unRset = stat.executeQuery("SELECT username FROM accounts WHERE username = \"" + un + "\";");
unRset.next();
String userOut = unRset.getString(1);
ResultSet pwRset = pwstat.executeQuery("SELECT password FROM accounts WHERE password = \"" + pw + "\";");
pwRset.next();
String pwordOut = pwRset.getString(1);
if (userOut == un) {
System.out.println("Welcome " + un + "!");
}
else {
System.out.println("Invalid username.");
}
conn.close();
}
catch (SQLException ex) {
ex.printStackTrace();
}
}