gpt4 book ai didi

java - 如何通过id从表中删除一条记录

转载 作者:太空宇宙 更新时间:2023-11-04 14:21:15 24 4
gpt4 key购买 nike

我从数据库中检索了所有数据并用表格显示列表。现在我想在单击表中的“删除”时删除一条记录。我不知道如何传递我要删除的记录的 id。您能检查我的代码并给出指示吗?我非常感谢您的指示。

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>

行动:

public String delete() throws Exception{
int i = EmployeeListDao.delete(this);
if(i>0){
return SUCCESS;
}
return ERROR;
}

道:

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>

最佳答案

您可以简单地使用 ServletActionContext.getRequest().getParameter("paramName"); 来获取 action 中的 HttpRequest 参数值类(class)。所以在你的删除方法中,

public String delete() throws Exception{
String id=ServletActionContext.getRequest().getParameter("name");
int i = EmployeeListDao.delete(id);
if(i>0){
return SUCCESS;
}
return ERROR;
}

将为您完成这项工作。但看看这里类似的线程 How to access url parameters in Action classes Struts 2

关于java - 如何通过id从表中删除一条记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27163601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com