Advertisement
Guest User

Untitled

a guest
Feb 26th, 2018
130
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package br.com.caelum.jdbc.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.Date;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Time;
  9. import java.time.LocalDate;
  10. import java.time.ZoneId;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. import br.com.caelum.jdbc.ConnectionFactory;
  15. import br.com.caelum.jdbc.modelo.Contato;
  16.  
  17. public class ContatoDAO {
  18.  
  19.     private Connection connection;
  20.    
  21.     public ContatoDAO() {
  22.         this.connection = new ConnectionFactory().getConnection();     
  23.     }
  24.    
  25.     public void adiciona(Contato contato) {
  26.         String sql = "insert into contatos " +
  27.                 "(nome,email,endereco,dataNascimento, horaAgendada)" +
  28.                 " values (?,?,?,?,?)";
  29.  
  30.         try {
  31.             PreparedStatement stmt = connection.prepareStatement(sql);
  32.  
  33.             stmt.setString(1,contato.getNome());
  34.             stmt.setString(2,contato.getEmail());
  35.             stmt.setString(3,contato.getEndereco());
  36.             stmt.setDate(4, Date.valueOf(contato.getDataNascimento())); //tem que converter
  37.             stmt.setTime(5, Time.valueOf(contato.getHoraAgendada())); //tem que converter
  38.  
  39.             stmt.execute();
  40.             stmt.close();
  41.         } catch (SQLException e) {
  42.             throw new RuntimeException(e);
  43.         }
  44.     }
  45.    
  46.     //###################################################################################################
  47.     public List<Contato> getLista() {
  48.         try {
  49.             List<Contato> contatos = new ArrayList<Contato>();
  50.             PreparedStatement stmt = this.connection.prepareStatement("select * from contatos");
  51.             ResultSet rs = stmt.executeQuery();
  52.  
  53.             while (rs.next()) {
  54.  
  55.                 Contato contato = new Contato();
  56.                 contato.setId(rs.getLong("id"));
  57.                 contato.setNome(rs.getString("nome"));
  58.                 contato.setEmail(rs.getString("email"));
  59.                 contato.setEndereco(rs.getString("endereco"));
  60.                 contato.setDataNascimento(rs.getDate("dataNascimento").toLocalDate());
  61.                 String tamanho = rs.getString("size");
  62.                 contato.setTamanho(Tamanho.valueOf(tamanho));
  63.                 contatos.add(contato);
  64.             }
  65.             rs.close();
  66.             stmt.close();
  67.             return contatos;
  68.         } catch (SQLException e) {
  69.             throw new RuntimeException(e);
  70.         }
  71.     }
  72.  
  73. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement