I retrieved all data from database and show the list with table. now I would like to delete a single record when click "Delete" in the table. I'm not sure how to pass the id of the record I want to delete. Could you please check my code and give instructions? I'm really appreciate for your 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 i = 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 from employee where 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>