无法显示从Servlet到JSP的ArrayList

时间:2018-11-15 21:15:01

标签: java jsp ms-access servlets intellij-idea

我正在使用servlet和jsp来显示Access数据库。

MainDatabase.java

    public class MainDatabase
    {
        private int proId;
        private String proName;
        private String proQuantity;
        private String proPrice;

        public MainDatabase()
        {
            this.proId = 0;
            this.proName = "";
            this.proQuantity = "";
            this.proPrice = "";
        }

        public int getProId() {
            return proId;
        }

        public void setProId(int proId) {
            this.proId = proId;
        }

        public String getProName() {
            return proName;
        }

        public void setProName(String proName) {
            this.proName = proName;
        }

        public String getProQuantity() {
            return proQuantity;
        }

        public void setProQuantity(String proQuantity) {
            this.proQuantity = proQuantity;
        }

        public String getProPrice() {
            return proPrice;
        }

        public void setProPrice(String proPrice) {
            this.proPrice = proPrice;
        }
    }

MainDatabaseUtil.java

    import org.hsqldb.Database;

    import java.io.*;
    import java.sql.*;
    import java.util.*;
    import java.util.ArrayList;
    import java.util.List;
    import org.json.simple.JSONObject;
    import org.json.simple.JSONArray;


    public class MainDatabaseUtil
    {
        public static List<MainDatabase> getMainDB() throws Exception
        {
            List<MainDatabase> mainDatabases = null;
            JSONArray payload = null;
            try
            {
                Connection conn = DriverManager.getConnection("jdbc:ucanaccess://D:\\study_materials\\Java\\Resources\\Database\\smsDemo.accdb");
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery("SELECT * from Database");

                stmt.close();
                conn.close();

                mainDatabases = new ArrayList<>();

                //payload = new JSONArray();

                if(rs != null)
                {
                    while(rs.next())
                    {
                        MainDatabase proDB = new MainDatabase();
                        proDB.setProId(rs.getInt("ID"));
                        proDB.setProName(rs.getString("ProName"));
                        proDB.setProQuantity(rs.getString("Quantity"));
                        proDB.setProPrice(rs.getString("Price"));

                        mainDatabases.add(proDB);
                    }
                }

                return mainDatabases;
                //return payload;
            }

            catch(Exception el)
            {
                return mainDatabases;
            }
        }
    }

MainDatabaseServlet.java

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.SQLException;
    import java.util.List;

    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.json.simple.JSONObject;
    import org.json.simple.JSONArray;
    import javax.servlet.*;

    @WebServlet("/MainDatabaseServlet")

    public class MainDatabaseServlet extends HttpServlet
    {
        private static final long serialVersionUID = 1L;

        public MainDatabaseServlet()
        {
            super();
        }

        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            try
            {
                List<MainDatabase> mainDatabases = MainDatabaseUtil.getMainDB();
                request.setAttribute("main_list", mainDatabases);
                RequestDispatcher dispatcher = request.getRequestDispatcher("/MainDatabaseView.jsp");
                dispatcher.forward(request, response);
            }
            catch (Exception el)
            {
                el.printStackTrace();
            }
        }

        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
        {
            doGet(request, response);
        }
    }

MainDatabaseView.jsp

    <%--
      Created by IntelliJ IDEA.
      User: Xenon
      Date: 11/15/2018
      Time: 9:15 PM
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ page import ="java.util.ArrayList"%>
    <%@ page import ="java.util.List"%>

    <html>
    <head>
        <title>Main Database</title>
    </head>
    <body>
        <h2>Student Table Demo</h2><br>
        <hr>
        <br>
        <table border="1">

            <tr>
                <th>ID</th>
                <th>NAME</th>
                <th>QUANTITY</th>
                <th>PRICE</th>
            </tr>

            <c:forEach var="tempData" items="${main_list}">
                <tr>
                    <td>${tempData.proId}</td>
                    <td>${tempData.proName}</td>
                    <td>${tempData.proQuantity}</td>
                    <td>${tempData.proPrice}</td>
                </tr>
            </c:forEach>

        </table>
    </body>
    </html>

我正在使用IntelliJ Idea。它表明在MainDatabaseView.jsp中,它无法解析变量main_list。

当前输出:其显示表列名称,但其中没有数据。

enter image description here

程序运行时,预期输出应在表视图中显示数据库值。

请帮助。谢谢。

1 个答案:

答案 0 :(得分:1)

stmt.close();检索值后,调用conn.close();ResultSet

https://docs.oracle.com/javase/tutorial/jdbc/basics/processingsqlstatements.html

  

完成使用Statement后,调用方法Statement.close立即释放其正在使用的资源。调用此方法时,其ResultSet对象将关闭。

如果在检索结果集之前关闭该语句,则结果集将不再对数据有任何引用。