其他分享
首页 > 其他分享> > 实现家庭记账本的展示全部账目以及对账目进行删除和修改操作

实现家庭记账本的展示全部账目以及对账目进行删除和修改操作

作者:互联网

本次演示,直接从登陆完成后开始

show.jsp

<%@page import="java.util.List"%>
<%@page import="com.zzw.entity.Bill"%>
<%@ 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');
        }
        .show-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="show-panel">
      <a href=index.jsp>返回系统首页</a><br>
          <table border="1px">
         <tr>
         <th>账目编号</th>
         <th>账目类型</th>
         <th>金额</th>
         <th>账目日期</th>
         </tr>
         <%
         List <Bill> bills =(List<Bill>) request.getAttribute("bills");
           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>

billinfo.jsp

<%@page import="com.zzw.entity.Bill"%>
<%@ 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>
</head>
<body>
 <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');
        }
        .info-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="info-panel">
         <%
       Bill bill= (Bill)request.getAttribute("bill");
    %>
    <a href=index.jsp>返回系统首页</a><br>
    <form action="UpdateBillServlet">
    <div style="text-align:center">
             账目编号<input type="text" name="bid"  value="<%=bill.getBid() %>" readonly="readonly">
    </div>
        <div style="text-align:center">
             账目类型<input type="text" name="btype"  value="<%=bill.getBtype() %>" readonly="readonly">
    </div>
    <div style="text-align:center">
     金额<input type="text"  name="bmoney" value="<%=bill.getBmoney()%>"><br/>
    </div>
    <div style="text-align:center">
     日期<input type="date"  name="bdate" value="<%=bill.getBdate()%>"><br/>
    </div>
    <div style="text-align:center">
     备注<input type="text"  name="bremark" value="<%=bill.getBremark()%>"><br/>
    </div>
    <div style="text-align:center">
      <input type="submit" value="修改"><br/>
    </div>
    </form>
      </div>
      </div>
</body>
</html>

UpdateBillServlet.java

package com.zzw.servlet;

import java.io.IOException;
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 UpdateBillServlet 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");
        //获取待修改人的学号
        int id= Integer.parseInt(request.getParameter("bid"));
        //获取修改后的内容
        String type=request.getParameter("btype");
        int money= Integer.parseInt(request.getParameter("bmoney"));
        String date= request.getParameter("bdate");
        String remark= request.getParameter("bremark");
        //将修改后的内容封装到一个实体类中
        Bill bill = new Bill(type,money,date,remark);
        IUserService userservice = new UserServiceImpl();
        boolean result=userservice.UpdateBill(id,bill);
        if(!result) {
            request.setAttribute("message","error");
        }else {
            request.setAttribute("message","noerror");
        }
        request.getRequestDispatcher("QueryAllBillServlet").forward(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

DeleteBillServlet.java

package com.zzw.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.zzw.service.IUserService;
import com.zzw.service.Impl.UserServiceImpl;

public class DeleteBillServlet 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");
        int id= Integer.parseInt(request.getParameter("bid"));
        IUserService userservice = new UserServiceImpl();
        boolean result=userservice.DeleteBill(id);

        if(!result) {
            request.setAttribute("message","error");
        }else {
            request.setAttribute("message","noerror");
        }
        request.getRequestDispatcher("QueryAllBillServlet").forward(request, response);
    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

QueryBillServlet.java

package com.zzw.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 QueryBillServlet 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");
        //获取待查询修改人的学号
        int id= Integer.parseInt(request.getParameter("bid"));
        
        IUserService userservice = new UserServiceImpl();
        Bill bill=userservice.Query(id);
        //out对象的获取方法
        PrintWriter out = response.getWriter();
        out.println(bill);
        request.setAttribute("bill", bill);
        request.getRequestDispatcher("billinfo.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

QueryAllBill.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 QueryAllBillServlet 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");
        
        IUserService userservice = new UserServiceImpl();
        List<Bill> bills=userservice.QueryAll();
        //out对象的获取方法
        PrintWriter out = response.getWriter();
        request.setAttribute("bills", bills);
        request.getRequestDispatcher("show.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> 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> 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> 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> QueryAll() ;
       
}

 

 

 

下面是测试截图

 

 

 

 此时,数据库中有我通过记账功能添加的三条账目

 

 点击查看全部账目

 

 此时未展示备注,点击查询

 

 将金额修改为400

 

 

 

 删除教育账目

 

标签:String,删除,rs,bill,request,记账,import,public,账目
来源: https://www.cnblogs.com/yeyueweiliang/p/12236704.html