This is a code that i'm creating helping users to log in into my application a method login that gets two arguments username and password.
I want to be specified with my error messages to the user, so:
- if the email does not exist in the table it shows "email is not registered"
- if the email exists and the password is incorrect is shows " incorrect password"
Is my code efficient? I need your opinion. What do I need to improve\change?
Here's the method
private String command;
private ResultSet resultSet;
private PreparedStatement statement;
private Connection connection;
String jdbcUrl = "jdbc:mysql://localhost:3306/registered";
String jdbcUser = "...";
String jdbcPassword = "...";
public boolean login(String eml, String pwd) {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(jdbcUrl, jdbcUser,
jdbcPassword);
command = "SELECT Email FROM users WHERE Email LIKE '" + eml + "';";
statement = connection.prepareStatement(command);
resultSet = statement.executeQuery();
if (!resultSet.isBeforeFirst()) {
System.out.println("Email (" + eml + ") is not registered ! ");
// show error message
} else {
command = "SELECT Email,Password FROM users WHERE Email LIKE '"
+ eml + "' AND Password LIKE '" + pwd + "';";
statement = connection.prepareStatement(command);
resultSet = statement.executeQuery();
if (!resultSet.isBeforeFirst()) {
System.out.println("Password for Email (" + eml
+ ") is incorrect ! ");
// show error message
}
else {
System.out.println("Logged in!");
}
}
} catch (SQLException e) {
System.out.println("SQLException: " + e.getMessage());
System.out.println("Vendor error: " + e.getErrorCode());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return false;
}