Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. ### JdbcTarefaDao.java V1
  2.  
  3. ```java
  4. package br.com.caelum.tarefas.dao;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.Date;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13. import java.util.List;
  14.  
  15. import org.springframework.stereotype.Repository;
  16.  
  17. import br.com.caelum.tarefas.ConnectionFactory;
  18. import br.com.caelum.tarefas.modelo.Tarefa;
  19.  
  20. @Repository
  21. public class JdbcTarefaDao {
  22. private Connection connection;
  23.  
  24. public JdbcTarefaDao() {
  25. try {
  26. this.connection = new ConnectionFactory().getConnection();
  27. } catch (Exception e) {
  28. System.out.println(e);
  29. }
  30. }
  31. public void adiciona(Tarefa tarefa) {
  32. String sql = "insert into tarefas (descricao, finalizado) values (?,?)";
  33. PreparedStatement stmt;
  34. try {
  35. stmt = connection.prepareStatement(sql);
  36. stmt.setString(1, tarefa.getDescricao());
  37. stmt.setBoolean(2, tarefa.isFinalizado());
  38. stmt.execute();
  39. } catch (SQLException e) {
  40. throw new RuntimeException(e);
  41. }
  42. }
  43.  
  44. public void remove(Tarefa tarefa) {
  45.  
  46. if (tarefa.getId() == null) {
  47. throw new IllegalStateException("Id da tarefa não deve ser nula.");
  48. }
  49.  
  50. String sql = "delete from tarefas where id = ?";
  51. PreparedStatement stmt;
  52. try {
  53. stmt = connection.prepareStatement(sql);
  54. stmt.setLong(1, tarefa.getId());
  55. stmt.execute();
  56. } catch (SQLException e) {
  57. throw new RuntimeException(e);
  58. }
  59. }
  60.  
  61. public void altera(Tarefa tarefa) {
  62. String sql = "update tarefas set descricao = ?, finalizado = ?, dataFinalizacao = ? where id = ?";
  63. PreparedStatement stmt;
  64. try {
  65. stmt = connection.prepareStatement(sql);
  66. stmt.setString(1, tarefa.getDescricao());
  67. stmt.setBoolean(2, tarefa.isFinalizado());
  68. stmt.setDate(3, tarefa.getDataFinalizacao() != null ? new Date(
  69. tarefa.getDataFinalizacao().getTimeInMillis()) : null);
  70. stmt.setLong(4, tarefa.getId());
  71. stmt.execute();
  72. } catch (SQLException e) {
  73. throw new RuntimeException(e);
  74. }
  75. }
  76.  
  77. public List<Tarefa> lista() {
  78. try {
  79. List<Tarefa> tarefas = new ArrayList<Tarefa>();
  80. PreparedStatement stmt = this.connection
  81. .prepareStatement("select * from tarefas");
  82.  
  83. ResultSet rs = stmt.executeQuery();
  84.  
  85. while (rs.next()) {
  86. // adiciona a tarefa na lista
  87. tarefas.add(populaTarefa(rs));
  88. }
  89.  
  90. rs.close();
  91. stmt.close();
  92.  
  93. return tarefas;
  94. } catch (SQLException e) {
  95. throw new RuntimeException(e);
  96. }
  97. }
  98.  
  99. public Tarefa buscaPorId(Long id) {
  100.  
  101. if (id == null) {
  102. throw new IllegalStateException("Id da tarefa não deve ser nula.");
  103. }
  104.  
  105. try {
  106. PreparedStatement stmt = this.connection
  107. .prepareStatement("select * from tarefas where id = ?");
  108. stmt.setLong(1, id);
  109.  
  110. ResultSet rs = stmt.executeQuery();
  111.  
  112. if (rs.next()) {
  113. return populaTarefa(rs);
  114. }
  115.  
  116. rs.close();
  117. stmt.close();
  118.  
  119. return null;
  120. } catch (SQLException e) {
  121. throw new RuntimeException(e);
  122. }
  123. }
  124.  
  125. public void finaliza(Long id) {
  126.  
  127. if (id == null) {
  128. throw new IllegalStateException("Id da tarefa não deve ser nula.");
  129. }
  130.  
  131. String sql = "update tarefas set finalizado = ?, dataFinalizacao = ? where id = ?";
  132. PreparedStatement stmt;
  133. try {
  134. stmt = connection.prepareStatement(sql);
  135. stmt.setBoolean(1, true);
  136. stmt.setDate(2, new Date(Calendar.getInstance().getTimeInMillis()));
  137. stmt.setLong(3, id);
  138. stmt.execute();
  139. } catch (SQLException e) {
  140. throw new RuntimeException(e);
  141. }
  142. }
  143.  
  144. private Tarefa populaTarefa(ResultSet rs) throws SQLException {
  145. Tarefa tarefa = new Tarefa();
  146.  
  147. // popula o objeto tarefa
  148. tarefa.setId(rs.getLong("id"));
  149. tarefa.setDescricao(rs.getString("descricao"));
  150. tarefa.setFinalizado(rs.getBoolean("finalizado"));
  151.  
  152. // popula a data de finalizacao da tarefa, fazendo a conversao
  153. Date data = rs.getDate("dataFinalizacao");
  154. if (data != null) {
  155. Calendar dataFinalizacao = Calendar.getInstance();
  156. dataFinalizacao.setTime(data);
  157. tarefa.setDataFinalizacao(dataFinalizacao);
  158. }
  159. return tarefa;
  160. }
  161. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement