Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dao.daoimpl;
- import com.dao.AbstractDao;
- import com.dao.Users;
- import com.dao.connection.DВConnectionPool;
- import com.dao.constants.ColumnName;
- import com.dao.constants.SqlRequest;
- import com.dao.exceptions.DaoException;
- import com.dao.utils.ClosingUtil;
- import com.dao.utils.EntityBuilder;
- import com.dao.utils.PaymentSystemLogger;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.List;
- public class UsersDaoImpl extends AbstractDao<Users>{
- private static UsersDaoImpl storage;
- private String message;
- public static synchronized UsersDaoImpl getInstance(){
- if(storage == null){
- storage = new UsersDaoImpl();
- }
- return storage;
- }
- @Override
- public void add(Users users) throws DaoException {
- try {
- connection = DВConnectionPool.getInstance().getConnection();
- statement = connection.prepareStatement(SqlRequest.ADD_USER);
- statement.setString(1, users.getName());
- statement.setString(2, users.getEmail());
- statement.setString(3, users.getLogin());
- statement.setString(4, users.getPassword());
- statement.setInt(5, users.getAge());
- statement.setInt(6, users.getId());
- statement.executeUpdate();
- }
- catch (SQLException e){
- message = "Unable to add the user account ";
- PaymentSystemLogger.getInstance().logError(getClass(), message);
- throw new DaoException(message, e);
- }
- finally{
- ClosingUtil.close(statement);
- }
- }
- @Override
- public void delete(int id) throws DaoException {
- try {
- connection = DВConnectionPool.getInstance().getConnection();
- statement = connection.prepareStatement(SqlRequest.DELETE_OPERATION_BY_ID);
- statement.setInt(6, id);
- statement.executeUpdate();
- }
- catch (SQLException e){
- message = "Unable to delete the operation ";
- PaymentSystemLogger.getInstance().logError(getClass(), message);
- throw new DaoException(message, e);
- }
- finally{
- ClosingUtil.close(statement);
- }
- }
- @Override
- public Users getById(int id) throws DaoException {
- Users user = null;
- try {
- connection = DВConnectionPool.getInstance().getConnection();
- statement = connection.prepareStatement(SqlRequest.GET_OPERATION_BY_ID);
- statement.setInt(1, id);
- result = statement.executeQuery();
- while (result.next()) {
- user = buildUser(result);
- }
- }
- catch (SQLException e){
- message = "Unable to return the operation ";
- PaymentSystemLogger.getInstance().logError(getClass(), message);
- throw new DaoException(message, e);
- }
- finally{
- ClosingUtil.close(result);
- ClosingUtil.close(statement);
- }
- return user;
- }
- public boolean isAuthorized(String login, String password) throws DaoException{
- boolean isLogIn = false;
- try {
- connection = DВConnectionPool.getInstance().getConnection();
- statement = connection.prepareStatement(SqlRequest.CHECK_AUTHORIZATION);
- statement.setString(1, login);
- statement.setString(2, password);
- result = statement.executeQuery();
- if (result.next()) {
- isLogIn = true;
- }
- }
- catch (SQLException e){
- message = "Unable to check the user ";
- PaymentSystemLogger.getInstance().logError(getClass(), message);
- throw new DaoException(message, e);
- }
- finally{
- ClosingUtil.close(result);
- ClosingUtil.close(statement);
- }
- return isLogIn;
- }
- public Users getByLogin(String login) throws DaoException{
- Users user = null;
- try {
- connection = DВConnectionPool.getInstance().getConnection();
- statement = connection.prepareStatement(SqlRequest.GET_USER_BY_LOGIN);
- statement.setString(1, login);
- result = statement.executeQuery();
- while (result.next()) {
- user = buildUser(result);
- }
- }
- catch (SQLException e){
- message = "Unable to return the user ";
- PaymentSystemLogger.getInstance().logError(getClass(), message);
- throw new DaoException(message, e);
- }
- finally{
- ClosingUtil.close(result);
- ClosingUtil.close(statement);
- }
- return user;
- }
- @Override
- public List<Users> getAll() throws DaoException {
- List<Users> list = new ArrayList<>();
- try {
- connection = DВConnectionPool.getInstance().getConnection();
- statement = connection.prepareStatement(SqlRequest.GET_ALL_CLIENTS);
- result = statement.executeQuery();
- while (result.next()) {
- Users user = new Users();
- user.setName(result.getString(ColumnName.USER_NAME));
- list.add(user);
- }
- }
- catch (SQLException e){
- message = "Unable to return list of users ";
- PaymentSystemLogger.getInstance().logError(getClass(), message);
- throw new DaoException(message, e);
- }
- finally{
- ClosingUtil.close(result);
- ClosingUtil.close(statement);
- }
- return list;
- }
- private Users buildUser(ResultSet result) throws SQLException{
- int id = result.getInt(ColumnName.USER_ID);
- String name = result.getString(ColumnName.USER_NAME);
- String email = result.getString(ColumnName.USER_EMAIL);
- String login = result.getString(ColumnName.USER_LOGIN);
- String password = result.getString(ColumnName.USER_PASSWORD);
- int age = result.getInt(ColumnName.USER_AGE);
- Users user = EntityBuilder.buildUser(id,name,email,login,password,age);
- return user;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement