第五篇——更新产品信息
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="提交">
<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>
启动项目测试运行效果