jsp+servlet手机管理(增删改查 二)

    科技2022-07-14  129

    jsp+servlet手机管理(增加和修改二)

    在以上篇我们写了实现列表功能

    一 :新增功能

    注意新增 不仅要增加手机 还要选择手机的品牌 , 我们可以将手机品牌做成下拉列表框

    首先 在t_list.jsp列表页面新增一个a标签 跳转新增界面

    <a href="<%=request.getContextPath()%>/tel?method=tosave">新增</a>

    虽然样式有点丑 ,勉强还能看 。 我们跳到新增界面后

    先实现添加时品牌动态从数据库读取

    Dao

    在dao层创建TbrandDao 接口

    List<Tbrand> findAll();

    接口实现类 TbrandDaoImpl

    public List<Tbrand> findAll() { Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; String sql="select * from t_brand"; List<Tbrand> tbrandList=null; Tbrand tbrand=null; try { tbrandList=new ArrayList<>(); connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery(); while (resultSet.next()) { tbrand=new Tbrand(); tbrand.setId(resultSet.getInt("id")); tbrand.setDname(resultSet.getString("dname")); tbrandList.add(tbrand); } }catch (Exception e){ e.printStackTrace(); }finally { JDBCUtil.close(resultSet,preparedStatement,connection); } return tbrandList; }

    与我们上一期的列表有些许相似

    service

    然后实现我们的service层

    TbrandService 接口

    List<Tbrand> findAll();

    TbrandService 接口实现类 TbrandServiceImpl

    public class TbrandServiceImpl implements TbrandService { private TbrandDao tbrandDao=new TbrandDaoImpl(); @Override public List<Tbrand> findAll() { return tbrandDao.findAll(); } }

    Telservlet

    在doPost方法中 还是老样子 判断请求地址

    String method = request.getParameter("method"); if ("findAll".equals(method)){ findAll(request,response); }else if ("tosave".equals(method)){ tosave(request,response); } } private void tosave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Tbrand> tbrandList = tbrandService.findAll(); request.setAttribute("tbrandList",tbrandList); request.getRequestDispatcher("/t_save.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); }

    创建 新增页面 t_save.jsp

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>新增</title> </head> <body> <form action="<%=request.getContextPath()%>/tel?method=saveTel" method="post"> 名称:<input type="text" name="name"><br> 品牌:<select name="bid"> <c:forEach items="${requestScope.tbrandList}" var="tbrandList"> <option value="${tbrandList.id}">${tbrandList.dname}</option> </c:forEach> </select> <input type="submit" value="提交"> </form> </body> </html>

    接下来我们实现新增功能

    在TelDao里 添加新方法

    //新增 void save(Tel tel);

    在TelDaoImpl接口实现类实现该方法

    @Override public void save(Tel tel) { Connection connection=null; PreparedStatement preparedStatement=null; String sql="insert into t_tel set name=?,bid=?"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,tel.getName()); preparedStatement.setInt(2,tel.getBid()); //更新操作 preparedStatement.executeUpdate(); }catch (Exception e){ e.printStackTrace(); }finally { JDBCUtil.close(null,preparedStatement,connection); } }

    下面就是TelService接口 添加新方法:

    void save(Tel tel);

    TelServiceImpl 实现 新方法

    public void save(Tel tel) { telDao.save(tel); }

    Telservlet

    还是老样子 判断请求参数

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getParameter("method"); if ("findAll".equals(method)){ findAll(request,response); }else if ("tosave".equals(method)){ tosave(request,response); }else if ("saveTel".equals(method)){ saveTel(request,response); } } private void saveTel(HttpServletRequest request, HttpServletResponse response) throws IOException { String name = request.getParameter("name"); String bid = request.getParameter("bid"); Tel tel = new Tel(); tel.setBid(Integer.parseInt(bid)); tel.setName(name); telService.save(tel); response.sendRedirect(request.getContextPath()+"/tel?method=findAll"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); //千万不要忘记 }

    二: 更新功能

    1. 我们先点击更新按钮实现一个数据的回显

    <td> <button οnclick="update(${vo.id})">更新</button> </td> <script> function update(id) { location.href="<%=request.getContextPath()%>/tel?method=toUpdate&id="+id } </script>

    根据Id来查站我们的数据

    TelDao创建新方法

    Tel findById(Integer id);

    TelDaoImpl实现接口方法

    public Tel findById(Integer id) { Connection connection=null; PreparedStatement preparedStatement=null; ResultSet resultSet=null; String sql="select id,name,bid from t_tel where id=?"; Tel tel=null; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); //给id一个占位符 preparedStatement.setInt(1,id); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { tel=new Tel(); tel.setId(resultSet.getInt("id")); tel.setName(resultSet.getString("name")); tel.setBid(resultSet.getInt("bid")); } }catch (Exception e){ e.printStackTrace(); }finally { JDBCUtil.close(resultSet,preparedStatement,connection); } return tel; }

    Telservice 添加新方法

    Tel findById(Integer id);

    TelServiceImpl 实现该方法

    public Tel findById(Integer id) { return telDao.findById(id); }

    Telservlet

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getParameter("method"); if ("findAll".equals(method)){ findAll(request,response); }else if ("tosave".equals(method)){ tosave(request,response); }else if ("saveTel".equals(method)){ saveTel(request,response); }else if ("toUpdate".equals(method)){ toUpdate(request,response); } } private void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); Tel byId = telService.findById(Integer.parseInt(id)); //品牌的回显 List<Tbrand> tbrands = tbrandService.findAll(); //存入作用域 request.setAttribute("byId",byId); request.setAttribute("tbrands",tbrands); request.getRequestDispatcher("/t_update.jsp").forward(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); //千万不要忘记 }

    t_update.jsp 页面

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>更新</title> </head> <body> <form action="<%=request.getContextPath()%>/tel?method=updateTel" method="post"> <input type="hidden" name="id" value="${requestScope.byId.id}"> <br> 名称:<input type="text" name="name" value="${requestScope.byId.name}"><br> 品牌:<select name="bid"> <c:forEach items="${requestScope.tbrands}" var="tbs"> <option value="${tbs.id}" <!--根据id来判断--> <c:if test="${tbs.id==requestScope.byId.bid}"> selected </c:if> >${tbs.dname} </option> </c:forEach> </select> <input type="submit" value="提交"> </form> </body> </html>

    2. 更新功能

    TelDao

    void updateTel(Tel tel);

    TelDaoImpl

    public void updateTel(Tel tel) { Connection connection=null; PreparedStatement preparedStatement=null; String sql="update t_tel set name=?, bid=? where id=?"; try { connection = JDBCUtil.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,tel.getName()); preparedStatement.setInt(2,tel.getBid()); preparedStatement.setInt(3,tel.getId()); preparedStatement.executeUpdate(); }catch (Exception e){ e.printStackTrace(); }finally { JDBCUtil.close(null,preparedStatement,connection); } }

    TelService

    void updateTel(Tel tel);

    TelServiceImpl

    public void updateTel(Tel tel) { telDao.updateTel(tel); }

    Telservlet

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getParameter("method"); if ("toUpdate".equals(method)){ toUpdate(request,response); }else if ("updateTel".equals(method)){ updateTel(request,response); } } private void updateTel(HttpServletRequest request, HttpServletResponse response) throws IOException { String id = request.getParameter("id"); String name = request.getParameter("name"); String bid = request.getParameter("bid"); Tel tel = new Tel(); tel.setId(Integer.parseInt(id)); tel.setName(name); tel.setBid(Integer.parseInt(bid)); telService.updateTel(tel); response.sendRedirect(request.getContextPath()+"/tel?method=findAll"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); //千万不要忘记 }
    Processed: 0.018, SQL: 8