How to create Exception

Asked

Viewed 320 times

0

I am trying to make an exception but I am not getting it. I would like your help to solve this problem. I am since yesterday trying to validate a field but this difficult... Thanks already to all! In case this field would be name if it was null!

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 Socio {

private Long codigo;
private String nome;
private Integer telefone;
private Integer ddd;
private String email;
private String cpf;

public Long getCodigo() {
    return codigo;
}

public void setCodigo(Long codigo) {
    this.codigo = codigo;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public Integer getTelefone() {
    return telefone;
}

public void setTelefone(Integer telefone) {
    this.telefone = telefone;
}

public Integer getDdd() {
    return ddd;
}

public void setDdd(Integer ddd) {
    this.ddd = ddd;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getCpf() {
    return cpf;
}

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

public class DadosUsuario {

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();
    }

}
  • 2

    You read calmly the answer I put to your question yesterday ? There you have the answer to this question. If there are any questions, let me know. Original question: http://answall.com/questions/63765/d%C3%Bavida-valida%C3%A7%C3%A3o-de-cpf-antes-de-inserir-no-banco

2 answers

0

If you’re sure what you want is to launch a Exception so here it goes.

In the method setNome class Partner test the past value.

public void setNome(String nome) {
    if(nome == null || nome == ""){
        throw new IllegalArgumentException("Tem de indicar um nome");
    }
    this.nome = nome;
}

-1

  • Can you explain the answer better? can you bring some content of this link here to make the answer more complete. I did not give it -1 but unexplained responses attract negative votes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.