万物之源——删

第四篇——删除商品信息

1、接口

boolean delete(int id);

 

2、dao

	public boolean delete(int id) {
		String sql="delete from product where id="+id;
		int n=0;
		try {
			ps=ConnectionDatabase.getConnection().prepareStatement(sql);
			n = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n>0;
	}

 

3、测试

public void t() {
	IProductDao dao = DaoFactory.getProductDao();
	System.out.println(dao.delete(1));
}

4、servlet

	private void deleteService(HttpServletRequest request, HttpServletResponse response) {
		int id=Integer.parseInt(request.getParameter("id"));
		try {
			if (DaoFactory.getProductDao().delete(id)) {
				request.getRequestDispatcher("OperatorServlet?op=query").forward(request, response);;
			} else {
				response.sendRedirect("main.jsp");
			} 
		} catch (Exception e) {
		}
		
	}

5、测试

启动项目,在搜索栏输入

http://localhost:8081/ProductTest/OperatorServlet?op=delete&&id=5

6、主页

<html>
<head>
<title>product</title>
<script>
	function fun( data){
		decide=confirm("确认删除吗")
		if(decide){
			self.location.href="OperatorServlet?op=delete&&id="+data;
		}
	}
</script>
</head>

<body>

	<center>
		<table border="1">
			<tr>
				<th width="60">编号</th>
				<th width="60">名称</th>
				<th width="60">产地</th>
				<th width="60">价格</th>
				<th colspan="2" width="800">操作</th>

			</tr>
			<%
				IProductDao dao = DaoFactory.getProductDao();
				List<Product> list = dao.queryProduct();
				for (Product p : list) {
			%>
			<tr>
				<td><%=p.getId()%></td>
				<td><%=p.getName()%></td>
				<td><%=p.getAddr()%></td>
				<td><%=p.getPrice()%></td>
				<td><a href="#" onclick="fun(<%=p.getId()%>)">删除</a></td>
				<td><a href="update.jsp?id=<%=p.getId() %>" >更新</a></td>
			</tr>
			<%
				}
			%>
		</table>
	</center>
</body>

</html>

 

7、测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值