从首页开始进行测试
screen.jsp
<%@page import="com.zzw.entity.Bill"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>按类型和日期查询账目</title> </head> <style type="text/css"> *{margin: 0;padding: 0} html,body{height: 100%} /*这里很关键*/ .outer-wrap{ /*只有同时为html和body设置height: 100%时,这里的height才生效, 并且随浏览器窗口变化始终保持和浏览器视窗等高*/ height: 100%; position: relative; background-image: url('images/01.jpg'); } .screen-panel{ width: 400px; height: 300px; background-image: url('images/05.jpg'); position: absolute; top: 50%; left: 50%; margin-top: -150px; margin-left: -200px; } </style> <body> <% request.setCharacterEncoding("utf-8"); String message=(String)request.getAttribute("message"); if(message!=null){ if(message.equals("error")){ %> <script type="text/javascript"> alert("操作失败"); </script> <% }else if(message.equals("noerror")){ %> <script type="text/javascript"> alert("操作成功"); </script> <% }else{ } } %> <div class="outer-wrap"> <div style="font-size:160px;text-align:center">家庭记账本</div> <div class="screen-panel"> <a href=index.jsp>返回系统首页</a><br> <form action="QueryPartBillServlet" method="post" onsubmit="return checkName()"> <div style="text-align:center;color:yellow"> 账目类型<select id="btype" name="btype"> <option value="饮食">饮食</option> <option value="教育">教育</option> <option value="购物">购物</option> <option value="医疗">医疗</option> <option value="收入">收入</option> <option value="借贷">借贷</option> <option value="其它">其它</option> </select> </div> <div style="text-align:center;color:yellow"> 日期<input type="date" name="bdate"><br/> </div> <div style="text-align:center;color:yellow"> <input type="submit" class="btn btn-primary" value="查询"><br/> </div> </form> <script src="jquery-3.4.1.min.js"></script> <script> function checkName() { var date = document.getElementById("bdate"); if (date.value==null||date.value == "") { alert("请选择日期"); return false; } return true; } </script> <table border="1px"> <tr> <th>账目编号</th> <th>账目类型</th> <th>金额</th> <th>账目日期</th> </tr> <% List <Bill> bills =(List<Bill>) request.getAttribute("bills"); if(bills!=null){ for(Bill bill:bills){ %> <tr> <td><%=bill.getBid() %></td> <td><%=bill.getBtype() %></td> <td><%=bill.getBmoney() %></td> <td><%=bill.getBdate() %></td> <td><a href ="DeleteBillServlet?bid=<%=bill.getBid() %>">删除</a></td> <td><a href="QueryBillServlet?bid=<%=bill.getBid() %>">查询</a></td> </tr> <% } } %> </table> </div> </div> </body> </html>
screentype.jsp
<%@page import="com.zzw.entity.Bill"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>按类型查询账目</title> </head> <style type="text/css"> *{margin: 0;padding: 0} html,body{height: 100%} /*这里很关键*/ .outer-wrap{ /*只有同时为html和body设置height: 100%时,这里的height才生效, 并且随浏览器窗口变化始终保持和浏览器视窗等高*/ height: 100%; position: relative; background-image: url('images/01.jpg'); } .screen-panel{ width: 400px; height: 300px; background-image: url('images/05.jpg'); position: absolute; top: 50%; left: 50%; margin-top: -150px; margin-left: -200px; } </style> <body> <% request.setCharacterEncoding("utf-8"); String message=(String)request.getAttribute("message"); if(message!=null){ if(message.equals("error")){ %> <script type="text/javascript"> alert("操作失败"); </script> <% }else if(message.equals("noerror")){ %> <script type="text/javascript"> alert("操作成功"); </script> <% }else{ } } %> <div class="outer-wrap"> <div style="font-size:160px;text-align:center">家庭记账本</div> <div class="screen-panel"> <a href=index.jsp>返回系统首页</a><br> <form action="QueryTypeServlet" method="post" "> <div style="text-align:center;color:yellow"> 账目类型<select id="btype" name="btype"> <option value="饮食">饮食</option> <option value="教育">教育</option> <option value="购物">购物</option> <option value="医疗">医疗</option> <option value="收入">收入</option> <option value="借贷">借贷</option> <option value="其它">其它</option> </select> </div> <div style="text-align:center;color:yellow"> <input type="submit" value="查询"><br/> </div> </form> <table border="1px"> <tr> <th>账目编号</th> <th>账目类型</th> <th>金额</th> <th>账目日期</th> </tr> <% List <Bill> bills =(List<Bill>) request.getAttribute("bills"); if(bills!=null){ for(Bill bill:bills){ %> <tr> <td><%=bill.getBid() %></td> <td><%=bill.getBtype() %></td> <td><%=bill.getBmoney() %></td> <td><%=bill.getBdate() %></td> <td><a href ="DeleteBillServlet?bid=<%=bill.getBid() %>">删除</a></td> <td><a href="QueryBillServlet?bid=<%=bill.getBid() %>">查询</a></td> </tr> <% } } %> </table> </div> </div> </body> </html>
screendate.jsp
<%@page import="com.zzw.entity.Bill"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>按日期查询账目</title> </head> <style type="text/css"> *{margin: 0;padding: 0} html,body{height: 100%} /*这里很关键*/ .outer-wrap{ /*只有同时为html和body设置height: 100%时,这里的height才生效, 并且随浏览器窗口变化始终保持和浏览器视窗等高*/ height: 100%; position: relative; background-image: url('images/01.jpg'); } .screen-panel{ width: 400px; height: 300px; background-image: url('images/05.jpg'); position: absolute; top: 50%; left: 50%; margin-top: -150px; margin-left: -200px; } </style> <body> <% request.setCharacterEncoding("utf-8"); String message=(String)request.getAttribute("message"); if(message!=null){ if(message.equals("error")){ %> <script type="text/javascript"> alert("操作失败"); </script> <% }else if(message.equals("noerror")){ %> <script type="text/javascript"> alert("操作成功"); </script> <% }else{ } } %> <div class="outer-wrap"> <div style="font-size:160px;text-align:center">家庭记账本</div> <div class="screen-panel"> <a href=index.jsp>返回系统首页</a><br> <form action="QueryDateServlet" method="post" onsubmit="return checkName()"> <div style="text-align:center;color:yellow"> 日期<input type="date" name="bdate"><br/> </div> <div style="text-align:center;color:yellow"> <input type="submit" class="btn btn-primary" value="查询"><br/> </div> </form> <script src="jquery-3.4.1.min.js"></script> <script> function checkName() { var date = document.getElementById("bdate"); if (date.value==null||date.value == "") { alert("请选择日期"); return false; } return true; } </script> <table border="1px"> <tr> <th>账目编号</th> <th>账目类型</th> <th>金额</th> <th>账目日期</th> </tr> <% List <Bill> bills =(List<Bill>) request.getAttribute("bills"); if(bills!=null){ for(Bill bill:bills){ %> <tr> <td><%=bill.getBid() %></td> <td><%=bill.getBtype() %></td> <td><%=bill.getBmoney() %></td> <td><%=bill.getBdate() %></td> <td><a href ="DeleteBillServlet?bid=<%=bill.getBid() %>">删除</a></td> <td><a href="QueryBillServlet?bid=<%=bill.getBid() %>">查询</a></td> </tr> <% } } %> </table> </div> </div> </body> </html>
QueryPartBillServlet.java
package com.zzw.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zzw.entity.Bill; import com.zzw.service.IUserService; import com.zzw.service.Impl.UserServiceImpl; public class QueryPartBillServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); String type=request.getParameter("btype"); String date=request.getParameter("bdate"); System.out.println(type+date); IUserService userservice = new UserServiceImpl(); List<Bill> bills=userservice.QueryPart(type,date); //out对象的获取方法 PrintWriter out = response.getWriter(); request.setAttribute("bills", bills); request.getRequestDispatcher("screen.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
QueryTypeServlet.java
package com.zzw.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zzw.entity.Bill; import com.zzw.service.IUserService; import com.zzw.service.Impl.UserServiceImpl; public class QueryTypeServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); String type=request.getParameter("btype"); IUserService userservice = new UserServiceImpl(); List<Bill> bills=userservice.QueryType(type); //out对象的获取方法 PrintWriter out = response.getWriter(); request.setAttribute("bills", bills); request.getRequestDispatcher("screentype.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
QueryDateServlet.java
package com.zzw.servlet; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zzw.entity.Bill; import com.zzw.service.IUserService; import com.zzw.service.Impl.UserServiceImpl; public class QueryDateServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("text/html; charset=utf-8"); String date=request.getParameter("bdate"); IUserService userservice = new UserServiceImpl(); List<Bill> bills=userservice.QueryDate(date); //out对象的获取方法 PrintWriter out = response.getWriter(); request.setAttribute("bills", bills); request.getRequestDispatcher("screendate.jsp").forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
UserServiceImpl.java
package com.zzw.service.Impl; import java.util.List; import com.zzw.dao.IUserDao; import com.zzw.dao.Impl.UserDaoImpl; import com.zzw.entity.Bill; import com.zzw.entity.User; import com.zzw.service.IUserService; public class UserServiceImpl implements IUserService{ IUserDao userdao= new UserDaoImpl(); //登录 public boolean Login(User user) { boolean flag=false; if(userdao.Login(user.getUname(),user.getUpwd())) { flag=true; } return flag; } //注册 public boolean Register(User user) { boolean flag=false; if(!userdao.isExist(user.getUname())) { userdao.Register(user); flag=true; }else { System.out.println("此人已存在"); } return flag; } //根据账号查询用户 public User Query(String uname) { return userdao.Query(uname); } //记账 public boolean AddBill(Bill bill) { boolean flag=false; if(userdao.AddBill(bill)) { flag=true; } return flag; } //根据账目编号进行删除 public boolean DeleteBill(int bid) { boolean flag=false; if(userdao.isExist(bid)) { userdao.DeleteBill(bid); flag=true; }else { System.out.println("此账目不存在"); } return flag; } //根据账目编号进行修改 public boolean UpdateBill(int bid,Bill bill) { boolean flag=false; if(userdao.isExist(bid)) { userdao.UpdateBill(bid,bill); flag=true; }else { System.out.println("此账目不存在"); } return flag; } //根据账目编号查询账目 public Bill Query(int bid) { return userdao.Query(bid); } //根据消费类型查询账单信息 public List<Bill> QueryType(String btype){ return userdao.QueryType(btype); } //根据日期查询账单信息 public List<Bill> QueryDate(String bdate) { return userdao.QueryDate(bdate); } //根据消费类型和日期查询账单信息 public List<Bill> QueryPart(String btype,String bdate) { return userdao.QueryPart(btype, bdate); } //查询全部账单信息 public List<Bill> QueryAll() { return userdao.QueryAll(); } }
IUserService.java
package com.zzw.service; import java.util.List; import com.zzw.entity.Bill; import com.zzw.entity.User; public interface IUserService { //登录 public boolean Login(User user); //注册 public boolean Register(User user) ; //根据账号查询用户 public User Query(String uname) ; //记账 public boolean AddBill(Bill bill) ; //根据账目编号进行删除 public boolean DeleteBill(int bid); //根据账目编号进行修改 public boolean UpdateBill(int bid,Bill bill) ; //根据账目编号查询账目 public Bill Query(int bid) ; //根据消费类型查询账单信息 public List<Bill> QueryType(String btype); //根据日期查询账单信息 public List<Bill> QueryDate(String bdate) ; //根据消费类型和日期查询账单信息 public List<Bill> QueryPart(String btype,String bdate) ; //查询全部账单信息 public List<Bill> QueryAll() ; }
UserDaoImpl.java
package com.zzw.dao.Impl; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.ArrayList; import com.zzw.dao.IUserDao; import com.zzw.entity.Bill; import com.zzw.entity.User; import com.zzw.utils.DBUtil; public class UserDaoImpl implements IUserDao{ //注册 public boolean Register(User user) { String sql="insert into user(uname,upwd,usex) values(?,?,?)" ; Object [] params= {user.getUname(),user.getUpwd(),user.getUsex()}; return DBUtil.executeUpdate(sql, params); } //查询账户是否存在 public boolean isExist(String uname) { return Query(uname)==null? false:true; } //登录 public boolean Login(String uname,String upwd) { return Query(uname,upwd)==null? false:true; } //根据账号查询用户全部信息 public User Query(String uname) { User user= null; ResultSet rs = null; try { String sql="select * from user where uname =?" ; Object [] params= {uname}; rs=DBUtil.executeQuery(sql, params); if(rs.next()) { String name=rs.getString("uname"); String pwd=rs.getString("upwd"); String sex=rs.getString("usex"); user= new User(name,pwd,sex); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return user; } //根据账户密码确定是否存在 public User Query(String uname,String upwd) { User user= null; ResultSet rs = null; try { String sql="select * from user where uname =? and upwd=?" ; Object [] params= {uname,upwd}; rs=DBUtil.executeQuery(sql, params); if(rs.next()) { String name=rs.getString("uname"); String pwd=rs.getString("upwd"); String sex=rs.getString("usex"); user= new User(name,pwd,sex); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return user; } //记账 public boolean AddBill(Bill bill) { String sql="insert into bill(btype,bmoney,bdate,bremark) values(?,?,?,?)" ; Object [] params= {bill.getBtype(),bill.getBmoney(),bill.getBdate(),bill.getBremark()}; return DBUtil.executeUpdate(sql, params); } //根据账目编号删除账目信息 public boolean DeleteBill(int bid) { String sql="delete from bill where bid=?" ; Object [] params= {bid}; return DBUtil.executeUpdate(sql, params); } //根据账目编号修改账目信息 public boolean UpdateBill(int bid,Bill bill) { String sql="update bill set btype =?,bmoney=?,bdate=?,bremark=? where bid =?" ; Object [] params= {bill.getBtype(),bill.getBmoney(),bill.getBdate(),bill.getBremark(),bid}; return DBUtil.executeUpdate(sql, params); } //查询账目是否存在 public boolean isExist(int bid) { return Query(bid)==null? false:true; } //根据账目编号查询账目信息 public Bill Query(int bid) { Bill bill= null; ResultSet rs = null; try { String sql="select * from bill where bid =? " ; Object [] params= {bid}; rs=DBUtil.executeQuery(sql, params); if(rs.next()) { int id=rs.getInt("bid"); String type=rs.getString("btype"); int money=rs.getInt("bmoney"); String date=rs.getString("bdate"); String remark=rs.getString("bremark"); bill=new Bill(id,type,money,date,remark); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bill; } //根据消费类型查询账单信息 public List<Bill> QueryType(String btype) { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill where btype=?" ; Object [] params= {btype}; rs=DBUtil.executeQuery(sql, params); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } //根据日期查询账单信息 public List<Bill> QueryDate(String bdate) { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill where bdate=?" ; Object [] params= {bdate}; rs=DBUtil.executeQuery(sql, params); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } //根据消费类型和日期查询账单信息 public List<Bill> QueryPart(String btype,String bdate) { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill where btype=?and bdate=?" ; Object [] params= {btype,bdate}; rs=DBUtil.executeQuery(sql, params); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } //查询全部账单信息 public List<Bill> QueryAll() { List<Bill> bills = new ArrayList<>(); Bill bill= null; ResultSet rs=null; try { String sql="select * from bill " ; rs=DBUtil.executeQuery(sql, null); while(rs.next()) { int id=rs.getInt("bid"); String type= rs.getString("btype"); int money=rs.getInt("bmoney"); String date= rs.getString("bdate"); String remark= rs.getString("bremark"); bill= new Bill(id,type,money,date,remark); bills.add(bill); } }catch(SQLException e) { e.printStackTrace(); }catch(Exception e) { e.printStackTrace(); }finally { try { //先开的后关,后开的先关 if(rs!=null)rs.close(); if(DBUtil.pstmt!=null)DBUtil.pstmt.close(); if(DBUtil.connection !=null)DBUtil.connection.close(); }catch(SQLException e) { e.printStackTrace(); }finally { } } return bills; } }
IUserDao.java
package com.zzw.dao; import java.util.List; import com.zzw.entity.Bill; import com.zzw.entity.User; public interface IUserDao { //注册 public boolean Register(User user) ; //查询账户是否存在 public boolean isExist(String uname) ; //登录 public boolean Login(String uname,String upwd) ; //根据帐号查询用户全部信息 public User Query(String uname) ; //记账 public boolean AddBill(Bill bill); //根据账目编号删除账目信息 public boolean DeleteBill(int bid); //根据账目编号修改账目信息 public boolean UpdateBill(int bid,Bill bill) ; //查询账目是否存在 public boolean isExist(int bid) ; //根据账目编号查询账目信息 public Bill Query(int bid); //根据消费类型查询账单信息 public List<Bill> QueryType(String btype); //根据日期查询账单信息 public List<Bill> QueryDate(String bdate) ; //根据消费类型和日期查询账单信息 public List<Bill> QueryPart(String btype,String bdate) ; //查询全部账单信息 public List<Bill> QueryAll() ; }
通过记账功能在数据库中记录如下账目
进入首页
查看全部账目
进入按日期与类型查看账目
点击查询
进入按照类型查询
点击按日期查询
来源:https://www.cnblogs.com/yeyueweiliang/p/12238961.html