Advertisement
JacksonBarbosa

Untitled

Oct 30th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package exemplodbmysql;
  7.  
  8. import atividade.Pessoa;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.sql.Statement;
  15. import java.util.ArrayList;
  16. import java.util.logging.Level;
  17. import java.util.logging.Logger;
  18.  
  19. /**
  20.  *
  21.  * @author neto
  22.  */
  23. public class ManipulaPessoa {
  24.  
  25.     Connection con = null;
  26.     Statement st = null;
  27.     ResultSet rs = null;
  28.  
  29.     public ArrayList<Pessoa> getAll() {
  30.         ArrayList<Pessoa> listaDePessoas = new ArrayList<>();
  31.  
  32.         try {
  33.  
  34.             con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "");
  35.             st = con.createStatement();
  36.             rs = st.executeQuery("Select * from pessoa");
  37.  
  38.             while (rs.next()) {
  39.                 Pessoa novaPessoa = new Pessoa();
  40.                 novaPessoa.setNome(rs.getString("nome"));
  41.                 novaPessoa.setId(rs.getInt("idade"));
  42.                 novaPessoa.setCpf(rs.getString("cpf"));
  43.                 novaPessoa.setSenha(rs.getString("senha"));
  44.                 novaPessoa.setUsuario(rs.getString("usuario"));
  45.                 novaPessoa.setId(rs.getInt("id"));
  46.                 listaDePessoas.add(novaPessoa);
  47.             }
  48.  
  49.             con.close();
  50.             st.close();
  51.             rs.close();
  52.  
  53.         } catch (SQLException ex) {
  54.             ex.printStackTrace();
  55.         } finally {
  56.             rs = null;
  57.             st = null;
  58.             con = null;
  59.         }
  60.  
  61.         return listaDePessoas;
  62.     }
  63.  
  64.     public Pessoa getPessoaByCPF(String cpf) {
  65.         Pessoa pessoaPorCpf = new Pessoa();
  66.  
  67.         try {
  68.             con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "123456");
  69.             st = con.createStatement();
  70.             rs = st.executeQuery("Select * from pessoa where cpf = '" + cpf + "'");
  71.  
  72.             if (rs.next()) {
  73.  
  74.                 pessoaPorCpf.setNome(rs.getString("nome"));
  75.                 pessoaPorCpf.setId(rs.getInt("idade"));
  76.                 pessoaPorCpf.setCpf(rs.getString("cpf"));
  77.                 pessoaPorCpf.setSenha(rs.getString("senha"));
  78.                 pessoaPorCpf.setUsuario(rs.getString("usuario"));
  79.                 pessoaPorCpf.setId(rs.getInt("id"));
  80.             }
  81.  
  82.             con.close();
  83.             st.close();
  84.             rs.close();
  85.  
  86.         } catch (SQLException ex) {
  87.             ex.printStackTrace();
  88.         }
  89.         return pessoaPorCpf;
  90.     }
  91.  
  92.     public int inserir(Pessoa pessoa) {
  93.         //Implementar
  94.         String sql = "insert into pessoa "
  95.                 + "(nome,idade,cpf,senha,usuario,id,) "
  96.                 + "values (" + pessoa.getNome() + ",?,?,?,?,?)";
  97.         try {
  98.             con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "");
  99.             st = con.createStatement();
  100.             st.execute(sql);
  101.             return 1;
  102.         } catch (SQLException ex) {
  103.             Logger.getLogger(ManipulaPessoa.class.getName()).log(Level.SEVERE, null, ex);
  104.             return 0;
  105.         }
  106.     }
  107.  
  108.     public Pessoa getPessoaByNome(String nome) {
  109.         Pessoa p = new Pessoa();
  110.         try {
  111.             con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "");
  112.             st = con.createStatement();
  113.             rs = st.executeQuery("Select * from pessoa where nome = '" + nome + "'");
  114.  
  115.             if (rs.next()) {
  116.  
  117.                 p.setNome(rs.getString("nome"));
  118.                 p.setId(rs.getInt("idade"));
  119.                 p.setCpf(rs.getString("cpf"));
  120.                 p.setSenha(rs.getString("senha"));
  121.                 p.setUsuario(rs.getString("usuario"));
  122.                 p.setId(rs.getInt("id"));
  123.             }
  124.  
  125.             con.close();
  126.             st.close();
  127.             rs.close();
  128.  
  129.         } catch (SQLException ex) {
  130.             ex.printStackTrace();
  131.         }
  132.         return p;
  133.     }
  134.  
  135.     public int atualizaDadosPessoaPorCPF(Pessoa pessoa) {
  136.         // Implementar com o update
  137.         return 0;
  138.     }
  139.  
  140.     public int apagarPessoaPorCPF(String cpf) {
  141.  
  142.         //Implementar com o Delete
  143.         return 0;
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement