Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

shopping cart problem  RSS feed

Donna Harrington
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!