10_书籍管理系统_修改_删除

1.在list页面的每一行后面添加,修改和删除的超链接

  • 通过超链接以get方式将传参数id
os.print("<a href='selectById?id="+list.get(i).getId()+"'>修改</a>&nbsp<a href='delete?id="+list.get(i).getId()+"'>删除</a>");

2.在SelectByIdServlet中处理修改的功能

  • 在BookDao中添加通过id查询书籍的方法
//根据id查询所有书籍
    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查询数据库中的数据
//获取id
int id=0;
if(req.getParameter("id")!=null){
    id=Integer.parseInt(req.getParameter("id"));
    //根据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>");
                //将id以隐性参数传递
                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中处理删除的功能

  • 在BookDao中添加通过id删除书籍的方法
    //删除书籍
    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("删除失败!!");
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值