java - How to pass id to delete one record from table -
i retrieved data database , show list table. delete single record when click "delete" in table. i'm not sure how pass id of record want delete. please check code , give instructions? i'm appreciate instructions.
jsp:
<table id="employee" class="table"> <thead> <tr> <th><span>id</span></th> <th><span>name</span></th> <th><span>dob</span></th> <th><span>address</span></th> <th><span>position</span></th> <th colspan="2"><span></span></th> </tr> </thead> <tbody> <s:iterator value="emplistlist" var="emplist"> <tr> <td><s:property value="id"></s:property></td> <td><s:property value="name"></s:property></td> <td><s:property value="dateofbirth"></s:property></td> <td><s:property value="address"></s:property></td> <td><s:property value="position"></s:property></td> <td><span><a href="delemp?name=<s:property value='id'/>">delete</a></span> </td> </tr> </s:iterator> </tbody> </table>
action:
public string delete() throws exception{ int = employeelistdao.delete(this); if(i>0){ return success; } return error; }
dao:
public static int delete(employeelistaction emp) { // todo auto-generated method stub int status = 0; connection conn = null; try { string url = "jdbc:mysql://localhost:3306/test"; class.forname("com.mysql.jdbc.driver"); conn = drivermanager.getconnection(url, "root", "root"); system.out.println(conn); preparedstatement ps = conn .preparestatement("delete employee emp_id=?"); ps.setint(1, emp.getid()); status = ps.executeupdate(); } catch (exception e) { // todo: handle exception system.out.println(e); } return status; }
struts.xml
<action name="delemp" class="master.struts2.action.employeelistaction" method="delete"> <result name="input">index.jsp</result> <result name="success" type="dispatcher">index.jsp</result> <result name="error">error.jsp</result> </action>
you can use servletactioncontext.getrequest().getparameter("paramname");
httprequest
parameter value in action
class. in delete method,
public string delete() throws exception{ string id=servletactioncontext.getrequest().getparameter("name"); int = employeelistdao.delete(id); if(i>0){ return success; } return error; }
will work . have @ similar thread here how access url parameters in action classes struts 2
Comments
Post a Comment