I am trying to check if the username already exists in the table: The database also has the field set to unique The method will return a boolean stating if the username already exists or not. The part of my code that sets the usernameExists var to true never runs even if the usernames are the same.
public static boolean CheckUsernameExists(String username)
{
boolean usernameExists = false;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(urlDB, usernameDB, passwordDB);
PreparedStatement st = connection.prepareStatement("select * from Members order by username desc");
ResultSet r1=st.executeQuery();
String usernameCounter;
if(r1.next())
{
usernameCounter = r1.getString("username");
if(usernameCounter == username) //this part does not happen even if it should
{
System.out.println("It already exists");
usernameExists = true;
}
}
}
catch (SQLException e)
{
System.out.println("SQL Exception: "+ e.toString());
}
catch (ClassNotFoundException cE)
{
System.out.println("Class Not Found Exception: "+ cE.toString());
}
return usernameExists;
}
IF you can help in anyway I would be thankful!