Results 1 to 3 of 3
  1. #1
    sara91 is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Error pops up while click on update, i have posted the errors and the pages, pls help

    This is ProductUpdateDeleteServlet

    package gsservlet;

    import java.io.IOException;
    import java.io.PrintWriter;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import gsdatabase.GSDBAO;
    import gsmodel.ProductSearch;

    /**
    * Servlet implementation class ProductUpdateDeleteServlet
    */
    public class ProductUpdateDeleteServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public ProductUpdateDeleteServlet() {
    super();
    // TODO Auto-generated constructor stub
    }

    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    * response)
    */
    protected void doGet(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }

    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    * response)
    */
    protected void doPost(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    int return_code;

    try {
    // Get the PrintWriter object
    System.out.println("in 1");
    PrintWriter writer = response.getWriter();
    writer.write("<html><body>");

    // Get product information from request.getParameter()
    String productid = request.getParameter("Product_ID");
    System.out.println("in 2 productid:" + productid);

    String quantity = request.getParameter("TCPD_Qty");
    System.out.println("in 3 quantity:" + quantity);

    // If "UPDATE" button is clicked
    if (request.getParameter("Update") != null) {
    // Create an instance of ProductBean object
    ProductSearch product = new ProductSearch();
    //System.out.println("in 4");

    //////////////////////////////////// This is where the error must be i think////////////////////////////////////////////////////////////////////////////
    product.setProductid(Integer.parseInt(productid));
    System.out.println("in 4");
    product.setQuantity(Integer.parseInt(quantity));
    System.out.println("in 5");

    // Invoke the ProductBeanDAO's updateProduct() method
    GSDBAO myDAO = new GSDBAO();
    System.out.println("in 6.5");
    return_code = myDAO.updateProduct(product);

    if (return_code != 0)
    writer.write("UPDATE successful!<br><br>");
    else
    writer.write("UPDATE failed!<br><br>");
    writer.write("<a href=\"ProductSearch.html\">Back</a> to Product Search Page.");

    }
    // If "DELETE" button is clicked
    else if (request.getParameter("Delete") != null) {
    // Invoke the ProductBeanDAO's deleteProduct() method
    GSDBAO myDAO = new GSDBAO();
    return_code = myDAO.deleteProductByID(Integer
    .parseInt(productid));

    if (return_code != 0)
    writer.write("DELETE successful!<br><br>");
    else
    writer.write("DELETE failed!<br><br>");
    writer.write("<a href=\"ProductSearch.html\">Back</a> to Product Search Page.");

    }

    writer.write("</body></html>");
    } catch (Exception ex) {
    System.out.println("Error: " + ex.getMessage());
    }
    }

    }



    ProductView.jsp

    XML Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <%@ page import="gsmodel.ProductSearch,java.util.ArrayList,java.util.ListIterator"%>
    <title>Insert title here</title>
    </head>
    <body>
    
    	<h1>Product Edit Page</h1>
    
    	<%
    		ProductSearch product = (ProductSearch) request.getAttribute("product");
    	%>
    
    	<form action="ProductUpdateDeleteServlet?Product_ID=<%=product.getProductid()%>" method="post">
    		<table>
    			<tr>
    				<td>Product ID</td>
    				<td><input readonly="readonly" name="productid" value="<%=product.getProductid()%>" disabled="disabled"></td>
    			</tr>
    			
    			<tr>
    				<td>Product Quantity</td>
    				<td><input name="quantity" value="<%=product.getQuantity()%>"></td>
    			</tr>
    			<tr>
    				<td></td>
    				<td><input type="submit" name="Delete" value="Delete">  <input type="submit" name="Update" value="Update"></td>
    			</tr>
    		</table>
    	</form>
    	<br><br><a href="ProductSearch.html">Back</a> to Product Search Page.
    </body>


    GSDBAO page, it consists of both update and delete statement


    // ProductBeanDAO method: updateProduct()
    public int updateProduct(ProductSearch product) throws Exception {
    int return_code = 0;

    try {
    PreparedStatement ps;
    ps = con
    .prepareStatement("UPDATE grocery.tempcartproduct SET TCPD_Qty=? WHERE Product_ID=?");

    ps.setInt(1, product.getQuantity());
    ps.setInt(2, product.getProductid());
    return_code = ps.executeUpdate();

    con.close();
    } catch (Exception ex) {
    throw new Exception("Error: " + ex.getMessage());
    }

    return return_code;
    }

    // ProductBeanDAO method: deleteProductByID()
    public int deleteProductByID(int productid) throws Exception {
    int return_code = 0;

    try {
    PreparedStatement ps;
    ps = con
    .prepareStatement("DELETE FROM grocery.tempcartproduct WHERE Product_ID= ?");
    ps.setInt(1, productid);
    return_code = ps.executeUpdate();

    con.close();
    } catch (Exception ex) {
    throw new Exception("Error: " + ex.getMessage());
    }

    return return_code;
    }



    ProductSearch.java

    package gsmodel;

    import java.io.Serializable;

    public class ProductSearch implements Serializable {

    private int productid;
    private String productname;
    private String productdesc;
    private int quantity;
    public int getProductid() {
    return productid;
    }
    public void setProductid(int productid) {
    this.productid = productid;
    }
    public String getProductname() {
    return productname;
    }
    public void setProductname(String productname) {
    this.productname = productname;
    }
    public String getProductdesc() {
    return productdesc;
    }
    public void setProductdesc(String productdesc) {
    this.productdesc = productdesc;
    }
    public int getQuantity() {
    return quantity;
    }
    public void setQuantity(int quantity) {
    this.quantity = quantity;
    }
    }


    This is the error that pops up:


    in 1
    in 2 productid:1
    in 3 quantity:null
    in 4
    Error: null

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Error pops up while click on update, i have posted the errors and the pages, pls

    Add a call to printStackTrace() in the catch block to get the full text of the error message. It will help you debug the problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Error pops up while click on update, i have posted the errors and the pages, pls

    And please use code tags for code.
    You managed to use them for the JSP code, so I'm not sure why you didn't for tha Java code.

    I will say (just scanning what I can make out from that code) that the return_code bit at least for the delete is unecessary. If the row has already been deleted by someone else then the user at the front shouldn't really care, and it's not a failure to delete.

    Of course that's not your bug.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 4
    Last Post: 01-04-2011, 12:36 AM
  2. Replies: 3
    Last Post: 10-06-2010, 08:45 AM
  3. Error message pops up no matter what?
    By murr0508 in forum New To Java
    Replies: 5
    Last Post: 02-26-2009, 10:10 PM
  4. Replies: 0
    Last Post: 02-16-2009, 07:18 AM
  5. Replies: 21
    Last Post: 08-01-2008, 11:13 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •