Doubt validation of cpf before inserting into the bank

2

In my class SocioDAO I have the salvarSocio() method that will save to the database. In my class DadosUsuario I have the information that needs to be written in the bank in cadastrarUsuario() case. How to proceed to validate CPF?

I did not want to put the validation inside my method cadastrarUsuario() because not the same should not get too big.

public class SocioDAO {

    public void salvarSocio(Socio socio) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append("INSERT INTO socio ");
        sql.append("(nome, telefone, ddd, email, cpf) ");
        sql.append("VALUES (?, ?, ?, ?, ?) ");

        Connection conexao = ConexaoFactory.conectar();

        PreparedStatement comando = conexao.prepareStatement(sql.toString());

        comando.setString(1, socio.getNome());
        comando.setInt(2, socio.getTelefone());
        comando.setInt(3, socio.getDdd());
        comando.setString(4, socio.getEmail());
        comando.setString(5, socio.getCpf());

        comando.executeUpdate();

    }



public class DadosUsuario {

    static Scanner scan = new Scanner(System.in);

    public void cadastrarUsuario() {

        Socio cadastrarUser = new Socio();

        System.out.println("Informe um nome: ");
        cadastrarUser.setNome(scan.nextLine());

        System.out.println("Informe um telefone: ");
        cadastrarUser.setTelefone(scan.nextInt());

        System.out.println("Informe o DDD: ");
        cadastrarUser.setDdd(scan.nextInt());

        System.out.println("Informe o email: ");
        cadastrarUser.setEmail(scan.next());

        System.out.println("Informe o cpf: ");
        cadastrarUser.setCpf(scan.next());

        SocioDAO dao = new SocioDAO();

        try {
            dao.salvarSocio(cadastrarUser);
            System.out.println("USUÁRIO CADASTRADO COM SUCESSO.");
        } catch (SQLException e) {
            System.out.println("ERRO AO CADASTRAR USUÁRIO.");
            //e.printStackTrace();
        }

    }
    
asked by anonymous 13.05.2015 / 19:13

1 answer

3

Since you do not have a business layer and access DAO directly, you should put the validation inside DAO, which encounters some problem, throwing an exception. Placing within DAO, anywhere you instantiate it, will ensure that the data is being validated.

In a more serious application, we never access DAO directly. We always go through a business layer that serves just to do this validation, getting Service (with your business rule and validation) - > DAO

Take a look at this answer , too, the subject may be quite long.

edit : simple example with business layer

public class SocioService { // Tem pessoas que preferem SocioBusiness

    private SocioDAO socioDAO;

    public SocioService() {
        socioDAO = new SocioDAO();
    }

    public void inserir(Socio socio) throws ServiceException { // inserir ou adicionar deixa claro que é "criação" de registro.

        if (socio.getNome() == null || socio.getNome().trim().length()) {
            throw new ServiceException("Nome não pode estar vazio");
        }

        // ... outras validacoes

        socioDAO.salvarSocio(socio); // cuidado com o adjetivo salvar, ele pode deixar a pessoa que chama em duvida, se estiver trabalhando em equipe, a pessoa iria querer ver o fonte para saber se aqui está sendo inserido ou atualizando um registro.
    }
}

public class ServiceException extends Exception {

    public ServiceException(String mensagem) {
        super(mensagem);
    }
}
    
13.05.2015 / 19:36