從首頁開始進行測試javascript
screen.jspcss
<%@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.jsphtml
<%@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.jspjava
<%@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.javajquery
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.javasql
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.javajsp
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.javapost
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() ; }
經過記帳功能在數據庫中記錄以下帳目
進入首頁
查看所有帳目
進入按日期與類型查看帳目
點擊查詢
進入按照類型查詢
點擊按日期查詢