• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp:compare select statement error

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone

I am doing my project on a shopping cart. i have a little problem! ive got 2 tables..products and tempcart. The products table contains the fields idnumber and all the rest of the product information (such as title, description). The tempcart table has 2 fields. sessionid and idnumber. Each time a user selects an item, the idnumber and the sessionid is inserted into the tempcart. It works so far!! however the problem is i want to find out what products has that particular user selected during that session not products selected by all users. At the moment when the user selects a product they want to purchase i get a blank page of the cart and does not show the product details. the code i am usinmg is as follows:

<%@ page import="java.sql.*, java.util.*, java.io.* " session="true" %>

<%
Class.forName("com.mysql.jdbc.Driver").newInstance();

java.sql.Connection con1 = null;

Statement stmt1 = null;
ResultSet rs1 = null;
String queryStr1 = null;
String usernamereg1 = request.getParameter("usernamereg");
String strsessionid = request.getParameter("sessionid");
try{
con1 = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/databasename?user=user&password=password");

stmt1 = con1.createStatement();
queryStr1 = "select * from products prod, tempcart cart where prod.idnumber=cart.idnumber AND cart.sessionid = '"+ strsessionid +"'";
rs1 = stmt1.executeQuery(queryStr1);
%>
<html>


User <font color = "red"><b><%=usernamereg1%></font></b> Logged In
<center>
<font size=5>Shopping Cart</font>
</center>
<center>

<br>
<form action="deletecart.jsp" method=post>
<br>
<table cellpadding=2 cellspacing=2 align="right" border=1 width="70%">
<tbody>
<tr>
<th>Select</th>

<th>Title</th>
<th>Description</th>
<th>Category</th>
<th>Price</th>
<th>Image</th>
<th>Remove</th>
</tr>
<%
while (rs1.next())
{
String idnumber = rs1.getString(1);
String title = rs1.getString(2);
String description = rs1.getString(3);
String category = rs1.getString(5);
String price = rs1.getString(6);
String image = rs1.getString(7);


%>

<tr><td valign = 'Top'><input type="radio" name="dvddownload" value="<%=idnumber%>"</br></td>
<td valign = 'Top'> <%=title%> </td>
<td valign = 'Top'> <%=description%> </td>
<td valign = 'Top'> <%=category%> </td>
<td valign = 'Top'> <%=price%> </td>
<td valign = 'Top'> <img src="<%=image%>" border=1 width=90 height=120"> </td>
<td valign = 'Top'><input type="submit" name="removetempcart" value="Remove" </td>

</tr>

<%
} // end while ()
}catch(SQLException s) {%> <%= s%> <% }
finally{
//clean up.
try{
if (rs1!=null) rs1.close();
if (stmt1!=null) stmt1.close();
if (con1!=null) con1.close();
}catch (SQLException s) {}

}
%>
</table>
<br>
<td align=center colspan=2>

</td>
</tr>


</center>

</form>
<form action="check2.html" method=post>
<input type="Submit" value="Checkout">
</form>
<form action="list.jsp" method=post>

<input type="submit" value="Continue Shopping">

<input type="hidden" name="sessionid" value="<%=session.getId()%>"

</form>

</html>


i have got the sessionid as a hidden field, convert it to a string and compare it in the select statement.but it still doesnt work. I would really appreciate if anyone can help me with this.

thanks shaz!!!
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two questions:

1.
Could the problem be on any of your other pages ? In particular, it might help if you list your "list.jsp" (assuming that's the one that allows users to add items to their shopping cart).

2.
Is there a special reason why you chose to use a hidden field to deliver the sessionID to/from the browser ? To the best of my knowledge this approach is a bit un-usual : people usually prefer to just call:

and rely on the session ID to travel *automatically* between client and server, relying on either cookies or "url rewrites" (it does, however, mean that either you ask your users to enable cookies on their browser, or you use the "encodeURL" statement whenever you render a link).
 
shaz akhtar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks so much that worked
im passing most values that i need from one jsp page to another in hidden fields, is there are a better way of doing it?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For starters, there's no need to pass the sessionId this way.
Use:
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Passing parameters between pages:
it might be useful to store shared data under your "session" (or under "application", if you'd like the datum to be viewed by *all users*, not just by the user who entered it).
A simple example : http://www.jsptut.com/Sessions.jsp

you might also want to have a look at the apache Struts framework (or the new "Java Server Faces" ).
It involves *considerably* more work (deploying jar files, creating java classes, editing xml files), and I would *not* recommend it for a very simple test program. But my company does use it for "real world" applications, where it proved priceless.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic