add<电脑删除>

master
liyansheng 2024-12-21 01:27:15 +08:00
parent b75c396a41
commit e00a186f03
5 changed files with 24 additions and 5 deletions

View File

@ -13,7 +13,7 @@ import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
@WebServlet(urlPatterns = {"/computerList"})
@WebServlet(urlPatterns = {"/computerList","/deleteComputer"})
public class ComputerServlet extends HttpServlet {
IComputerService computerService = new ComputerServiceImpl();
@ -29,6 +29,19 @@ public class ComputerServlet extends HttpServlet {
throw new RuntimeException(e);
}
break;
case "/deleteComputer":
deleteComputer(req, resp);
break;
}
}
private void deleteComputer(HttpServletRequest req, HttpServletResponse resp) {
String id = req.getParameter("id");
try {
computerService.deleteComputer(id);
resp.sendRedirect("/computerList");
} catch (IOException e) {
throw new RuntimeException(e);
}
}

View File

@ -31,14 +31,13 @@ public class ComputerDao {
return 0;
}
public int deleteComputer(int id) {
public void deleteComputer(int id) {
String sql = "delete from computer where id=?";
try {
return queryRunner.update(sql, id);
queryRunner.update(sql, id);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public List<Computer> getAllComputer() throws SQLException {

View File

@ -10,4 +10,6 @@ public interface IComputerService {
public void addComputer(String name, String brand, String type, String price);
List<Computer> listComputer() throws SQLException;
void deleteComputer(String id);
}

View File

@ -20,4 +20,9 @@ public class ComputerServiceImpl implements IComputerService {
public List<Computer> listComputer() throws SQLException {
return computerDao.getAllComputer();
}
@Override
public void deleteComputer(String id) {
computerDao.deleteComputer(Integer.parseInt(id));
}
}

View File

@ -28,7 +28,7 @@
<td>${computer.stock}</td>
<td>
<a href="/computer?action=updateForm&id=${computer.id}">Edit</a> |
<a href="/computer?action=delete&id=${computer.id}">Delete</a>
<a href="/deleteComputer?id=${computer.id}">Delete</a>
</td>
</tr>
</c:forEach>