[英]Unable to set attribute of ResultSet in Servlet
In my project,I am getting the query result from the database into my servlet and passing the result to JSP after setting the attribute for the ResultSet
and then accessing it in JSP.But somehow i am able to set and access my all other attributes in JSP and servlets except the ResultSet. 在我的项目中,我将查询结果从数据库中获取到我的servlet中,并在为
ResultSet
设置了属性之后将结果传递给JSP,然后在JSP中对其进行了访问。但是,我能够以某种方式设置和访问我的所有其他属性JSP和Servlet(结果集除外)。
So here is my database access function getcart() 这是我的数据库访问函数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;
}
Here is the JSP 这是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>
And here is the servlets snippet: 这是servlet片段:
res=accessDB.getCart(it,ci);
request.setAttribute("cart",res);
view=request.getRequestDispatcher("cart.jsp");
view.forward(request, response);
I dont know WHAT is happening?? 我不知道发生了什么事? I have now spent a lot of time on this.And my database is not empty.So res.next() is valid.
我现在花了很多时间,而且我的数据库不是空的,所以res.next()是有效的。
One thing is working.When i break down my ResultSet
in my servlets code using getInt
and getString
,and then send these to JSP individually instead of sending the whole ResultSet
,then getAttribute
in JSP works and i can print my result.But don't know why the whole ResultSet
is not going.I am using Netbeans 8.0.1. 一件事情正在工作。当我使用
getInt
和getString
在Servlet代码中分解ResultSet
并将它们分别发送到JSP而不是发送整个ResultSet
,JSP中的getAttribute
可以工作并且我可以打印我的结果。但是不要知道为什么整个ResultSet
无法运行。我正在使用Netbeans 8.0.1。
Please Help. 请帮忙。
From my observation, I think your Query SELECT prod,price,count(*) FROM cart WHERE c_id="+cust+" GROUP BY i_id
returns only one row. 根据我的观察,我认为您的查询
SELECT prod,price,count(*) FROM cart WHERE c_id="+cust+" GROUP BY i_id
仅返回一行。
First of all you should not use res.next();
首先,您不应该使用
res.next();
just before returning RESULTSET
object. 在返回
RESULTSET
对象之前。 This is a wrong way of designing a function. 这是设计功能的错误方法。 What
getCart(int item,int cust)
should do is just return the RESULTSET
. getCart(int item,int cust)
应该做的只是返回RESULTSET
。
Problem that I assume , that seems to be here is that, your query
just returns one row. 我假设的问题,似乎在这里是,您的
query
仅返回一行。 As you call res.next();
当您调用
res.next();
just before returning the RESULTSET
object. 在返回
RESULTSET
对象之前。 The resultset cursor
already points to the first row of the result. resultset cursor
已经指向resultset cursor
的第一行。
When you call <%while(r.next()){%>
, this time it gets NULL
, because next row in resultset is non-existent. 当您调用
<%while(r.next()){%>
,这一次它<%while(r.next()){%>
NULL
,因为结果集中的下一行不存在。 That`s why result cannot be printed. 这就是为什么无法打印结果的原因。
What I have observed now that you have made a typo in you jsp code. 我现在观察到的是,您在jsp代码中输入了错误。
<%=re.getString(1)%>
what it should be is <%=r.getString(1)%>
beacause you are creating <%ResultSet r=(ResultSet)request.getAttribute("cart");%>
Resultset variable named r
but you are using re
. <%=re.getString(1)%>
应该是<%=r.getString(1)%>
因为您正在创建<%ResultSet r=(ResultSet)request.getAttribute("cart");%>
结果集变量名为 r
但您正在使用re
。 But re
does not exist in the file. 但是
re
在文件中不存在。 I think JSP should not even compile, because re
variable is never defined. 我认为JSP甚至不应该编译,因为从未定义过
re
变量。
That s the reason it
s not working. 这
s the reason it
不起作用s the reason it
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.