I have a Login Method that is triggered by JButton Actionperformed.
private void loginButtonActionPerformed(java.awt.event.ActionEvent evt) {
try {
Users userData = userDAO.login(Integer.parseInt(loginUserId.getText()), String.valueOf(loginUserPassword.getPassword()));
} catch (SQLException ex) {
LOGGER.log(Level.SEVERE, ex.toString(), ex);
JOptionPane.showMessageDialog(null, "Error SQL: Please Contact your System Administrator", cf.WINDOW_TITLE, JOptionPane.ERROR_MESSAGE);
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), cf.WINDOW_TITLE, JOptionPane.ERROR_MESSAGE);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Pls write a number", cf.WINDOW_TITLE, JOptionPane.ERROR_MESSAGE);
}
}
Here is my Method that returns an Object from the database.
public Users login(int userId, String password) throws NumberFormatException, SQLException , NullPointerException {
SQL sql = new SQL();
query = sql.createPStatement(cf.SELECT_USER_BY_LOGIN_DATA);
query.setInt(1, userId);
query.setString(2, password);
ResultSet resultSet = query.executeQuery();
try {
if (!resultSet.isBeforeFirst()) {
throw new NullPointerException("User Not Found");
}
while (resultSet.next()) {
Users newLoggedUser = new Users();
//Set Object Values
return newLoggedUser;
}
} catch (SQLException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
throw e;
}
catch (NullPointerException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
throw e;
} catch (NumberFormatException e) {
LOGGER.log(Level.SEVERE, e.toString(), e);
throw e;
} finally {
SQLUtils.closeQuietly(sql.dbConection());
SQLUtils.closeQuietly(resultSet);
SQLUtils.closeQuietly(query);
}
return null;
}
I have to show multiple error messages to the USER accorgding to:
- User not found (No SQL result);
- Type Letters instead of numbers
- SQL Error.
I have some questions about this:
Should I Stop the app to doing more actions Where my Results/Objects are
NULL
, usingthrow new NullPointerException("User Not Found");
Is safely to use those exceptions to send User Messages, like in the case of Object is null ("User not Found"), and throw them by the DAO Method to be catched/Displayed by the
JButton ActionPerformed try/Catch?
Where I should log the exceptions, in the DAO Method? , or ActionPerformed try/Catch?.
Should I
throw e;
again in DAO Method to be catched byActionPerformed try/Catch
?
Like Always thank you very much for your help.