File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes shopping cart problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "shopping cart problem" Watch "shopping cart problem" New topic

shopping cart problem

Donna Harrington
Ranch Hand

Joined: Aug 08, 2003
Posts: 31
Hi everybody,
I"m having fierce problems with my add to cart screen in my shopping cart. I want a customer to be able to buy a product and put it in cart and continue shopping.I"m using just jsp (no classes) here is my code..i dont know if my sql statement is right also to buy a product
When the customer buys products i want them to be given an order id automatically..order id is set to auto-increment in my order table.
i hope someone can point me in the right direction.
here is my code for 'add to cart' screen.

<head><title>Add to Cart</title></head>

<BODY BGCOLOR="#228B22">
<form action="thankyou.jsp" method = "POST">

<% String notEntered = "";
String connectionURL ="jdbc:mysql://localhost:3306/petshopwebsite";
String driver= "com.mysql.jdbc.Driver";
Connection c = null;
ResultSet rs = null;
Statement st = null;
String password = "";
String username ="root";
Vector userCart;

c = DriverManager.getConnection(connectionURL,username, password);
st = c.createStatement();

String productId = request.getParameter("productId");
String PName = request.getParameter("PName");
String price = request.getParameter("Price");
Properties product = new Properties();

PreparedStatement p = c.prepareStatement("SELECT PName,Price FROM product WHERE productId='"+ productId +"'");

if (session.getAttribute("userCart") == null) { //no products added yet
userCart = new Vector();
userCart = (Vector) session.getAttribute("userCart");

productId = rs.getString(1);
product.setProperty("productId", productId);

productId = rs.getString(2);
product.setProperty("PName", PName);

price = rs.getString(3);
product.setProperty("Price", price);

session.setAttribute("userCart", userCart);

userCart = (Vector) session.getAttribute("userCart");
Iterator iter = userCart.iterator();

while (iter.hasNext()) {

Properties prod = (Properties);
System.out.println("productId : " + prod.getProperty("productId"));
System.out.println("PName : " + prod.getProperty("PName"));
System.out.println("Price : " + prod.getProperty("Price"));

catch (Exception e) {
throw (new ServletException(e));
try { if( rs != null ) rs.close() ; } catch( SQLException ex ) { }
try { if( st != null ) st.close() ; } catch( SQLException ex ) { }
try { if( c != null ) c.close() ; } catch( SQLException ex ) { }
} %>

<TABLE align='center'>
<TR><TD><TD><DIV ALIGN="right"><FONT size="+2"><B><A href='categoryList.jsp'> Continue Shopping!
<TR><TD><TD><DIV ALIGN="right"><FONT size="+2"><B><A href='checkout.jsp'> Go To Checkout
Jeffrey Hunter
Ranch Hand

Joined: Apr 16, 2004
Posts: 305
Where are you executing your PreparedStatement? I'm assuming you just left out some code, because you should get a NullPointerException if you run the code you have above.

When building a query, I usually use the following structure to initialize the PreparedStatement:

Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572

Why are you using prepare statement if you are going to make the query like this? Use following

productId = rs.getString(1);

Where is Reulstset object rs initialized? i didn't see anywhere.Initialize it as

so change you product class like this and set the properties now to

Change you code and then tell what are the exceptions and errors are you getting?
I agree. Here's the link:
subject: shopping cart problem
jQuery in Action, 3rd edition