I have made a simple login webapp with Servlet and JSP and MySQL in java. I have made a username manually in the MySQL database. When I want to login, it show's the user is null(user is an object of the class User, the codes are below my explanation).
Iam using Apache Tomcat 10, IntelliJ, Mysql 8, and jakarta.
User
UserDao
ConnectionProvider
LoginServlet
These are the java classes that I've used.
Source Codes : : :
UserDao
This is the class that I have been having a problem, where I've introduced the "User" class as null, it can't get into the if statement. And when I don't declare it as null, it can login with any name or password I type in... please help this humble learner.public class UserDao {
private final Connection con;
public UserDao(Connection con) {
this.con = con;
}
public User login(String email, String password) {
User user = null;
try {
String query = "SELECT * FROM USERS WHERE email=? AND password=?";
PreparedStatement pstmt = con.prepareStatement(query);
pstmt.setString(1, email);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
return user;
}
}


