修改的后半部分内容。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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; } </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: cyan;">记账信息修改</h1> <a href="index.jsp">返回主页</a> <form action="BillServlet?method=modify" method="post" onsubmit="return check()"> <div class="a"> 账单类型<input type="text" id="type" name="type" value="${bill.type}"/> </div> <div class="a"> 年<input type="text" id="year" name="year" value="${bill.year}"/> </div> <div class="a"> 月<input type="text" id="month" name="month" value="${bill.month}"/> </div> <div class="a"> 日<input type="text" id="day" name="day" value="${bill.day}"/> </div> <div class="a"> 收入<input type="text" id="income" name="income" value="${bill.income}"/> </div> <div class="a"> 支出<input type="text" id="pay" name="pay" value="${bill.pay}"/> </div> <input type="hidden" id="id" name="id" value="${bill.id}"/> <div class="a"> <button type="submit" class="b">修 改</button> </div> </form> </div> </body> </html>
dao层到目前为止所有的
package com.bill.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.bill.util.DBUtil; import com.bill.been.Bill; @SuppressWarnings("unused") public class BillDao { //----------------------------------------------------------------------------------------------------------------------------- public boolean add(Bill bill) { String sql = "insert into bill(type,year,month,day,income,pay) values(‘" + bill.getType() + "‘,‘" + bill.getYear() + "‘,‘"+bill.getMonth()+"‘,‘"+bill.getDay()+"‘,‘"+bill.getIncome()+"‘,‘"+bill.getPay()+"‘)"; Connection conn = DBUtil.getConn();//调用方法连接数据库 Statement state = null; boolean f = false; int a = 0 ; try { //监视大括号内的代码 state = conn.createStatement(); a = state.executeUpdate(sql); } catch (Exception e) { //捕获错误 e.printStackTrace(); } finally { //关闭z 连接 DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } //---------------------------------------------------------------------------------------------------------------------------- public Bill getBillById(int id) { String sql = "select * from bill where id =‘" + id + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Bill bill = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String type = rs.getString("type"); String year = rs.getString("year"); String month = rs.getString("month"); String day = rs.getString("day"); String income = rs.getString("income"); String pay = rs.getString("pay"); bill = new Bill(id, type, year, month,day,income,pay); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return bill; } public List<Bill> dellist() { String sql = "select * from bill"; List<Bill> dellist = 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); dellist.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return dellist; } public boolean delete (int id) { boolean f = false; String sql = "delete from bill where id=‘" + id + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } //-------------------------------------------------------------------------------------------------------------- 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; } public boolean modify(Bill bill) { String sql = "update bill set type=‘" + bill.getType() + "‘, year=‘" + bill.getYear() + "‘,month=‘" + bill.getMonth() + "‘,day=‘"+bill.getDay()+"‘,income=‘"+bill.getIncome()+"‘,pay=‘"+bill.getPay()+"‘where id=‘" + bill.getId() + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; }
servlet层到目前位置所有的
package com.bill.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.bill.dao.BillDao; import com.bill.been.Bill; @WebServlet("/BillServlet") public class BillServlet extends HttpServlet{ private static final long serialVersionUID = 1L; public BillServlet() { super(); } BillDao dao=new BillDao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); }else if ("getbillbyid".equals(method)) { getBillById(req, resp); }else if ("dellist".equals(method)) { dellist(req,resp); }else if ("delete".equals(method)) { delete(req,resp); }else if ("getbillbyid2".equals(method)) { getBillById2(req, resp); }else if ("modifylist".equals(method)) { modifylist(req,resp); }else if ("modify".equals(method)) { modify(req,resp); }else if ("search".equals(method)) { search(req,resp); } } //------------------------------------------------------------------------------------------------------ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { // TODO Auto-generated method stub String type = req.getParameter("type"); String year = req.getParameter("year"); String month = req.getParameter("month"); String day = req.getParameter("day"); String income = req.getParameter("income"); String pay = req.getParameter("pay"); Bill bill=new Bill(type,year,month,day,income,pay); if(dao.add(bill)) { req.setAttribute("message", "保存成功!"); req.getRequestDispatcher("add.jsp").forward(req, resp); }else { req.setAttribute("message", "保存失败!"); req.getRequestDispatcher("add.jsp").forward(req, resp); } } //------------------------------------------------------------------------------------------------------ private void getBillById(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("delete.jsp").forward(req,resp); } private void dellist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); List<Bill> bills = dao.dellist(); req.setAttribute("bills", bills); req.getRequestDispatcher("dellist.jsp").forward(req,resp); } private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); dao.delete(id); req.setAttribute("message", "删除成功"); req.getRequestDispatcher("index.jsp").forward(req,resp); } //--------------------------------------------------------------------------------------------------------------------------------- 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); } private void modify(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String type = req.getParameter("type"); String year = req.getParameter("year"); String month = req.getParameter("month"); String day = req.getParameter("day"); String income = req.getParameter("income"); String pay = req.getParameter("pay"); Bill bill = new Bill(id, type, year, month,day,income,pay); dao.modify(bill); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("BillServlet?method=modifylist").forward(req,resp); }
效果展示
原文:https://www.cnblogs.com/birdmmxx/p/10409430.html