Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Struts delete and edit options in struts

Have Programming Question? Ask it here!
 
 
 


surya lakshmi
delete and edit options in struts
0 Answer(s)      a year and 11 months ago
Posted in : Struts


Hi, I am doing an web application using struts, jsp, tomcat server, oracle as a database in netbeans IDE 7.1.2, I have a developed a code for item deletion and updation, it is not showing any errors but the edit and delete operations were not working, so please modify my code given below. please and urgent.

DBtable:

create table iteminsert( sno number(10), code varchar(15) not null, type varchar(25) not null, quantity number(10) not null, mrp number(10,2) not null, pcost number (10,2) not null, vat number (10) not null, tcost number(10,2) not null);

commit;

CREATE SEQUENCE seq INCREMENT BY 1 START WITH 1 MAXVALUE 99999 NOCACHE NOCYCLE;

commit;

itemupdate.jsp

<%-- Document : itemupdate Created on : Sep 15, 2012, 3:50:19 PM Author : admin --%> <%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page import="java.util.Iterator"%> <%@page import="java.util.List"%>

<%! String code=null; %>

<table width="1000" cellspacing="0" border=0 align="center">
    <tr><td>
<jsp:include page="menu.jsp"></jsp:include>
        </td></tr>
<tr><td>
        <br>
        <br>
        <br>
<form name="update" method="get">            
<table width="1000" cellspacing="0" border=1 align="center">
<tr>
    <td align="middle" width="60"><b>Select</b></td>
    <td align=middle width="120"><b>Item Code</b></td>
    <td align=middle width="250"><b>Item Type</b></td>
    <td align=middle width="120"><b>Quantity</b></td>
    <td align=middle width="180"><b>M.R.P</b></td>
    <td align=middle width="180"><b>Purchase Cost</b></td>
    <td align=middle width="120"><b>VAT on item</b></td>
    <td align=middle width="200"><b>Net Cost</b></td>

 <%
 List list=(List)request.getAttribute("updateprod");
 if(list!=null)
    {
     Iterator iter=list.iterator();
     while(iter.hasNext())
      {
         com.dao.InsertBean ib =(com.dao.InsertBean)iter.next();
         code=ib.getCode();
      %>
    <tr>
            <td align=middle width="60"><input type="checkbox" name="pdeleteitem" value="<%=ib.getCode()%>"</td>
            <td align=middle width="120"><a href="javascript:editProduct('<%=code%>')"><%=code%></a></td>
            <td align=middle width="250"><%=ib.getType()%></td>
            <td align=middle width="120"><%=ib.getQuantity()%></td>
        <td align=middle width="180"><%=ib.getMrp()%></td>
            <td align=middle width="180"><%=ib.getPcost()%></td>
            <td align=middle width="120"><%=ib.getVat()%></td>
            <td align=middle width="200"><%=ib.getTcost()%></td>
</tr>
        <%
         }
    }
 %>

</table>
 <table width="1000" cellspacing="0" align="center">
 <tr><td width="500"></td><td><input type="button" value="Delete" name="delete" onclick="deleteProduct()" align="right"></td>
 </tr>
 </table>

 <script type="text/javascript">
    function editProduct(code)
        {
            document.update.action="getproductforupdate.do?code="+code;
            document.update.submit();
        }
 </script>
 <script type="text/javascript">
    function deleteProduct()
        {
            document.update.action="deleteproduct.do";
            document.update.submit();  
        }
</script>

InsertDAO.java

package com.dao;

import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.util.Vector;

public class InsertDAO { Connection con=null; PreparedStatement psmt=null; PreparedStatement psmt1=null; public void setConnection() { con=DataBaseBean.getConnection(); } public void closeConnection() { DataBaseBean.closeConnection(); } public List updateProd() { List arrayList=new ArrayList(); try { psmt=con.prepareStatement("select * from iteminsert");

    ResultSet rs=psmt.executeQuery();


    while(rs.next())
    {
        InsertBean ib = new InsertBean();
        ib.setCode(rs.getString("code"));
                        ib.setType(rs.getString("type"));
                        ib.setQuantity(rs.getInt("quantity"));
                        ib.setMrp(rs.getFloat("mrp"));
                        ib.setPcost(rs.getFloat("pcost"));
                        ib.setVat(rs.getInt("vat"));
                        ib.setTcost(rs.getFloat("tcost"));

                        arrayList.add(ib);
    }
    return arrayList;
}
        catch(Exception e)
{
    System.out.println(e);
    return arrayList;
}
finally
{
    try
    {
        psmt.close();
    }
    catch (Exception e)
    {
        System.out.println(e);
    }
}

}

}

ProdEdit.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package action;

import com.dao.InsertBean; import com.dao.InsertDAO; import formbeans.InsertForm; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;

