为什么从Postgresql检索数据不起作用?

时间:2019-07-02 07:25:56

标签: java jsp servlets jstl

所以我制作了一个药品网页,我们应该在该网页上查看数据库中的药品清单,并可以在数据库中插入新药品。 插入功能正常工作,因为它不需要c:forEach和c:out。 列出药品是行不通的,c:forEach部分内部有问题。但是没有错误! 我的servlet工作良好,因为在jsp中我打印出了要控制台的对象和列表的大小,但它不是空的,我可以看到它们。

我认为我做了网上阅读的所有内容,但仍然无法正常工作。 您也可以看到我的代码。

jsp:// ofc我跳过了html,头部,身体等部位...

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="webApp.model.Medicines" import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<div>
            <h2>Retrieve data from medicines</h2>

            <table border="2" align="center">
                <tr>
                    <th>ID</th>
                    <th>NAME</th>
                    <th>DESCRIPTION</th>
                    <th>PATIENT ID</th>


                </tr>
                <%
                    List<Medicines> medicineList = (List<Medicines>) request.getAttribute("MedicineList");
                %>
                <c:forEach items="${medicineList}" var="medicine">

                    <tr>
                        <td><c:out value="${medicine.medID}" /></td>
                        <td><c:out value="${medicine.medName}" /></td>
                        <td><c:out value="${medicine.description}" /></td>
                        <td><c:out value="${medicine.patientID}" /></td>
                    </tr>
                </c:forEach>

            </table>

        </div>

servlet://也有一个doPost方法,但是它对于插入函数起作用,所以我从这里跳过了

@WebServlet(name = "Medicines", urlPatterns = "/medicines")

public class ServletM extends HttpServlet {

    private static final long serialVersionUID = -2644163711496279913L;
    DB db = DB.getInstance();
    List<Medicines> medicineList = null;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");

        try {
            medicineList = db.getAllMedicine();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("MedicineList", medicineList);
        RequestDispatcher dispatcher = request.getRequestDispatcher("pages/Medicines.jsp");
        if (dispatcher != null) {
            dispatcher.forward(request, response);
        }

    }
}

db://仅方法

public List<Medicines> getAllMedicine() throws ClassNotFoundException, SQLException {
        List<Medicines> medicineList = new ArrayList<Medicines>();

        try {
            if (conn != null) {
                String query = "SELECT * FROM \"Medicines\"";
                pstmt = conn.prepareStatement(query);
                rs = pstmt.executeQuery();
                if (rs.next() == false) {
                    System.out.println("ResultSet in empty in Java");
                }

                while (rs.next()) {

                    int medID = rs.getInt("MedicineID");
                    String medName = rs.getString("MedName");
                    String description = rs.getString("Description");
                    int patientID = rs.getInt("PatientID");

                    Medicines medicine = new Medicines(medID, medName, description, patientID);
                    medicineList.add(medicine);
                }

            } else {
                System.out.println("Connection is not created! 1");
            }
        } catch (SQLException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {

            if (pstmt != null) {
                pstmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        }
        return medicineList;
    }

0 个答案:

没有答案