Answer the question
In order to leave comments, you need to log in
Should the DAO layer check for possible errors?
Hello. I have a DAO layer where I work with the database. There are methods something like createUser(). Take a look:
public boolean createUser(User user) throws DAOException {
ConnectionPool connectionPool = ConnectionPoolImpl.getInstance();
try (Connection connection = connectionPool.getConnection()) {
final String CREATE_USER_SQL = "INSERT INTO hostel.user" +
"(`login`," +
"`password`," +
"`first_name`," +
"`last_name`," +
"`sex`," +
"`country`," +
"`city`," +
"`registration_date`," +
"`date_of_birth`," +
"`phone_number`," +
"`discount_id`," +
"`account_status_id`)" +
"VALUES (?, (SELECT SHA2(?, 256)), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement preparedStatement =
connection.prepareStatement(CREATE_USER_SQL)) {
preparedStatement.setString(1, user.getLogin());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getFirstName());
preparedStatement.setString(4, user.getLastName());
preparedStatement.setBoolean(5, user.isSex());
preparedStatement.setString(6, user.getCountry());
preparedStatement.setString(7, user.getCity());
preparedStatement.setDate(8, new Date(user.getRegistrationDate().getTime()));
preparedStatement.setDate(9, new Date(user.getDateOfBirth().getTime()));
preparedStatement.setInt(10, user.getPhoneNumber());
preparedStatement.setInt(11, user.getDiscountId());
preparedStatement.setInt(12, user.getAccountStatusId());
return preparedStatement.executeUpdate() > 0;
}
} catch (SQLException e) {
logger.log(Level.ERROR, e);
throw new DAOException("Error while executing the query.");
} catch (ConnectionPoolException e) {
logger.log(Level.ERROR, e);
throw new DAOException("Error trying to work with the connection pool.", e);
}
}
Answer the question
In order to leave comments, you need to log in
1. It is not forbidden to check input parameters and this does not apply to business logic. Those. checks won't hurt.
2. Despite the checks of item 1, the input parameters must be correct and not cause errors, i.e. in calling methods of the service layer, User objects must be correctly formed in any case.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question