javaweb的批量删除
时间: 2024-06-13 08:09:25 浏览: 9
以下是JavaWeb中实现批量删除的示例代码:
前端页面代码:
```html
<form action="deleteBatchServlet" method="post">
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别th>
<th>年龄</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="ids" value="1"></td>
<td>张三</td>
<td>男</td>
<td>20</td>
<td><a href="#">编辑</a></td>
</tr>
<tr>
<td><input type="checkbox" name="ids" value="2"></td>
<td>李四</td>
<td>女</td>
<td>22</td>
<td><a href="#">编辑</a></td>
</tr>
<tr>
<td><input type="checkbox" name="ids" value="3"></td>
<td>王五</td>
<td>男</td>
<td>25</td>
<td><a href="#">编辑</a></td>
</tr>
</tbody>
</table>
<input type="submit" value="批量删除">
</form>
```
后端Servlet代码:
```java
public class DeleteBatchServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取前端页面传来的选中记录的编号
String[] ids = request.getParameterValues("ids");
// 调用service层的方法进行批量删除
UserService userService = new UserServiceImpl();
userService.deleteBatch(ids);
// 删除完成后重定向到列表页面
response.sendRedirect(request.getContextPath() + "/userListServlet");
}
}
```
Service层代码:
```java
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl();
@Override
public void deleteBatch(String[] ids) {
userDao.deleteBatch(ids);
}
}
```
Dao层代码:
```java
public class UserDaoImpl implements UserDao {
@Override
public void deleteBatch(String[] ids) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtils.getConnection();
String sql = "delete from user where id=?";
ps = conn.prepareStatement(sql);
for (String id : ids) {
ps.setInt(1, Integer.parseInt(id));
ps.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(ps, conn);
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)