I have a class called Socio
that asks for the name, phone ... and I have created methods getters and setters . And another class called DadosDoUsuario
that will ask for the information to fill the register, but what happens is that in this class DadosDoUsuario
is receiving information from the user and entering it into the database.
I would like to make another class InsereNoBanco
receiving the data entered by the user ... That is, the class DadosDoUsuario
would only receive the information and move to the InsereNoBanco
class. You could give me some tips, because I've tried it anyway and it's complicated. I thank you all. Thanks!
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) throws NomeUsuarioNaoInformado {
if (nome.equals(null) || nome.equals("")) {
throw new NomeUsuarioNaoInformado();
}
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 {
static Scanner scan = new Scanner(System.in);
public void cadastrarUsuario() throws NomeUsuarioNaoInformado {
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();
}
}
}
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();
}
}