/** * * @author admin */ public class ProductEdit extends Action { public ProductEdit() { super(); } /* * forward name="success" path="" */ Connection con=null; PreparedStatement psmt=null; //PreparedStatement psmt1=null; public String result;

@Override public ActionForward execute(ActionMapping mapping, ActionForm form, // HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException // { InsertForm fr = (InsertForm) form; InsertBean ib = new InsertBean(); InsertDAO idao=new InsertDAO();

idao.setConnection();
        int i=0;
        int vat=ib.getVat();
        float pcost=ib.getMrp()*ib.getQuantity();
        float tcost=pcost+(pcost*vat)/100;   

try
{
    psmt=con.prepareStatement("update iteminsert set type=?,quantity=?,mrp=?,pcost=?,vat=?,tcost=? where code='"+ib.getCode()+"'");
    //psmt.setString(1,ib.getCode());
    psmt.setString(2,ib.getType());
                psmt.setInt(3,ib.getQuantity()); 
                psmt.setFloat(4,ib.getMrp());
                psmt.setFloat(5,pcost);
                psmt.setInt(6,vat);
                psmt.setFloat(7,tcost);
                psmt.setInt(8,ib.getCount());

    i=psmt.executeUpdate();
                result="success";
        }
catch(Exception e)
{
    System.out.println("\n Exception : "+e+"\n");

                 //result="success";
}
        finally
{
    try
    {
        psmt.close();
    }
    catch (Exception e)
    {
        System.out.println(e);
    }
}

//int res=idao.editItems(ib);
if(i>0)
{
     result="success";

        }
else
{
     result="failure";
}


idao.closeConnection();
    return  mapping.findForward(result);

}

}

ProductDeleteAction.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package action;

import com.dao.InsertDAO; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;

/** * * @author admin */ public class ProductDeleteAction extends Action { public ProductDeleteAction() { super(); } /* * forward name="success" path="" */ Connection con=null; PreparedStatement psmt=null; //PreparedStatement psmt1=null; public String result;

@Override public ActionForward execute(ActionMapping mapping, ActionForm form, // HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException // { try{

InsertDAO idao=new InsertDAO();
        int a=0;
idao.setConnection();
String dele[]=request.getParameterValues("pdeleteitem");
        String query1="delete from iteminsert where code=(?)";
        //String query2="delete from pitems where code=?";
        for(int i=0;i<dele.length;i++)
            {
                psmt=con.prepareStatement(query1);
                psmt.setString(1,dele[i]);
                a= psmt.executeUpdate();
            }
        if(a==1)
        {
            result="success";
        }
        else
        {  
            result="failure";
        } 
        idao.closeConnection();     
    }
catch(Exception e)
    {
     System.out.println(e);   
    }
return mapping.findForward("result");

}

}

producteditform.jsp

<%-- Document : producteditform Created on : Sep 15, 2012, 5:09:41 PM Author : admin --%>

<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ page import="java.util.*"%>

<tr><td width="120"></td><td>Item Code:          <input type="text" name="code" value="" property="hidden"></td></tr> 
<tr><td><br></td></tr>
<tr><td width="120"></td><td>Item Type:           <input type="text" name="type" value="" ></td></tr>
<tr><td><br></td></tr>
        <tr><td width="120"></td><td>Quantity:             <input type="text" name="quantity" value="" ></td></tr>
<tr><td><br></td></tr>
<tr><td width="120"></td><td>  M.R.P:               <input type="text" name="mrp" value="" ></td></tr>
<tr><td><br></td></tr>
        <tr><td width="120"></td><td>  VAT:                   <input type="text" name="vat" value=""></td></tr>
<tr><td><br></td></tr>        
        <tr><td width="120"></td><td width="300">                                        
        <button align="center">Submit</button></td></tr>
    </table>
</form>

GetProductAction.java

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package action;

import com.dao.InsertDAO; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping;

/** * * @author admin */ public class GetProductAction extends Action { @Override public ActionForward execute(ActionMapping mapping, ActionForm form,

    HttpServletRequest request, HttpServletResponse response)

{ String result="failure"; HttpSession ses = request.getSession();

InsertDAO idao=new InsertDAO();
        idao.setConnection();

List arrayList=new ArrayList();
arrayList=idao.updateProd();
result="success";
request.setAttribute("updateprod",arrayList);
idao.closeConnection();
return  mapping.findForward(result);

}

}

struts-config.xml

 <!-- get items to Edit -->
    <action path="/getproductforupdate" scope="session" type="action.GetProductAction">
    <forward name="success" path="/producteditform.jsp" />
<forward name="failure" path="/error.jsp" />
    </action>
 <!-- end of get items to Edit --> 

 <!-- Edit items -->
    <action path="/getprodedit" scope="session" type="action.ProductEdit">
    <forward name="success" path="/updateprod.do" />
<forward name="failure" path="/error.jsp" />
    </action>
 <!-- end of Edit items --> 

 <!-- Delete Items -->
    <action path="/deleteproduct" scope="session" type="action.ProductDeleteAction">
    <forward name="success" path="/updateprod.do" />
<forward name="failure" path="/error.jsp" />
    </action>
 <!-- end of Delete items -->

Advertisement
View Answers

Related Tutorials/Questions & Answers:
 

 

 

DMCA.com