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