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.
<html>
<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;
try{
Class.forName("com.mysql.jdbc.Driver");
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();
}
else{
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);
userCart.add(product);
session.setAttribute("userCart", userCart);
userCart = (Vector) session.getAttribute("userCart");
Iterator iter = userCart.iterator();
while (iter.hasNext()) {
Properties prod = (Properties) iter.next();
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));
}
finally{
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!
</A></B></FONT></DIV></TD>
<TR><TD><TD><DIV ALIGN="right"><FONT size="+2"><B><A href='checkout.jsp'> Go To Checkout
</A></B></FONT></DIV></TD>
</TR>
</TABLE>
</BODY>
</HTML>