Advertisement
arthur_arw

CadastroDAO.java

Nov 13th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. package trab;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import trab.Conexao;
  11. import trab.CadastroClientes;
  12.  
  13. public class CadastroDAO {
  14.  
  15.     public static CadastroClientes consultarRG(String rg) {
  16.         Connection conn = null;
  17.         PreparedStatement pstm = null;
  18.         ResultSet rs = null;
  19.         CadastroClientes auxRg = new CadastroClientes();
  20.  
  21.         String consulRG = "SELECT * FROM clientes WHERE rg =?";
  22.  
  23.         try {
  24.             conn = Conexao.createConnectionToMySQL();
  25.             pstm = conn.prepareStatement(consulRG);
  26.             pstm.setString(1, rg);
  27.             rs = pstm.executeQuery();
  28.             if (rs.next()) {
  29.                 auxRg.setRg(rs.getString("rg"));
  30.             }
  31.             return auxRg;
  32.         } catch (Exception e1) {
  33.             System.out.println(e1);
  34.         } finally {
  35.             try {
  36.                 if (pstm != null){
  37.                     pstm.close();
  38.                 }
  39.                 if(conn!=null){
  40.                     conn.close();
  41.                 }
  42.             } catch (Exception e){
  43.                 e.printStackTrace();
  44.             }
  45.         }
  46.         return auxRg;
  47.     }
  48.  
  49.     public static CadastroClientes consultar(String cpf) {
  50.         Connection conn = null;
  51.         PreparedStatement pstm = null;
  52.         ResultSet rs = null;
  53.         CadastroClientes auxCli = new CadastroClientes();
  54.  
  55.         String consulCPF = "SELECT * FROM clientes WHERE cpf = ?";
  56.  
  57.         try {
  58.             conn = Conexao.createConnectionToMySQL();
  59.             pstm = conn.prepareStatement(consulCPF);
  60.             pstm.setString(1, cpf);
  61.             rs = pstm.executeQuery();
  62.             if (rs.next()) {
  63.                 auxCli.setCpf(rs.getString("cpf"));
  64.             }
  65.             return auxCli;
  66.         } catch (Exception e1) {
  67.             System.out.println(e1);
  68.         } finally {
  69.             try {
  70.                 if (pstm != null) {
  71.                     pstm.close();
  72.                 }
  73.                 if (conn != null) {
  74.                     conn.close();
  75.                 }
  76.             } catch (Exception e) {
  77.                 e.printStackTrace();
  78.             }
  79.         }
  80.         return auxCli;
  81.     }
  82.  
  83.     public void Salvar(CadastroClientes cliente) {
  84.  
  85.         Connection conn = null;
  86.         PreparedStatement pstm = null;
  87.  
  88.         CadastroClientes cliAux;
  89.         CadastroClientes cliRg;
  90.        
  91.         cliRg = consultarRG(cliente.getRg());
  92.         cliAux = consultar(cliente.getCpf());
  93.  
  94.         if (cliAux.getCpf() != null || cliRg.getRg() != null) {
  95.             if (cliAux.getCpf().equals(cliente.getCpf()) || cliRg.getRg().equals(cliente.getRg()))
  96.                 System.out.println("[ERRO] CPF ou RG já cadastrado!.");
  97.         } else {
  98.  
  99.             String sql = "INSERT INTO clientes (nome,idade,cpf,rg,tipo,nivel,PlanoSaude,dataCadastro)"
  100.                     + "VALUES(?,?,?,?,?,?,?,?)";
  101.  
  102.             try {
  103.  
  104.                 conn = Conexao.createConnectionToMySQL();
  105.  
  106.                 pstm = conn.prepareStatement(sql);
  107.  
  108.                 pstm.setString(1, cliente.getNome());
  109.                 pstm.setInt(2, cliente.getIdade());
  110.                 pstm.setString(3, cliente.getCpf());
  111.                 pstm.setString(4, cliente.getRg());
  112.                 pstm.setInt(5, cliente.getTipo());
  113.                 pstm.setInt(6, cliente.getNivel());
  114.                 pstm.setInt(7, cliente.getPlanoSaude());
  115.                 pstm.setDate(8, new Date(cliente.getDataCadastro().getTime()));
  116.                 pstm.execute();
  117.  
  118.             } catch (Exception e) {
  119.                 e.printStackTrace();
  120.             } finally {
  121.                 try {
  122.                     if (pstm != null) {
  123.                         pstm.close();
  124.                     }
  125.                     if (conn != null) {
  126.                         conn.close();
  127.                     }
  128.                 } catch (Exception e) {
  129.                     e.printStackTrace();
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135.     public void removerCadastro(int id) {
  136.         String sql = "DELETE FROM clientes WHERE id = ?";
  137.  
  138.         Connection conn = null;
  139.         PreparedStatement pstm = null;
  140.  
  141.         try {
  142.             conn = Conexao.createConnectionToMySQL();
  143.             pstm = conn.prepareStatement(sql);
  144.             pstm.setInt(1, id);
  145.             pstm.execute();
  146.         } catch (Exception e) {
  147.             e.printStackTrace();
  148.         } finally {
  149.             try {
  150.                 if (pstm != null) {
  151.                     pstm.close();
  152.                 }
  153.  
  154.                 if (conn != null) {
  155.                     conn.close();
  156.                 }
  157.             } catch (Exception e) {
  158.                 e.printStackTrace();
  159.             }
  160.         }
  161.  
  162.     }
  163.  
  164.     public List<CadastroClientes> getClientes() {
  165.  
  166.         String sql = "SELECT * FROM clientes";
  167.  
  168.         List<CadastroClientes> clientes = new ArrayList<CadastroClientes>();
  169.  
  170.         Connection conn = null;
  171.         PreparedStatement pstm = null;
  172.         ResultSet rset = null;
  173.  
  174.         try {
  175.             conn = Conexao.createConnectionToMySQL();
  176.             pstm = conn.prepareStatement(sql);
  177.             rset = pstm.executeQuery();
  178.  
  179.             while (rset.next()) {
  180.                 CadastroClientes cliente = new CadastroClientes();
  181.  
  182.                 cliente.setId(rset.getInt("id"));
  183.                 cliente.setNome(rset.getString("nome"));
  184.                 cliente.setIdade(rset.getInt("idade"));
  185.                 cliente.setCpf(rset.getString("cpf"));
  186.                 cliente.setRg(rset.getString("rg"));
  187.                 cliente.setTipo(rset.getInt("tipo"));
  188.                 cliente.setNivel(rset.getInt("nivel"));
  189.                 cliente.setPlanoSaude(rset.getInt("PlanoSaude"));
  190.                 cliente.setDataCadastro(rset.getDate("dataCadastro"));
  191.  
  192.                 clientes.add(cliente);
  193.             }
  194.         } catch (Exception e) {
  195.             e.printStackTrace();
  196.         } finally {
  197.             try {
  198.                 if (rset != null) {
  199.                     rset.close();
  200.                 }
  201.  
  202.                 if (pstm != null) {
  203.                     pstm.close();
  204.                 }
  205.  
  206.                 if (conn != null) {
  207.                     conn.close();
  208.                 }
  209.             } catch (Exception e) {
  210.                 e.printStackTrace();
  211.             }
  212.         }
  213.  
  214.         return clientes;
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement