Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package br.ufscar.dc.hospital.dao;
  2.  
  3.  
  4. import br.ufscar.dc.hospital.beans.Consultas;
  5. import br.ufscar.dc.hospital.beans.Paciente;
  6. import br.ufscar.dc.hospital.beans.Medico;
  7. import java.sql.Connection;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12. import java.util.ArrayList;
  13. import java.util.Date;
  14. import java.util.List;
  15. import javax.naming.NamingException;
  16. import javax.sql.DataSource;
  17.  
  18.  
  19. public class ConsultasDAO {
  20.  
  21.  
  22. private final static String CRIAR_CONSULTAS_SQL = "insert into Consultas"
  23. + " (cpf, crm, dataConsulta)"
  24. + " values (?,?,?)";
  25.  
  26.  
  27. /*
  28. private final static String LISTAR_CONSULTAS_SQL = "select"
  29. + " p.id as palpiteId, p.campeao, p.vice,"
  30. + " u.id as usuarioId, u.nome, u.email, u.telefone, u.dataDeNascimento"
  31. + " from Palpite p inner join Usuario u on p.palpiteiro = u.id";
  32. */
  33.  
  34.  
  35. private final static String LISTAR_CONSULTAS_POR_SELECAO_SQL = "select"
  36. + " c.cpf as consultasCpf, c.cpf, c.crm,"
  37. + " p.cpf as pacienteCpf, p.nome, p.telefone, p.sexo, p.dataDeNascimento"
  38. + " from Consultas c inner join Paciente u on c.paciente = p.cpf"
  39. + " where cpf = ?";
  40.  
  41.  
  42. DataSource dataSource;
  43.  
  44.  
  45. public ConsultasDAO(DataSource dataSource) {
  46. this.dataSource = dataSource;
  47. }
  48.  
  49.  
  50. public Consultas gravarConsultas(Consultas c) throws SQLException, NamingException {
  51. try (Connection con = dataSource.getConnection();
  52. PreparedStatement ps = con.prepareStatement(CRIAR_CONSULTAS_SQL);) {
  53. ps.setInt(1, c.getPaciente().getCpf());
  54. ps.setInt(2, c.getMedico().getCrm());
  55. ps.setDate(3, new java.sql.Date(c.getDataConsulta().getTime()));
  56. ps.execute();
  57. }
  58. return c;
  59. }
  60.  
  61. /*
  62. public List<Palpite> listarTodosPalpites() throws SQLException, NamingException {
  63. List<Palpite> ret = new ArrayList<>();
  64. try (Connection con = dataSource.getConnection();
  65. PreparedStatement ps = con.prepareStatement(LISTAR_PALPITES_SQL)) {
  66.  
  67.  
  68. try (ResultSet rs = ps.executeQuery()) {
  69. while (rs.next()) {
  70. Palpite p = new Palpite();
  71. Usuario u = new Usuario();
  72. p.setId(rs.getInt("palpiteId"));
  73. p.setCampeao(rs.getString("campeao"));
  74. p.setVice(rs.getString("vice"));
  75. u.setId(rs.getInt("usuarioId"));
  76. u.setNome(rs.getString("nome"));
  77. u.setEmail(rs.getString("email"));
  78. u.setTelefone(rs.getString("telefone"));
  79. u.setDataDeNascimento(new Date(rs.getDate("dataDeNascimento").getTime()));
  80. p.setPalpiteiro(u);
  81. ret.add(p);
  82. }
  83. }
  84. }
  85. return ret;
  86. }
  87. */
  88.  
  89. public List<Consultas> listarTodasConsultasPorSelecao(String selecao) throws SQLException {
  90. List<Consultas> ret = new ArrayList<>();
  91. try (Connection con = dataSource.getConnection();
  92. PreparedStatement ps = con.prepareStatement(LISTAR_CONSULTAS_POR_SELECAO_SQL)) {
  93.  
  94.  
  95. ps.setString(1, selecao);
  96. ps.setString(2, selecao);
  97. try (ResultSet rs = ps.executeQuery()) {
  98. while (rs.next()) {
  99.  
  100. //instanciando objetos
  101. Consultas c = new Consultas();
  102. Paciente p = new Paciente();
  103. Medico m = new Medico();
  104.  
  105. //setando Consultas
  106. c.getPaciente().setCpf(rs.getInt("consultasCPF"));
  107. c.getMedico().setCrm(rs.getInt("consultasCRM"));
  108. c.setDataConsulta(new Date(rs.getDate("dataDeNascimento").getTime()));
  109.  
  110. //setando Paciente
  111. p.setCpf(rs.getInt("cpf"));
  112. p.setNome(rs.getString("nome"));
  113. p.setSenha(rs.getString("senha"));
  114. p.setTelefone(rs.getString("telefone"));
  115. p.setSexo(rs.getString("sexo"));
  116. p.setDataDeNascimento(new Date(rs.getDate("dataDeNascimento").getTime()));
  117.  
  118. //setando Medico
  119. m.setCrm(rs.getInt("crm"));
  120. m.setNome(rs.getString("nome"));
  121. m.setSenha(rs.getString("senha"));
  122. m.setEspecialidade(rs.getString("especialidade"));
  123.  
  124. //atribuindo o Paciente e o Medico em Consultas
  125. c.setPaciente(p);
  126. c.setMedico(m);
  127. ret.add(c); }
  128. }
  129. }
  130. return ret;
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement