今天做的是修改。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">账单信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>账单类型</td> <td>年</td> <td>月</td> <td>日</td> <td>收入</td> <td>支出</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${bills}" var="item"> <tr> <td>${item.id}</td> <td>${item.type}</td> <td>${item.year}</td> <td>${item.month}</td> <td>${item.day}</td> <td>${item.income}</td> <td>${item.pay}</td> <td><a href="BillServlet?method=getbillbyid2&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
后面部分以后做。
dao层
public List<Bill> modifylist() { String sql = "select * from bill"; List<Bill> modifylist = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Bill bean = null; while (rs.next()) { int id = rs.getInt("id"); String type2 = rs.getString("type"); String year2 = rs.getString("year"); String month2 = rs.getString("month"); String day2 = rs.getString("day"); String income2=rs.getString("income"); String pay2=rs.getString("pay"); bean = new Bill(id, type2, year2, month2,day2,income2,pay2); modifylist.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return modifylist; }
servlet层
private void getBillById2(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Bill bill = dao.getBillById(id); req.setAttribute("bill", bill); req.getRequestDispatcher("modify.jsp").forward(req,resp); } private void modifylist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Bill> bills = dao.modifylist(); req.setAttribute("bills",bills); req.getRequestDispatcher("modifylist.jsp").forward(req,resp); }
这些只是能出现上图那个修改列表,点击修改进入修改界面。后续再完成。
原文:https://www.cnblogs.com/birdmmxx/p/10404142.html