繁体   English   中英

无法在Servlet中设置ResultSet的属性

[英]Unable to set attribute of ResultSet in Servlet

在我的项目中,我将查询结果从数据库中获取到我的servlet中,并在为ResultSet设置了属性之后将结果传递给JSP,然后在JSP中对其进行了访问。但是,我能够以某种方式设置和访问我的所有其他属性JSP和Servlet(结果集除外)。

这是我的数据库访问函数getcart()

public static ResultSet getCart(int item,int cust){
ResultSet rs=null,res=null;
String i_name;
int price;
try{
    rs=stmnt.executeQuery("SELECT i_name,price FROM items WHERE i_id="+item);
    rs.next();
    i_name=rs.getString(1);
    price=rs.getInt(2);
    stmnt.executeUpdate("INSERT INTO cart(prod,price,c_id,i_id) VALUES('"+i_name+"',"+price+","+cust+","+item+")");
    res=stmnt.executeQuery("SELECT prod,price,count(*) FROM cart WHERE c_id="+cust+" GROUP BY i_id"); 
    res.next();
}catch(SQLException e){}
return res;

}

这是JSP

<%ResultSet r=(ResultSet)request.getAttribute("cart");%>
<div id="pageContent">
        <div style="margin:24px; text-align:left;"><br />
        <table width="100%" border="2" cellspacing="0" id ="table1">
            <tr>
            <td width="15%" bgcolor="#000000" align="center"><strong>Product</strong></td>
            <td width="10%" bgcolor="#000000" align="center"><strong>Price</strong></td>
            <td width="12%" bgcolor="#000000" align="center"><strong>Quantity</strong></td>
            <!--<td width="9%" bgcolor="#000000" align="center"><strong>Total</strong></td>-->
            </tr>
            <%while(r.next()){%>
            <tr class="spaceUnder">
                <td width="15%" bgcolor="#FFFFFF" align="center"><font color="#000"> <%=re.getString(1)%></font></td>
                    <td width="10%" bgcolor="#FFFFFF" align="center"><font color="#000"> <%=re.getInt(2)%></font></td>
            <td width="12%" bgcolor="#FFFFFF" align="center"><font color="#000"> <%=re.getInt(3)%> </font></td>
            </tr>
            <%}%>
        </table> 
            <div class="container">
        <left><h3 style="color:#FFFFF;padding-top:30px;"><font color="#000">Total: <%=total%> </font></h3></left>
            </div>
        </div>
</div>

这是servlet片段:

                res=accessDB.getCart(it,ci);
                request.setAttribute("cart",res);
                view=request.getRequestDispatcher("cart.jsp");
                view.forward(request, response);         

我不知道发生了什么事? 我现在花了很多时间,而且我的数据库不是空的,所以res.next()是有效的。

一件事情正在工作。当我使用getIntgetString在Servlet代码中分解ResultSet并将它们分别发送到JSP而不是发送整个ResultSet ,JSP中的getAttribute可以工作并且我可以打印我的结果。但是不要知道为什么整个ResultSet无法运行。我正在使用Netbeans 8.0.1。

请帮忙。

根据我的观察,我认为您的查询SELECT prod,price,count(*) FROM cart WHERE c_id="+cust+" GROUP BY i_id仅返回一行。

首先,您不应该使用res.next(); 在返回RESULTSET对象之前。 这是设计功能的错误方法。 getCart(int item,int cust)应该做的只是返回RESULTSET

我假设的问题,似乎在这里是,您的query仅返回一行。 当您调用res.next(); 在返回RESULTSET对象之前。 resultset cursor已经指向resultset cursor的第一行。

当您调用<%while(r.next()){%> ,这一次它<%while(r.next()){%> NULL ,因为结果集中的下一行不存在。 这就是为什么无法打印结果的原因。


我现在观察到的是,您在jsp代码中输入了错误。 <%=re.getString(1)%>应该是<%=r.getString(1)%>因为您正在创建<%ResultSet r=(ResultSet)request.getAttribute("cart");%> 结果集变量名为 r但您正在使用re 但是re在文件中不存在。 我认为JSP甚至不应该编译,因为从未定义过re变量。

s the reason it不起作用s the reason it

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM