How to create Exception [duplicate]

0

I'm trying to make an Exception but I can not. I would like your help to solve this problem. I'm from yesterday trying to validate a field but this is difficult ... Thanks in advance! In case this field would be named if it were 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();
    }

}
    
asked by anonymous 15.05.2015 / 21:04

2 answers

0

If you are sure that what you want is to throw an Exception then here it goes.

In the% method of the Partner class test the passed value.

public void setNome(String nome) {
    if(nome == null || nome == ""){
        throw new IllegalArgumentException("Tem de indicar um nome");
    }
    this.nome = nome;
}
    
15.05.2015 / 21:35
-1

include import java.sql.SQLException

See more here. - > link

    
15.05.2015 / 21:21