在表JSP中显示数据库

时间:2016-12-04 14:56:44

标签: java jsp servlets

我正在尝试从我的数据库中检索数据,并使用jsp和servlet在表中显示它。我创建了表并从数据库中检索数据,但它没有显示在表中。我不确定是什么问题。到目前为止,这是我的代码:

JSP页面:

<h1>List of Cars in Inventory</h1>
        <form name="table" action="Inventory" method="get">
        <table>
            <tr>
                <td>Car ID</td>
                <td>Car Make</td>
                <td>Car Model</td>
                <td>Car Year</td>
                <td>Transmission Type</td>
                <td>Rate Per Mile</td>
                <td>Rate Per Day</td>
                <td>Availability</td>
            </tr>

            <c:forEach var="vehicle" items="${list}">
                <tr>
                    <td><c:out value="${vehicle.id}"/></td>
                    <td><c:out value="${vehicle.make}"/></td>
                    <td><c:out value="${vehicle.model}"/></td>
                    <td><c:out value="${vehicle.year}"/></td>
                </tr>
            </c:forEach>

        </table>
        </form>

Servlet页面:

public class Inventory extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        List<Vehicles> list = InventoryService.getListOfVehicles();
        request.setAttribute("list", list);
        request.getRequestDispatcher("inventory.jsp").forward(request,response);
    }

数据库类:

public static List<Vehicles> getListOfVehicles()
    {
        Vehicles vehicles = new Vehicles();
        List<Vehicles> vehicleList = new ArrayList<>();
        try
        {
            Connection conn = DBConnection.getConnection();

            String query = "SELECT * FROM Vehicles";
            PreparedStatement stmt = conn.prepareStatement(query);
            ResultSet rs = stmt.executeQuery();

            while(rs.next())
            {
                vehicles.setId(rs.getInt("id"));
                vehicles.setMake(rs.getString("make"));
                vehicles.setModel(rs.getString("model"));
                vehicles.setYear(rs.getString("year"));
                vehicles.setRatePerMile(rs.getString("ratepermile"));
                vehicles.setRatePerDay(rs.getString("rateperday"));
                vehicles.setTaken(rs.getString("taken"));
                vehicleList.add(vehicles);
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }

        return vehicleList;
    }

1 个答案:

答案 0 :(得分:0)

您应该将新创建的对象添加到while块中的vehicleList。

public static List<Vehicles> getListOfVehicles()
{
    // remove from here
    //Vehicles vehicles = new Vehicles();

    List<Vehicles> vehicleList = new ArrayList<>();
    try
    {
        Connection conn = DBConnection.getConnection();

        String query = "SELECT * FROM Vehicles";
        PreparedStatement stmt = conn.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();

        while(rs.next())
        {
            //create object here
            Vehicles vehicle = new Vehicles();

            vehicle.setId(rs.getInt("id"));
            vehicle.setMake(rs.getString("make"));
            vehicle.setModel(rs.getString("model"));
            vehicle.setYear(rs.getString("year"));
            vehicle.setRatePerMile(rs.getString("ratepermile"));
            vehicle.setRatePerDay(rs.getString("rateperday"));
            vehicle.setTaken(rs.getString("taken"));
            vehicleList.add(vehicle);
        }
    }
    catch (Exception e)
    {
        System.out.println(e);
    }

    return vehicleList;
}

还要考虑将您的JavaBean类从车辆重命名为单数车辆