Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package exemplodbmysql;
- import atividade.Pessoa;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author neto
- */
- public class ManipulaPessoa {
- Connection con = null;
- Statement st = null;
- ResultSet rs = null;
- public ArrayList<Pessoa> getAll() {
- ArrayList<Pessoa> listaDePessoas = new ArrayList<>();
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "");
- st = con.createStatement();
- rs = st.executeQuery("Select * from pessoa");
- while (rs.next()) {
- Pessoa novaPessoa = new Pessoa();
- novaPessoa.setNome(rs.getString("nome"));
- novaPessoa.setId(rs.getInt("idade"));
- novaPessoa.setCpf(rs.getString("cpf"));
- novaPessoa.setSenha(rs.getString("senha"));
- novaPessoa.setUsuario(rs.getString("usuario"));
- novaPessoa.setId(rs.getInt("id"));
- listaDePessoas.add(novaPessoa);
- }
- con.close();
- st.close();
- rs.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- } finally {
- rs = null;
- st = null;
- con = null;
- }
- return listaDePessoas;
- }
- public Pessoa getPessoaByCPF(String cpf) {
- Pessoa pessoaPorCpf = new Pessoa();
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "123456");
- st = con.createStatement();
- rs = st.executeQuery("Select * from pessoa where cpf = '" + cpf + "'");
- if (rs.next()) {
- pessoaPorCpf.setNome(rs.getString("nome"));
- pessoaPorCpf.setId(rs.getInt("idade"));
- pessoaPorCpf.setCpf(rs.getString("cpf"));
- pessoaPorCpf.setSenha(rs.getString("senha"));
- pessoaPorCpf.setUsuario(rs.getString("usuario"));
- pessoaPorCpf.setId(rs.getInt("id"));
- }
- con.close();
- st.close();
- rs.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return pessoaPorCpf;
- }
- public int inserir(Pessoa pessoa) {
- //Implementar
- String sql = "insert into pessoa "
- + "(nome,idade,cpf,senha,usuario,id,) "
- + "values (" + pessoa.getNome() + ",?,?,?,?,?)";
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "");
- st = con.createStatement();
- st.execute(sql);
- return 1;
- } catch (SQLException ex) {
- Logger.getLogger(ManipulaPessoa.class.getName()).log(Level.SEVERE, null, ex);
- return 0;
- }
- }
- public Pessoa getPessoaByNome(String nome) {
- Pessoa p = new Pessoa();
- try {
- con = DriverManager.getConnection("jdbc:mysql://localhost/aulajdbc", "root", "");
- st = con.createStatement();
- rs = st.executeQuery("Select * from pessoa where nome = '" + nome + "'");
- if (rs.next()) {
- p.setNome(rs.getString("nome"));
- p.setId(rs.getInt("idade"));
- p.setCpf(rs.getString("cpf"));
- p.setSenha(rs.getString("senha"));
- p.setUsuario(rs.getString("usuario"));
- p.setId(rs.getInt("id"));
- }
- con.close();
- st.close();
- rs.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- return p;
- }
- public int atualizaDadosPessoaPorCPF(Pessoa pessoa) {
- // Implementar com o update
- return 0;
- }
- public int apagarPessoaPorCPF(String cpf) {
- //Implementar com o Delete
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement