Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.ufscar.dc.hospital.dao;
- import br.ufscar.dc.hospital.beans.Consultas;
- import br.ufscar.dc.hospital.beans.Paciente;
- import br.ufscar.dc.hospital.beans.Medico;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- public class ConsultasDAO {
- private final static String CRIAR_CONSULTAS_SQL = "insert into Consultas"
- + " (cpf, crm, dataConsulta)"
- + " values (?,?,?)";
- /*
- private final static String LISTAR_CONSULTAS_SQL = "select"
- + " p.id as palpiteId, p.campeao, p.vice,"
- + " u.id as usuarioId, u.nome, u.email, u.telefone, u.dataDeNascimento"
- + " from Palpite p inner join Usuario u on p.palpiteiro = u.id";
- */
- private final static String LISTAR_CONSULTAS_POR_SELECAO_SQL = "select"
- + " c.cpf as consultasCpf, c.cpf, c.crm,"
- + " p.cpf as pacienteCpf, p.nome, p.telefone, p.sexo, p.dataDeNascimento"
- + " from Consultas c inner join Paciente u on c.paciente = p.cpf"
- + " where cpf = ?";
- DataSource dataSource;
- public ConsultasDAO(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- public Consultas gravarConsultas(Consultas c) throws SQLException, NamingException {
- try (Connection con = dataSource.getConnection();
- PreparedStatement ps = con.prepareStatement(CRIAR_CONSULTAS_SQL);) {
- ps.setInt(1, c.getPaciente().getCpf());
- ps.setInt(2, c.getMedico().getCrm());
- ps.setDate(3, new java.sql.Date(c.getDataConsulta().getTime()));
- ps.execute();
- }
- return c;
- }
- /*
- public List<Palpite> listarTodosPalpites() throws SQLException, NamingException {
- List<Palpite> ret = new ArrayList<>();
- try (Connection con = dataSource.getConnection();
- PreparedStatement ps = con.prepareStatement(LISTAR_PALPITES_SQL)) {
- try (ResultSet rs = ps.executeQuery()) {
- while (rs.next()) {
- Palpite p = new Palpite();
- Usuario u = new Usuario();
- p.setId(rs.getInt("palpiteId"));
- p.setCampeao(rs.getString("campeao"));
- p.setVice(rs.getString("vice"));
- u.setId(rs.getInt("usuarioId"));
- u.setNome(rs.getString("nome"));
- u.setEmail(rs.getString("email"));
- u.setTelefone(rs.getString("telefone"));
- u.setDataDeNascimento(new Date(rs.getDate("dataDeNascimento").getTime()));
- p.setPalpiteiro(u);
- ret.add(p);
- }
- }
- }
- return ret;
- }
- */
- public List<Consultas> listarTodasConsultasPorSelecao(String selecao) throws SQLException {
- List<Consultas> ret = new ArrayList<>();
- try (Connection con = dataSource.getConnection();
- PreparedStatement ps = con.prepareStatement(LISTAR_CONSULTAS_POR_SELECAO_SQL)) {
- ps.setString(1, selecao);
- ps.setString(2, selecao);
- try (ResultSet rs = ps.executeQuery()) {
- while (rs.next()) {
- //instanciando objetos
- Consultas c = new Consultas();
- Paciente p = new Paciente();
- Medico m = new Medico();
- //setando Consultas
- c.getPaciente().setCpf(rs.getInt("consultasCPF"));
- c.getMedico().setCrm(rs.getInt("consultasCRM"));
- c.setDataConsulta(new Date(rs.getDate("dataDeNascimento").getTime()));
- //setando Paciente
- p.setCpf(rs.getInt("cpf"));
- p.setNome(rs.getString("nome"));
- p.setSenha(rs.getString("senha"));
- p.setTelefone(rs.getString("telefone"));
- p.setSexo(rs.getString("sexo"));
- p.setDataDeNascimento(new Date(rs.getDate("dataDeNascimento").getTime()));
- //setando Medico
- m.setCrm(rs.getInt("crm"));
- m.setNome(rs.getString("nome"));
- m.setSenha(rs.getString("senha"));
- m.setEspecialidade(rs.getString("especialidade"));
- //atribuindo o Paciente e o Medico em Consultas
- c.setPaciente(p);
- c.setMedico(m);
- ret.add(c); }
- }
- }
- return ret;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement