Error in the validation of the CPF

2

I'm trying to do a validation in the CPF and I'm encountering problems. When I type any number that is not in if , it inserts in the bank normally, but when I type numbers from within if I would like it to display the invalid cpf message and not play to bank, but the most I could do was insert "INVALID CPF !!!" in the bank ... If anyone can help me. I am sending three classes Conexao , Socio and validarCpf .

Class Conexao :

public void CadastrarUsuario() {
    conectarBanco();

    CpfValidacao ver2 = new CpfValidacao();

    try {
        Socio cadastrarNovoSocio = new Socio();
        CpfValidacao ver = new CpfValidacao();

        String sql = ("insert into cadastrousuario values('"
                + cadastrarNovoSocio.getNome() + "','"
                + cadastrarNovoSocio.getEmail() + "','"
                + cadastrarNovoSocio.getDdd() + "','"
                + cadastrarNovoSocio.getTelefoneUsuario() + "','"
                + ver.getValidaCpf()+"')");

        stm.executeUpdate(sql);
        con.close();

    } catch (Exception e) {
        System.out.println("Erro: " + e);

    }
}

Partner Class

public String getCpf() {
    System.out.println("Informe o CPF: ");
    cpf = scan.nextLine();
    return cpf;
}

public void setCpf(String cpf) {
    this.cpf = cpf;
}

Class CpfValidation

public class CpfValidacao {
    private String validaCpf;

    public void fecharConexao(){
        Conexao teste = new Conexao();
        teste.desconectarBanco();
    }
    public String getValidaCpf() {
        Socio validar = new Socio();
        validaCpf = validar.getCpf();

        if (validaCpf.equals("11111111111") || validaCpf.equals("22222222222")
                || validaCpf.equals("33333333333") || validaCpf.equals("44444444444")
                || validaCpf.equals("55555555555") ||validaCpf.equals("66666666666")
                || validaCpf.equals("77777777777") ||validaCpf.equals("88888888888")
                || validaCpf.equals("99999999999") ||validaCpf.length() != 11) {

            fecharConexao();
            return "CPF INVÁLIDO!!!";
        }
        return validaCpf;
    }
    public void fecharConexao(String validaCpf) {
        this.validaCpf = validaCpf;
    }
}
    
asked by anonymous 14.04.2015 / 17:47

1 answer

1

You are not inserting the return of the direct method into your database no matter what it returns, validate the cpf before giving an insert in the table.

And an observation you could replace all this if

    if (validaCpf.equals("11111111111") || validaCpf.equals("22222222222")
            || validaCpf.equals("33333333333") || validaCpf.equals("44444444444")
            || validaCpf.equals("55555555555") ||validaCpf.equals("66666666666")
            || validaCpf.equals("77777777777") ||validaCpf.equals("88888888888")
            || validaCpf.equals("99999999999") ||validaCpf.length() != 11) {

        fecharConexao();
        return "CPF INVÁLIDO!!!";
    }

by a regular expression :

    if (!validaCpf.matches("\d{11,11}")) {
       fecharConexao();
        return "CPF INVÁLIDO!!!";
      }
    
14.04.2015 / 18:06