Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trab;
- import java.sql.Connection;
- import java.sql.Date;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.List;
- import trab.Conexao;
- import trab.CadastroClientes;
- public class CadastroDAO {
- public static CadastroClientes consultarRG(String rg) {
- Connection conn = null;
- PreparedStatement pstm = null;
- ResultSet rs = null;
- CadastroClientes auxRg = new CadastroClientes();
- String consulRG = "SELECT * FROM clientes WHERE rg =?";
- try {
- conn = Conexao.createConnectionToMySQL();
- pstm = conn.prepareStatement(consulRG);
- pstm.setString(1, rg);
- rs = pstm.executeQuery();
- if (rs.next()) {
- auxRg.setRg(rs.getString("rg"));
- }
- return auxRg;
- } catch (Exception e1) {
- System.out.println(e1);
- } finally {
- try {
- if (pstm != null){
- pstm.close();
- }
- if(conn!=null){
- conn.close();
- }
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- return auxRg;
- }
- public static CadastroClientes consultar(String cpf) {
- Connection conn = null;
- PreparedStatement pstm = null;
- ResultSet rs = null;
- CadastroClientes auxCli = new CadastroClientes();
- String consulCPF = "SELECT * FROM clientes WHERE cpf = ?";
- try {
- conn = Conexao.createConnectionToMySQL();
- pstm = conn.prepareStatement(consulCPF);
- pstm.setString(1, cpf);
- rs = pstm.executeQuery();
- if (rs.next()) {
- auxCli.setCpf(rs.getString("cpf"));
- }
- return auxCli;
- } catch (Exception e1) {
- System.out.println(e1);
- } finally {
- try {
- if (pstm != null) {
- pstm.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return auxCli;
- }
- public void Salvar(CadastroClientes cliente) {
- Connection conn = null;
- PreparedStatement pstm = null;
- CadastroClientes cliAux;
- CadastroClientes cliRg;
- cliRg = consultarRG(cliente.getRg());
- cliAux = consultar(cliente.getCpf());
- if (cliAux.getCpf() != null || cliRg.getRg() != null) {
- if (cliAux.getCpf().equals(cliente.getCpf()) || cliRg.getRg().equals(cliente.getRg()))
- System.out.println("[ERRO] CPF ou RG já cadastrado!.");
- } else {
- String sql = "INSERT INTO clientes (nome,idade,cpf,rg,tipo,nivel,PlanoSaude,dataCadastro)"
- + "VALUES(?,?,?,?,?,?,?,?)";
- try {
- conn = Conexao.createConnectionToMySQL();
- pstm = conn.prepareStatement(sql);
- pstm.setString(1, cliente.getNome());
- pstm.setInt(2, cliente.getIdade());
- pstm.setString(3, cliente.getCpf());
- pstm.setString(4, cliente.getRg());
- pstm.setInt(5, cliente.getTipo());
- pstm.setInt(6, cliente.getNivel());
- pstm.setInt(7, cliente.getPlanoSaude());
- pstm.setDate(8, new Date(cliente.getDataCadastro().getTime()));
- pstm.execute();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (pstm != null) {
- pstm.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- public void removerCadastro(int id) {
- String sql = "DELETE FROM clientes WHERE id = ?";
- Connection conn = null;
- PreparedStatement pstm = null;
- try {
- conn = Conexao.createConnectionToMySQL();
- pstm = conn.prepareStatement(sql);
- pstm.setInt(1, id);
- pstm.execute();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (pstm != null) {
- pstm.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- public List<CadastroClientes> getClientes() {
- String sql = "SELECT * FROM clientes";
- List<CadastroClientes> clientes = new ArrayList<CadastroClientes>();
- Connection conn = null;
- PreparedStatement pstm = null;
- ResultSet rset = null;
- try {
- conn = Conexao.createConnectionToMySQL();
- pstm = conn.prepareStatement(sql);
- rset = pstm.executeQuery();
- while (rset.next()) {
- CadastroClientes cliente = new CadastroClientes();
- cliente.setId(rset.getInt("id"));
- cliente.setNome(rset.getString("nome"));
- cliente.setIdade(rset.getInt("idade"));
- cliente.setCpf(rset.getString("cpf"));
- cliente.setRg(rset.getString("rg"));
- cliente.setTipo(rset.getInt("tipo"));
- cliente.setNivel(rset.getInt("nivel"));
- cliente.setPlanoSaude(rset.getInt("PlanoSaude"));
- cliente.setDataCadastro(rset.getDate("dataCadastro"));
- clientes.add(cliente);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (rset != null) {
- rset.close();
- }
- if (pstm != null) {
- pstm.close();
- }
- if (conn != null) {
- conn.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- return clientes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement