Donna Harrington

Ranch Hand
+ Follow
since Aug 08, 2003
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Donna Harrington

Hey all,
I have 3 radio buttons and i'm trying to insert the values of the radio buttons. I also have 3 combo boxes for date,month,year.

In my database i have my radio buttons set to varchar and my combo boxes date,month,year are varchar also..but i dont know if this is the right way.
I also don't know if my SQL statement is right.
I'm getting this error when i compile program

java.sql.SQLException: General error, message from server: "Column count doesn't match value count at row 1"

Thanx

<%
String radio = request.getParameter("radiobutton");
String Username = request.getParameter("Username");
String Password = request.getParameter("Password");
String FirstName = request.getParameter("FirstName");
String Surname = request.getParameter("Surname");
String Address = request.getParameter("Address");
String City = request.getParameter("City");
String Email = request.getParameter("Email");
String telephoneNum = request.getParameter("TelephoneNum");
String radioB = request.getParameter("radiobutton");
String cardNum = request.getParameter("cardNum");
String date = request.getParameter("date");
String month = request.getParameter("month");
String year = request.getParameter("year");
String radioBtn = request.getParameter("radiobutton");
String shipName = request.getParameter("shipName");
String shipSurname = request.getParameter("shipSurname");
String shipAddress = request.getParameter("shipAddress");
String shipCity = request.getParameter("shipCity");
String shipCountry = request.getParameter("shipCountry");
String shipPostalCode = request.getParameter("shipPostalCode");
String driver="com.mysql.jdbc.Driver";
String connectionURL="jdbc:mysql://localhost:3306/petshopwebsite";
ResultSet rs = null;
String password = "";
String username ="root";

//connection code

PreparedStatement p = con.prepareStatement("INSERT INTO customerdetails(Title,Username,Password,FirstName,Surname,Address,City,Email,TelephoneNum,cardType,
cardNum,date,month,year,shipTitle,shipSurname,shipAddress,shipCity,shipCountry,shipPostalCode)VALUES('"
+radio+"','"+Username+"','"+Password+"','"+FirstName+"','"+Surname+"','"+Address+"','"+City+"','"+Email+
"','"+telephoneNum+"','"+radioB+"','"+cardNum+"','"+date+"','"+month+"','"+year+"','"+radioBtn+"','"
+shipName+"','"+shipSurname+"','"+shipAddress+"','"+shipCity+"','"+shipCountry+"','"+shipPostalCode+"')");
int affectedRows= p.executeUpdate();

}

[Edited by Jeanne to add linebreaks]
[ May 31, 2004: Message edited by: Jeanne Boyarsky ]
a stoopid error.its solved.thanx
Hey everybody,
Does anybody know what this means..in the output its inserting the values but its not physically inserting them into the database.
thanx


java.sql.SQLException: Syntax error or access violation, message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'order (username,productId,Quantity) VALUES('eoiny','21','1')' a"
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1905)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1109)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1203)
at com.mysql.jdbc.Connection.execSQL(Connection.java:2090)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1680)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1527)
at org.apache.jsp.checkout_jsp._jspService(checkout_jsp.java:117)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:133)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:311)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:284)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:233)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:204)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:257)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:245)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:199)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:184)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:164)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:567)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:972)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:206)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:833)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:732)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:619)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:688)
at java.lang.Thread.run(Thread.java:534)
yep that worked! thanx a million for yer help :-) i really appreciate that cos i'm crap at this stuff.
take care!
Hi,
I'd be very grateful if somebody could see where my syntax error is in my SQL statements.
thanx :-)

<html>
<head>
<title> Checkout</title>
</head>
<body bgcolor="#228B22">
<font face="Times New Roman,Times" size="+3"> Checkout</font>
<hr>
<jsp:useBean id="cb" scope="session" class="myBeans.CartBean"/>

<form method=get>

<% 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";



try{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection(connectionURL,username, password);
st = c.createStatement();
%>

<%
String Username = request.getParameter("username");
String productId = request.getParameter("productId");

String Quantity = request.getParameter("Quantity");


PreparedStatement p = c.prepareStatement("SELECT Username FROM customerdetails WHERE username='"+ Username +"'");
PreparedStatement p1 = c.prepareStatement("SELECT productId FROM product WHERE productId='"+ productId +"'");
PreparedStatement p2 = c.prepareStatement("INSERT INTO order (Username,productId,Quantity)VALUES('"+Username+"','"+productId+"','"+Quantity+"')");
p.execute();
p1.execute();
int affectedRows= p2.executeUpdate();

if (rs.next()){
Username = rs.getString(1);
System.out.println(Username);
productId = rs.getString(2);
System.out.println(productId);
Quantity = rs.getString(3);
System.out.println(Quantity);
%>
<jsp:include page = "signinsuccessful.jsp"/> <%--display this page on the current page--%>
<%
}
else{
%>
<jsp:include page = "Err.jsp"/> <%--display this page on the current page--%>
<%}



%>




<a href="http://localhost:3306/categoryList.jsp">Shop some more!</a>
<%

}
catch(Exception yu){
yu.printStackTrace();
}

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 ) { }
}
%>
</center>

</body>
</html>
thanx ;-)

But i'm still getting the error. do i just type say 'package myBeans' on my 'CartBean' class or must i do something else..cos i did what u said and its still bringing up the error.thanx
20 years ago
JSP
hey everyone!
I'm having awful trouble here. What i want to do is insert username,product id and quantity into my shopping cart table. There is no problem with quantity but i'm havin problems with username and productid because the customer has signed in previous pages before and also they have chosen a product previous pages before also...so i created a java bean which where i would be able to get username and productid from previous pages. Here is my code for my java bean and my checkout page..where i get my username.product id and quantity and insert into cart table by sql statement.
My problem is it does not recognise my "CartBean" so gives this error

org.apache.jasper.JasperException: CartBean
org.apache.jasper.compiler.BeanRepository.getBeanType(BeanRepository.java:122)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1053)
org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1079)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2180)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2230)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2236)
org.apache.jasper.compiler.Node$Root.accept(Node.java:485)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2180)
org.apache.jasper.compiler.Generator.generate(Generator.java:3255)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:277)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:456)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:439)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:553)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)


root cause


Here's my CartBean code:


public class CartBean {
private String Username;
private String productId;

public CartBean(){
}
public String getUsername(){
return Username;
}
public void setUsername(String newUsername){
Username = newUsername;
}

public String getproductId(){
return productId;
}

public void setproductId(String newproductId){
productId = newproductId;

}
}

And here's my checkout.jsp --->

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import = "java.sql.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.util.Properties"%>
<%@page import = "java.io.InputStream"%>
<%@page session = "true"%>
<%@page import = "java.util.*"%>


<html>
<head>
<title> Checkout</title>
</head>
<body bgcolor="#228B22">
<font face="Times New Roman,Times" size="+3"> Checkout</font>
<hr>
<jsp:useBean id="cb" scope="session" class="CartBean"/>
<jsp:setProperty name="cb" property="Username" param="newUsername"/>
<jsp:setProperty name="cb" property="productId" param="newproductId"/>
<form method=get>

<% 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";



try{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection(connectionURL,username, password);
st = c.createStatement();
%>
<jsp:getProperty name="cb" property="Username"/>
<jsp:getProperty name="cb" property="productId"/>
<%
String Username = request.getParameter("Username");
String productId = request.getParameter("productId");
//String PName = request.getParameter("PName");
//String price = request.getParameter("Price");
String Quantity = request.getParameter("Quantity");
//Properties product = new Properties();


PreparedStatement p = c.prepareStatement("SELECT Username FROM customerdetails WHERE Username='"+ Username +"'");
PreparedStatement p1 = c.prepareStatement("SELECT productId FROM product WHERE productId='"+ productId +"'");
PreparedStatement p2 = c.prepareStatement("INSERT INTO Cart (Username,productId,Quantity)VALUES('"+Username+"','"+productId+"','"+Quantity+"')");
p.execute();
p1.execute();
int affectedRows= p2.executeUpdate();

if (rs.next()){
Username = rs.getString(1);
System.out.println(Username);
productId = rs.getString(2);
System.out.println(productId);
Quantity = rs.getString(3);
System.out.println(Quantity);
%>
<jsp:include page = "signinsuccessful.jsp"/> <%--display this page on the current page--%>
<%
}
else{
%>
<jsp:include page = "Err.jsp"/> <%--display this page on the current page--%>
<%}



%>




<a href="http://localhost:3306/categoryList.jsp">Shop some more!</a>
<%

}
catch(Exception yu){
yu.printStackTrace();
}

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 ) { }
}
%>
</center>
</body>
</html>


Thanx for taking time to read this!
20 years ago
JSP
ok thanx very much
Hey everybody.
hope somebody can help me. I have this error that says ' javax.servlet.ServletException: Can not issue data manipulation statements with executeQuery() '.
Its something to do with my rs = p.executeQuery();
Thanx for ur time!



<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import = "java.sql.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.util.Properties"%>
<%@page import = "java.io.InputStream"%>
<%@page session = "true"%>


<html>
<head>
<title> Checkout</title>
</head>
<body bgcolor="#228B22">
<font face="Times New Roman,Times" size="+3"> Checkout</font>
<hr>

<% 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";


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");
String Quantity = request.getParameter("Quantity");
//Properties product = new Properties();

if(rs == null)
{
PreparedStatement p = c.prepareStatement("INSERT INTO order (username,productId,Quantity)"+" VALUES('"+username+"','"+productId+"','"+Quantity+"')");
rs = p.executeQuery();
%>
<jsp:include page = "signinsuccessful.jsp"/> <%--display this page on the current page--%>
<%
}
else{
%>
<jsp:include page = "Err.jsp"/> <%--display this page on the current page--%>
<%}



%>




<a href="http://localhost:3306/categoryList.jsp">Shop some more!</a>
<%

}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 ) { }
}
%>
</center>
</body>
</html>
Hey everybody,
I hope somebody can help me.I am trying to create an online shopping cart that uses sessions. So i have a servlet side and a class Product and the client side 'ShopCart'. I have 2 errors..one is in the ShopCart.jsp file where it cannot resolve symbol 'class Product' and the other error is ShopCart.jsp uses or overrides deprecated API.
Here is ShopCart which is -->

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import = "java.sql.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.util.Properties"%>
<%@page import = "java.io.InputStream"%>
<%@page session = "true"%>
<%@page import = "java.util.*"%>

<%
Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
if (buylist != null && (buylist.size() > 0)) {
%>
<center>
<table border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
<tr>
<td><b>ProductId</b></td>
<td><b>Product Name</b></td>
<td><b>Price</b></td>
<td><b>Quantity</b></td>
<td></td>
</tr>
<%
for (int index=0; index < buylist.size();index++) {
Product anOrder = (Product) buylist.elementAt(index);
%>
<tr>
<td><b><%= anOrder.getProductId() %></b></td>
<td><b><%= anOrder.getPName() %></b></td>
<td><b><%= anOrder.getPrice() %></b></td>
<td><b><%= anOrder.getQuantity() %></b></td>
<td>
<form name="deleteForm"
action="C:\Documents and Settings\HarringtonD\.netbeans\3.6\sampledir\WEB-INF\classes\com\mycompany\ShoppingServlet"
method="POST">
<input type="submit" value="Delete">
<input type="hidden" name= "delindex" value='<%= index %>'>
<input type="hidden" name="action" value="DELETE">
</form>
</td>
</tr>
<% } %>
</table>
<p>
<form name="checkoutForm"
action="C:\Documents and Settings\HarringtonD\.netbeans\3.6\sampledir\WEB-INF\classes\com\mycompany\ShoppingServlet"
method="POST">
<input type="hidden" name="action" value="CHECKOUT">
<input type="submit" name="Checkout" value="Checkout">
</form>
</center>
<% } %>

Here is my class Product -->

package shopping.Product;
public class Product {
String productId;
String PName;
float price;
int quantity;


/** Creates a new instance of Product */
public Product() {
productId="";
PName="";
price=0;
quantity=0;

}

public void setProductId(String title){
productId=title;
}
public String getProductId(){
return ProductId;
}
public String getPName(){
return PName;
}
public void setPrice(float p){
price=p;
}
public void setQuantity(){
return quantity;
}
}

And Finally Here is my Servlet -->

package shopping.Product;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
*
* @author HarringtonD
* @version
*/
public class ShoppingServlet extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session == null) {
res.sendRedirect("http://localhost:8084/sampledir/errorCart.html");
}
Vector buylist=
(Vector)session.getValue("shopping.shoppingcart");
String action = req.getParameter("action");
if (!action.equals("CHECKOUT")) {
if (action.equals("DELETE")) {
String del = req.getParameter("delindex");
int d = (new Integer(del)).intValue();
buylist.removeElementAt(d);
} else if (action.equals("ADD")) {
//any previous buys of same cd?
boolean match=false;
Product aProduct = getProduct(req);
if (buylist==null) {
//add first cd to the cart
buylist = new Vector(); //first order
buylist.addElement(aProduct);
} else { // not first buy
for (int i=0; i< buylist.size(); i++) {
Product prod = (Product) buylist.elementAt(i);
if (prod.getProductId().equals(aProduct.getProductId())) {
prod.setQuantity(prod.getQuantity()+aProduct.getQuantity());
buylist.setElementAt(prod,i);
match = true;
} //end of if name matches
} // end of for
if (!match)
buylist.addElement(aProduct);
}
}
session.putValue("shopping.shoppingcart", buylist);
String url="http://localhost:8084/sampledir/ShopCart.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
} else if (action.equals("CHECKOUT")) {
float total =0;
for (int i=0; i< buylist.size();i++) {
Product anOrder = (Product) buylist.elementAt(i);
float price= anOrder.getPrice();
int qty = anOrder.getQuantity();
total += (price * qty);
}
total += 0.005;
String amount = new Float(total).toString();
int n = amount.indexOf('.');
amount = amount.substring(0,n+3);
req.setAttribute("amount",amount);
String url="http://localhost:8084/sampledir/checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
}
}
private Product getProduct(HttpServletRequest req) {
String myProduct = req.getParameter("Product");
String qty = req.getParameter("qty");
StringTokenizer t = new StringTokenizer(myProduct,"|");
String productId= t.nextToken();
String PName = t.nextToken();
String price = t.nextToken();
price = price.replace('$',' ').trim();
Product prod = new Product();
prod.setProductId(productId);
prod.setPName(PName);
prod.setPrice((new Float(price)).floatValue());
prod.setQuantity((new Integer(qty)).intValue());
return prod;
}
}


I hope somebody can help me., I cant see these erros..sorry about all the code. thanx.
20 years ago
JSP
Hey everybody,
I hope somebody can help me.I am trying to create an online shopping cart that uses sessions. So i have a servlet side and a class Product and the client side 'ShopCart'. I have 2 errors..one is in the ShopCart.jsp file where it cannot resolve symbol 'class Product' and the other error is ShopCart.jsp uses or overrides deprecated API.
Here is ShopCart which is -->

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import = "java.sql.*"%>
<%@page import = "java.io.*"%>
<%@page import = "java.util.Properties"%>
<%@page import = "java.io.InputStream"%>
<%@page session = "true"%>
<%@page import = "java.util.*"%>

<%
Vector buylist = (Vector) session.getValue("shopping.shoppingcart");
if (buylist != null && (buylist.size() > 0)) {
%>
<center>
<table border="0" cellpadding="0" width="100%" bgcolor="#FFFFFF">
<tr>
<td><b>ProductId</b></td>
<td><b>Product Name</b></td>
<td><b>Price</b></td>
<td><b>Quantity</b></td>
<td></td>
</tr>
<%
for (int index=0; index < buylist.size();index++) {
Product anOrder = (Product) buylist.elementAt(index);
%>
<tr>
<td><b><%= anOrder.getProductId() %></b></td>
<td><b><%= anOrder.getPName() %></b></td>
<td><b><%= anOrder.getPrice() %></b></td>
<td><b><%= anOrder.getQuantity() %></b></td>
<td>
<form name="deleteForm"
action="C:\Documents and Settings\HarringtonD\.netbeans\3.6\sampledir\WEB-INF\classes\com\mycompany\ShoppingServlet"
method="POST">
<input type="submit" value="Delete">
<input type="hidden" name= "delindex" value='<%= index %>'>
<input type="hidden" name="action" value="DELETE">
</form>
</td>
</tr>
<% } %>
</table>
<p>
<form name="checkoutForm"
action="C:\Documents and Settings\HarringtonD\.netbeans\3.6\sampledir\WEB-INF\classes\com\mycompany\ShoppingServlet"
method="POST">
<input type="hidden" name="action" value="CHECKOUT">
<input type="submit" name="Checkout" value="Checkout">
</form>
</center>
<% } %>

Here is my class Product -->

package shopping.Product;
public class Product {
String productId;
String PName;
float price;
int quantity;


/** Creates a new instance of Product */
public Product() {
productId="";
PName="";
price=0;
quantity=0;

}

public void setProductId(String title){
productId=title;
}
public String getProductId(){
return ProductId;
}
public String getPName(){
return PName;
}
public void setPrice(float p){
price=p;
}
public void setQuantity(){
return quantity;
}
}

And Finally Here is my Servlet -->

package shopping.Product;

import java.io.*;
import java.net.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

/**
*
* @author HarringtonD
* @version
*/
public class ShoppingServlet extends HttpServlet {
public void init(ServletConfig conf) throws ServletException {
super.init(conf);
}
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(false);
if (session == null) {
res.sendRedirect("http://localhost:8084/sampledir/errorCart.html");
}
Vector buylist=
(Vector)session.getValue("shopping.shoppingcart");
String action = req.getParameter("action");
if (!action.equals("CHECKOUT")) {
if (action.equals("DELETE")) {
String del = req.getParameter("delindex");
int d = (new Integer(del)).intValue();
buylist.removeElementAt(d);
} else if (action.equals("ADD")) {
//any previous buys of same cd?
boolean match=false;
Product aProduct = getProduct(req);
if (buylist==null) {
//add first cd to the cart
buylist = new Vector(); //first order
buylist.addElement(aProduct);
} else { // not first buy
for (int i=0; i< buylist.size(); i++) {
Product prod = (Product) buylist.elementAt(i);
if (prod.getProductId().equals(aProduct.getProductId())) {
prod.setQuantity(prod.getQuantity()+aProduct.getQuantity());
buylist.setElementAt(prod,i);
match = true;
} //end of if name matches
} // end of for
if (!match)
buylist.addElement(aProduct);
}
}
session.putValue("shopping.shoppingcart", buylist);
String url="http://localhost:8084/sampledir/ShopCart.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
} else if (action.equals("CHECKOUT")) {
float total =0;
for (int i=0; i< buylist.size();i++) {
Product anOrder = (Product) buylist.elementAt(i);
float price= anOrder.getPrice();
int qty = anOrder.getQuantity();
total += (price * qty);
}
total += 0.005;
String amount = new Float(total).toString();
int n = amount.indexOf('.');
amount = amount.substring(0,n+3);
req.setAttribute("amount",amount);
String url="http://localhost:8084/sampledir/checkout.jsp";
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req,res);
}
}
private Product getProduct(HttpServletRequest req) {
String myProduct = req.getParameter("Product");
String qty = req.getParameter("qty");
StringTokenizer t = new StringTokenizer(myProduct,"|");
String productId= t.nextToken();
String PName = t.nextToken();
String price = t.nextToken();
price = price.replace('$',' ').trim();
Product prod = new Product();
prod.setProductId(productId);
prod.setPName(PName);
prod.setPrice((new Float(price)).floatValue());
prod.setQuantity((new Integer(qty)).intValue());
return prod;
}
}


I hope somebody can help me., I cant see these erros..sorry about all the code. thanx.
20 years ago
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>
20 years ago
JSP
I broke it into 2 statements. Thanx very much for yer help! appreciate yer comments!
Hey all,
I have a syntax error and i want to update both tables customerdetails table and order table. but my join is wrong or something. can anybody see my error. thank u!
PreparedStatement p = con.prepareStatement("INSERT INTO customerdetails JOIN order(Title,Username,Password,FirstName,Surname,Address,City,Email,TelephoneNum,cardType,cardNum,date,month,year,shipTitle,shipSurname,shipAddress,shipCity,shipCountry,shipPostalCode)"+" VALUES('"+radio+"','"+Username+"','"+Password+"','"+FirstName+"','"+Surname+"','"+Address+"','"+City+"','"+Email+"','"+telephoneNum+"','"+radio1+"','"+cardNum+"','"+date+"','"+month+"','"+year+"','"+radio2+"','"+shipName+"','"+shipSurname+"','"+shipAddress+"','"+shipCity+"','"+shipCountry+"','"+shipPostalCode+"')");
p.executeUpdate();
sorry put this message in wrong place!