1.在list页面的每一行后面添加,修改和删除的超链接
os.print("<a href='selectById?id="+list.get(i).getId()+"'>修改</a> <a href='delete?id="+list.get(i).getId()+"'>删除</a>")
2.在SelectByIdServlet中处理修改的功能
public Book getById(int id){
String sql="select * from book where id=?";
try {
ResultSet rs=DBhelper.executeQuery(sql,id);
if(rs.next()){
return new Book(rs.getInt(1),rs.getString(2),rs.getDouble(3),
rs.getString(4 ),rs.getDate(5));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBhelper.close();
}
return null;
}
- 接收传过来的参数id,获取id,通过id查询数据库中的数据
int id=0;
if(req.getParameter("id")!=null){
id=Integer.parseInt(req.getParameter("id"));
Book book=bookdao.getById(id);
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter os=resp.getWriter();
os.print("<html>");
os.print("<head>");
os.print("<title>");
os.print("书籍列表");
os.print("</title>");
os.print("</head>");
os.print("<body>");
os.print("<form action='update' method='get'>");
os.print("<table align='center' width='80%'>");
os.print("<tr><td colspan='2' align='center'>修改书籍</td></tr>");
os.print("<tr>");
os.print("<td>");
os.print("书名:<input type='hidden' name='id' value="+book.getId()+">");
os.print("</td>");
os.print("<td>");
os.print("<input type='text' name='name' value="+book.getName()+">");
os.print("</td>");
os.print("<tr>");
os.print("<td>");
os.print("价格:");
os.print("</td>");
os.print("<td>");
os.print("<input type='text' name='price' value="+book.getPrice()+">");
os.print("</td>");
os.print("</tr>");
os.print("<tr>");
os.print("<td>");
os.print("作者:");
os.print("</td>");
os.print("<td>");
os.print("<input type='text' name='author' value="+book.getAuthor()+">");
os.print("</td>");
os.print("</tr>");
os.print("<tr>");
os.print("<td>");
os.print("出版日期:");
os.print("</td>");
os.print("<td>");
os.print("<input type='text' name='pubDate' value="+book.getPubDate()+">");
os.print("</td>");
os.print("</tr>");
os.print("<tr>");
os.print("<td colspan='2' align='center'>");
os.print("<input type='submit' value='修改'>");
os.print("</td>");
os.print("</tr>");
os.print("</table>");
os.print("<form >");
os.print("</body>");
os.print("</html>");
}
3.将SelectByIdServlet中的表单提交给UpdateServlet处理
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
int id=0;
if(req.getParameter("id")!=null)
id=Integer.parseInt(req.getParameter("id"));
String name=req.getParameter("name");
double price=Double.parseDouble(req.getParameter("price"));
String author=req.getParameter("author");
Date pubDate=null;
try {
pubDate=new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("pubDate"));
} catch (ParseException e) {
e.printStackTrace();
}
Book book=new Book(id,name, price, author, pubDate);
BookDao bookdao=new BookDao();
if(bookdao.update(book)>0){
resp.sendRedirect("list");
}else{
resp.getWriter().print("修改失败!!!");
}
4. 在DeleteServlet中处理删除的功能
public int delete(int id){
String sql="delete from book where id=?";
return DBhelper.executeUpdate(sql, id);
}
- 接收传过来的参数id,获取id,通过id删除数据库中的数据
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
int id=0;
if(req.getParameter("id")!=null){
id=Integer.parseInt(req.getParameter("id"));
}
if(bookdao.delete(id)>0){
resp.sendRedirect("list");
}else{
resp.getWriter().print("删除失败!!");
}
}