【发布时间】:2016-02-17 14:36:47
【问题描述】:
我试图在从我的 Oracle 数据库中检索数据的表中显示数据,但它不会显示。
这是我正在使用的代码。
Servlet 列出数据:
package org.sistemamedico.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.sistemamedico.db.Conexion;
@WebServlet("/ServletListarMedicamento.do")
public class ServletListarMedicamento extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
RequestDispatcher despachador = null;
req.setAttribute("listaMedicamento", Conexion.getInstancia().listar("From Medicamento"));
despachador = req.getRequestDispatcher("sistemamedico/farmacia.jsp");
despachador.forward(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
}
使用列表方法连接数据库:
package org.sistemamedico.db;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Conexion {
private SessionFactory sesion;
private static Conexion instancia;
public static synchronized Conexion getInstancia(){
return (instancia==null)?new Conexion():instancia;
}
public Conexion(){
try {
sesion=new Configuration().configure().buildSessionFactory();
} catch (Throwable e) {
// TODO: handle exception
e.printStackTrace();
}
}
public void closeSession()throws HibernateException {
try {
if (sesion.isClosed()==false | sesion.getCurrentSession().isOpen()) {
sesion.close();
sesion.getCurrentSession().close();
}
} catch (HibernateException ex) {
throw new HibernateException(ex);
}
}
public List<Object> listar(String consulta){
Session miSesion=sesion.getCurrentSession();
List<Object> lista=null;
miSesion.beginTransaction();
lista=miSesion.createQuery(consulta).list();
miSesion.getTransaction().commit();
return lista;
}
public List<Object> autenticarUsuario(String nick,String contraseña){
Session miSesion=sesion.getCurrentSession();
List<Object> lista=null;
miSesion.beginTransaction();
lista=miSesion.createQuery("From Usuario u where u.nick='"+nick+"' and contraseña='"+contraseña+"'").list();
miSesion.getTransaction().commit();
return lista;
}
public void agregar (Object agregar){
Session session=sesion.getCurrentSession();
session.beginTransaction();
session.save(agregar);
session.getTransaction().commit();
}
public Object buscar(Class<?> clase,int id){
Session session=sesion.getCurrentSession();
session.beginTransaction();
Object obj=session.get(clase, id);
session.getTransaction().commit();
return obj;
}
public Object buscar(Class<?> clase,String id){
Session session=sesion.getCurrentSession();
session.beginTransaction();
Object obj=session.get(clase, id);
session.getTransaction().commit();
return obj;
}
}
我在 JSP 中的表格:
<table class="table table-hover">
<thead>
<tr>
<th>Nombre</th>
<th>Descripcion</th>
<th>Proveedor</th>
<th>Fecha Vencimiento</th>
<th>Precio</th>
</tr>
</thead>
<tbody>
<c:forEach items="${listaMedicamento}" var="medicamento">
<tr>
<td>${medicamento.getNombre()}</td>
<td>${medicamento.getDescripcion()}</td>
<td>${medicamento.getIdProveedor().getNombre()}</td>
<td>${medicamento.getFechaVencimiento()}</td>
<td>${medicamento.getPrecio()}</td>
</tr>
</c:forEach>
</tbody>
</table>
我已经放了 taglib,所以 c:foreach 可以工作,但它不会显示任何数据。
【问题讨论】:
-
您确定您的 SQL“From”语句正在检索数据吗?