万物之源——改

该博客围绕产品信息更新展开,涉及接口、dao、servlet等开发环节,还对数据库信息进行整理。同时给出了servlet、update.jsp的测试链接,最后通过product.jsp启动项目测试运行效果。

第五篇——更新产品信息

1、接口

boolean updata(Product p);

2、dao

	public boolean update(Product p) {
		String sql = "update product set name=?,addr=?,price=? where id=?";
		int n = 0;
		try {
			ps = ConnectionDatabase.getConnection().prepareStatement(sql);
			ps.setString(1, p.getName());
			ps.setString(2, p.getAddr());
			ps.setDouble(3, p.getPrice());
			ps.setInt(4, p.getId());
			n = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n > 0;
	}

3、测试

整理数据库信息

	@Test
 public void dao() {
	Product p=new Product(1,"2","2",2);
	System.out.println(DaoFactory.getProductDao().update(p));
 }

 

4、servlet

	private void updateService(HttpServletRequest request, HttpServletResponse response) {
		int id=Integer.parseInt(request.getParameter("id"));
		String name=request.getParameter("name");
		String addr=request.getParameter("addr");
		double price=Double.parseDouble(request.getParameter("price"));
		Product p=new Product(id,name,addr,price);
		System.out.println(p);
		try {
			if (DaoFactory.getProductDao().update(p)) {
				request.getRequestDispatcher("OperatorServlet?op=query").forward(request, response);
			} else {
				response.sendRedirect("update.jsp");
			} 
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

测试

http://localhost:8081/ProductTest/OperatorServlet?op=update&&id=1&&name=1&&addr=1&&price=1

5、update.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
</head>
<body>
	<center>
		<form action="OperatorServlet?op=update" method="post">
			<table>
				<tr>
					<th>编号</th>
					<td><input type="text" name="id" readonly="readonly"
						value="<%=request.getParameter("id")%>"></td>
				</tr>
				<tr>
					<th>名称</th>
					<td><input type="text" name="name"></td>
				</tr>
				<tr>
					<th>产地</th>
					<td><input type="text" name="addr"></td>
				</tr>
				<tr>
					<th>价格</th>
					<td><input type="text" name="price"></td>
				</tr>
			</table>
				<input type="submit" value="提交">&nbsp;&nbsp;
				<input type="reset" value="重置">
		</form>
	</center>
</body>
</html>

测试

http://localhost:8081/ProductTest/update.jsp?id=1

6、product.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@page import="com.l.bean.Product"%>
<%@ page import="java.util.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在此处插入标题</title>
<script>
function del(data){
	decide=confirm("确认删除吗?");
	if(decide){
		self.location.href="OperatorServlet?op=delete&&id="+data;
	}
}
</script>
</head>
<body>

	<center>
		<table border="1" height="300"width="500">
			<tr>
				<th width="50">编号</th>
				<th width="50">名称</th>
				<th width="50">产地</th>
				<th width="50">价格</th>
				<th width="80" colspan="2">操作</th>
			</tr>
			<%
				List<Product> list=(List)request.getAttribute("list");
				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="del(<%=p.getId()%>)">删除</a></td>
				<td><a href="update.jsp?id=<%=p.getId()%>">更新</a></td>
			</tr>
			<% 
			}
			%>
		</table>
	</center>
</body>
</html>

启动项目测试运行效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值