This code is part of DBConnector class:
public void loginUsersIntoStore(User user) throws SQLException{
connection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
String sql = "SELECT id FROM users WHERE username=? AND password=?";
pStatement = connection.prepareStatement(sql);
pStatement.setString(1, user.getUsername());
pStatement.setString(2, user.getPassword());
ResultSet resultSet = pStatement.executeQuery();
boolean found = false;
if(resultSet.next()) {
int id = resultSet.getInt("ID");
if (id != 0) {
found = true;
}
}
if(found){
loginUsersIntoStore(user);
pStatement.executeQuery();
pStatement.close();
connection.close();
}
if(!found){
System.out.println("Invalid login credentials");
}
}
This part of code is where i call the method in Main.
private static void login(UserManager userManager, Scanner input) throws SQLException {
System.out.print("Enter username: ");
String userName = input.next();
System.out.print("Enter password: ");
String password = input.next();
User user = new User(userName, password);
boolean isAuthenticated = userManager.authenticate(user);
if (isAuthenticated) {
DBConnector.getInstance().loginUsersIntoStore(user);
System.out.println("Login was successful!");
} else {
System.out.println("Invalid username or passwordl!");
}
}
In this code i am trying to make an online store (console application), the product part works fine ( where i add products to database and then display them), when it comes to users part, it saves me the users in database but i cannot login from there ( if i close the app and run it again, and i try to login with users that exists in database it prints Invalid username or password)
First, I forgot to close the connection and the statement. After i closed nothing changes.
>Solution :
Seems like a logical issue in the loginUsersIntoStore() method. The loginUsersIntoStore() method is recursively calling itself in the found block, which can lead to an infinite loop and may cause the program to crash. The pStatement.executeQuery() method is being called twice in the found block, which may not be necessary. To fix this issue you should remove the recursive call to loginUsersIntoStore() in the found block and only call pStatement.executeQuery() once.
public void loginUsersIntoStore(User user)
throws SQLException {
connection = DriverManager.getConnection(JDBC_URL, USER, PASSWORD);
String sql = "SELECT id FROM users WHERE username=? AND password=?";
pStatement = connection.prepareStatement(sql);
pStatement.setString(1, user.getUsername());
pStatement.setString(2, user.getPassword());
ResultSet resultSet = pStatement.executeQuery();
boolean found = false;
if (resultSet.next()) {
int id = resultSet.getInt("ID");
if (id != 0) {
found = true;
}
}
if (found) {
System.out.println("Login was successful!");
} else {
System.out.println("Invalid login credentials");
}
pStatement.close();
connection.close();
}
check that the credentials entered by the user in the login() method are being passed correctly to the loginUsersIntoStore() method.