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.
So here is my database access function 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
<%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:
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.
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.
Please Help.