【问题标题】:displaying resultset values from servlet to jsp page将结果集值从 servlet 显示到 jsp 页面
【发布时间】:2014-03-21 09:41:16
【问题描述】:

我正在通过 servlet 从数据库中获取数据,然后将其设置为结果集。现在我想在 jsp 页面上显示数据。为此,我使用列表来设置所有数据,并在 jsp 页面上显示 jstl。但我得到两列相同的数据。 这是代码..

服务代码:

ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {

                wb.setDeviceAccount(rs.getString("accountID"));
                wb.setVehicleId(rs.getString("deviceID"));
                wb.setSimNumber(rs.getString("simID"));
                wb.setImeiNumber(rs.getString("imeiNumber"));
                wb.setLastTimestamp(rs.getString("lastGPSTimestamp"));
                wb.setLastLoginTime(rs.getString("lastUpdateTime"));
                wb.setExpirationTime(rs.getString("expirationTime"));
                list.add(wb);

            }
            rs.close();
            stmt.close();
            con.close();
            request.setAttribute("deviceList", list);

            RequestDispatcher requestDispatcher = request.getRequestDispatcher("/installedDeviceList.jsp");
            requestDispatcher.forward(request, response);

JSP 页面:

<c:forEach items="${deviceList}" var="dList">
                <tr>
                    <td></td>
                    <td>${dList.deviceAccount}</td>
                    <td>${dList.vehicleId}</td>
                    <td><a href="#" onclick="toggle();">${dList.simNumber}</a></td>
                    <td>${dList.imeiNumber}</td>
                    <td>${dList.lastTimestamp}</td>
                    <td>${dList.lastLoginTime}</td>
                </tr>
            </c:forEach>

【问题讨论】:

  • 快速回复将不胜感激......谢谢......
  • 我知道已经有一段时间了,但是“wb”与什么有关?

标签: java servlets jstl


【解决方案1】:

在你的循环中总是创建新的 Wb 引用。 这里我假设 Wb 是您的班级名称。您可以根据您的班级名称更改代码。

Wb wb=null;


ResultSet rs = stmt.executeQuery(query);    
            while (rs.next()) {   

                wb =new Wb();  

                wb.setDeviceAccount(rs.getString("accountID"));
                wb.setVehicleId(rs.getString("deviceID"));
                wb.setSimNumber(rs.getString("simID"));
                wb.setImeiNumber(rs.getString("imeiNumber"));
                wb.setLastTimestamp(rs.getString("lastGPSTimestamp"));
                wb.setLastLoginTime(rs.getString("lastUpdateTime"));
                wb.setExpirationTime(rs.getString("expirationTime"));
                list.add(wb);

            }

【讨论】:

    【解决方案2】:

    您显然只创建了一个对象,而您应该为每一行创建一个唯一的“wb”实例。您没有两个重复的行,您只需将完全相同的对象多次添加到列表中。

    【讨论】:

    • 那么我怎样才能创建不同的...?我只能设置一次while循环。
    • 神秘,当你甚至不了解如何创建和使用对象实例时,你怎么能编写使用 JDBC 并且在 Web 应用程序中的代码?
    【解决方案3】:

    对于结果集中的每一行,创建一个新的wb 对象来存储来自结果集中的数据并将其添加到列表中。

    示例

    while ( rs.next() ) {
        // set this to equivalent class on your wb variable.
        // wb = new wb();
    
        wb.setDeviceAccount( rs.getString( "accountID" ) );
        // read other column values here and set in wb
        // ...
    
        // lastly
        list.add( wb );
    } // while rs
    

    然后在您的网页上,您将从列表中正确读取每条记录。

    【讨论】:

    • @AshishSrivastava:你没有得到什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多