Foros del Web » Programación para mayores de 30 ;) » Java »

Mostrar data desde 2 tablas usando 1 servlet, consultas HQL y 2 sesiones

Estas en el tema de Mostrar data desde 2 tablas usando 1 servlet, consultas HQL y 2 sesiones en el foro de Java en Foros del Web. Necesito ayuda pues necesito mostrar la data consultada desde 2 tablas, ya logré mostrarlas por separado pero mi punto es extraer la data desde ambas ...
  #1 (permalink)  
Antiguo 24/11/2012, 07:49
 
Fecha de Ingreso: noviembre-2012
Mensajes: 2
Antigüedad: 11 años, 5 meses
Puntos: 0
Pregunta Mostrar data desde 2 tablas usando 1 servlet, consultas HQL y 2 sesiones

Necesito ayuda pues necesito mostrar la data consultada desde 2 tablas, ya logré mostrarlas por separado pero mi punto es extraer la data desde ambas en una misma acción

Código del servlet:
Cita:
if(accion.equals("LISTAR")){
//capturando datos ingresados en inputs
String CodPaciente=request.getParameter("CodPaciente");
String CodHistorial=request.getParameter("CodHistorial");

//En ambos casos quiero extraer la data de ambas tablas ingresando un único parámetro
if(!CodPaciente.equalsIgnoreCase("")){
PacienteBD pb=new PacienteBD();
Paciente p=pb.buscar(CodPaciente);

HistorialBD hb=new HistorialBD();
Historial h=hb.buscarPaciente(CodPaciente);
if(p!=null){
//Aqui inicio las sesiones
request.getSession().setAttribute("paciente", p);
request.getSession().setAttribute("historial", h);
response.sendRedirect("Registro.jsp");
}else{
request.getSession().setAttribute("x","No existe el usuario");
response.sendRedirect("Mensaje.jsp");
}
}else if(!CodHistorial.equalsIgnoreCase("")){
HistorialBD hb=new HistorialBD();
Historial h=hb.buscar(CodHistorial);

PacienteBD pb=new PacienteBD();
Paciente p=pb.buscarHistorial(CodHistorial);
if(h!=null){
request.getSession().setAttribute("historial", h);
request.getSession().setAttribute("paciente", p);
response.sendRedirect("Registro.jsp");
}else{
request.getSession().setAttribute("x","No existe el usuario");
response.sendRedirect("Mensaje.jsp");
}
}
}
Código para buscar la data de Historial:
Cita:
public Historial buscar(String Codhistorial){
Historial h;
EntityManager em=getEm();
try{
h=em.getReference(Historial.class,Codhistorial);
}catch (Exception e){
h=null;
}
return h;
}

public Historial buscarPaciente(String Codpaciente){
Historial h;
EntityManager em=getEm();
try{
Query q=em.createQuery("SELECT h FROM Historial h WHERE h.codhistorial ='"+Codpaciente+"'");
h=(Historial)q.getSingleResult();
}catch (Exception e){
h=null;
}
return h;
}
Código para encontrar la data de Paciente:
Cita:
public Paciente buscar(String CodPaciente){
Paciente p;
EntityManager em=getEm();
try{
p=em.getReference(Paciente.class,CodPaciente);
}catch (Exception e){
p=null;
}
return p;
}

public Paciente buscarHistorial(String CodHistorial){
Paciente p;
EntityManager em=getEm();
try{
Query q=em.createQuery("SELECT p FROM Paciente p INNER JOIN Historial h WHERE h.codhistorial ='"+CodHistorial+"'");
//Aqui también tengo un problema porque esta línea parece no estar bien aunque tampoco sé cómo
p=(Paciente)q.getSingleResult();
}catch (Exception e){
p=null;
}
return p;
}
Código extracción de datos de las sesiones:
Cita:
<% Paciente p=(Paciente)session.getAttribute("paciente");
String ape=p.getApellidopaciente().toString();
String paterno=ape.substring(0,ape.indexOf(" "));
String materno=ape.substring(paterno.length()+1);
String cod=p.getCodpaciente().toString();
String estado=p.getEstadocivil().toString();
String nom=p.getNombrepaciente().toString();
String numdoc=p.getNumdocumento().toString();
String ocu=p.getOcupacion().toString();
String sexo=p.getSexo().toString();
String tipodoc=p.getTipodocumento().toString();
String fecha=null,dia=null,mes=null;
int year,month,day;
year=p.getFechanacimiento().getYear();
if (year < 1000) year+=1900;
month=p.getFechanacimiento().getMonth()+1;
mes=String.valueOf(month);
if(month<10) mes="0"+mes;
day=p.getFechanacimiento().getDate();
dia=String.valueOf(day);
if (day<10) dia="0"+dia;
fecha=year+"-"+mes+"-"+dia;
String fecnac=fecha;
Historial h=(Historial)session.getAttribute("historial");
String codhist=h.getCodhistorial().toString();
%>

Etiquetas: data, jsp, servlet, sesiones, string, tablas, usando
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:36.