记账系统java web
程序员文章站
2022-03-26 20:32:34
实体类 entityimport java.util.Date;public class Account { private Integer id; private String name;//标题 private Date time;//时间 private Integer category;//类别:0不限、1支出、2收入、3转账、4借出、5借入、6还入、7还出 private Double price;//金额 private String ex....
实体类 entity
import java.util.Date;
public class Account {
private Integer id;
private String name;//标题
private Date time;//时间
private Integer category;//类别:0不限、1支出、2收入、3转账、4借出、5借入、6还入、7还出
private Double price;//金额
private String explan;
public Account() {
}
public Account(String name, Date time, Integer category, Double price, String explan) {
this.name = name;
this.time = time;
this.category = category;
this.price = price;
this.explan = explan;
}
public Account(Integer id, String name, Date time, Integer category, Double price, String explan) {
this.id = id;
this.name = name;
this.time = time;
this.category = category;
this.price = price;
this.explan = explan;
}
// get set toString方法省略
}
Dao层
package dao;
import entity.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import util.DBUtils;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AccountDao {
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
/**
* 获取所有账单信息
* 当category为0时查询所有
*
* @return
*/
public List<Account> getAccountList(int category, Date startTime, Date endTime) {
String sql = "select id,name,`time`,category,price,explan from account where 1=1";
List param = new ArrayList();
if (category != 0) {
sql += " and category=? ";
param.add(category);
}
if (startTime != null) {
sql += " and `time`>? ";
param.add(startTime);
}
if (endTime != null) {
sql += " and `time`<? ";
param.add(endTime);
}
try {
return queryRunner.query(sql, new BeanListHandler<>(Account.class), param.toArray());
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 添加账单
* @param account
* @return
*/
public int addAccount(Account account){
String sql = "insert into account(name,`time`,category,price,explan) values(?,?,?,?,?)";
try {
return queryRunner.update(sql,account.getName(),account.getTime(),account.getCategory(),account.getPrice(),account.getExplan());
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}
service层
package service;
import dao.AccountDao;
import entity.Account;
import java.util.Date;
import java.util.List;
public class AccountService {
/**
* 根据category
* 查询所有账单
* @param category 要查的类别
* @return
*/
public List<Account> getAccountList(int category, Date startTime, Date endTime){
AccountDao accountDao = new AccountDao();
return accountDao.getAccountList(category,startTime,endTime);
}
/**
* 记账(添加账单)
* @param account
* @return
*/
public int addAccount(Account account){
AccountDao accountDao = new AccountDao();
return accountDao.addAccount(account);
}
}
contrallor层
package servlet;
import entity.Account;
import service.AccountService;
import util.ParameterUtil;
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 java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* 根据表单输入
* 获取账单信息
*/
@WebServlet(name = "AccountListServlet", urlPatterns = "/accountlist")
public class AccountListServlet extends HttpServlet {
private AccountService accountService = new AccountService();
private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Integer category = ParameterUtil.getInteger(request, "category");
String startTime = ParameterUtil.getString(request, "startTime");
String endTime = ParameterUtil.getString(request, "endTime");
List<Account> accountList = null;
if (startTime != null) {
try {
if (endTime != null) {
accountList = accountService.getAccountList(category, sf.parse(startTime), sf.parse(endTime));
} else {
accountList = accountService.getAccountList(category, sf.parse(startTime), null);
}
} catch (ParseException e) {
e.printStackTrace();
}
} else {
accountList = accountService.getAccountList(category, null, null);
}
request.setAttribute("accountList", accountList);
for (Account account : accountList) {
System.out.println(account);
}
request.getRequestDispatcher("accountlist.jsp").forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
import entity.Account;
import service.AccountService;
import util.ParameterUtil;
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 java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* 记账功能
*/
@WebServlet(name = "AddAccountServlet", urlPatterns = "/add")
public class AddAccountServlet extends HttpServlet {
private AccountService accountService = new AccountService();
private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = ParameterUtil.getString(request,"name");
String time = ParameterUtil.getString(request,"time");
Integer category = ParameterUtil.getInteger(request,"category");
Double price = ParameterUtil.getDouble(request,"price");
String explan = ParameterUtil.getString(request,"explan");
try {
Account account = new Account(name,sf.parse(time),category,price,explan);
int res = accountService.addAccount(account);
request.setAttribute("message",res>0?"记账成功":"记账失败");
request.getRequestDispatcher("accountlist.jsp").forward(request,response);
} catch (ParseException e) {
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
过滤器
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter("/*")
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("UTF-8");
servletResponse.setContentType("text/html;charset=UTF-8");
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}
工具类
package util;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DBUtils {
private static final Properties PROPERTIES = new Properties();
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<>();//ThreadLocal能保证当前线程共享同一个对象
private static DruidDataSource dataSource;//数据源 连接池
public static DruidDataSource getDataSource() {
return dataSource;
}
static {
InputStream is = DBUtils.class.getResourceAsStream("/db.properties");
try {
PROPERTIES.load(is);
//创建连接池
dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(PROPERTIES);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
try {
Connection connection = threadLocal.get();//先去拿,如果有的话,代表当前线程曾经打开过连接
if (connection == null) {//当前线程没有打开过连接
//连接池获取连接
connection = dataSource.getConnection();
threadLocal.set(connection);//将connection存入ThreadLocal,能保证当前线程使用的是同一个connection
}
return connection;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
package util;
import javax.servlet.http.HttpServletRequest;
public class ParameterUtil {
//从请求中获取String类型的参数
public static String getString(HttpServletRequest request, String name) {
String param = request.getParameter(name);
if (param == null || param.trim().length() == 0) {
return null;
}
return param.trim();
}
public static Integer getInteger(HttpServletRequest request, String name) {
String param = getString(request, name);
if (param == null) {//没传参数
return null;
}
try {
return Integer.parseInt(param);
} catch (NumberFormatException e) {//如果参数不是数字格式
e.printStackTrace();
}
return null;
}
public static Double getDouble(HttpServletRequest request, String name) {
String param = getString(request, name);
if (param == null) {//没传参数
return null;
}
try {
return Double.parseDouble(param);
} catch (NumberFormatException e) {//如果参数不是数字格式
e.printStackTrace();
}
return null;
}
}
配置文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/bank?useSSL=false&characterEncoding=utf-8
username=root
password=123456
initialSize=10
maxActive=50
minIdle=5
maxWait=5000
前端
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: EVA_01
Date: 2020/7/24 0024
Time: 20:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>账单</title>
</head>
<body>
<div align="center">
<div>
<h2>记账管理</h2>
</div>
<br/>
<div style="width: 60%">
<form method="post" action="accountlist">
类型:<select name="category">
<option value="0">不限</option>
<option value="1">支出</option>
<option value="2">收入</option>
<option value="3">转账</option>
<option value="4">借出</option>
<option value="5">借入</option>
<option value="6">还入</option>
<option value="7">还出</option>
</select>
时间:从<input type="text" name="startTime" >到<input type="text" name="endTime">
<input type="submit" value="搜索"> <a href="addaccount.jsp"><button type="button">记账</button></a>
</form>
<c:if test="${!requestScope.accountList.isEmpty()}">
<table border="1" style="border-color: white ;text-align: center" width="100%">
<tr bgcolor="#7cfc00">
<td>标题</td>
<td>记账时间</td>
<td>类别</td>
<td>金额</td>
<td>说明</td>
</tr>
<c:forEach items="${requestScope.accountList}" var="account" varStatus="i">
<tr bgcolor="${i.count%2!=0?'yellow':''}">
<td>${account.name}</td>
<td>${account.time}</td>
<td>
<c:choose>
<c:when test="${account.category==1}">支出</c:when>
<c:when test="${account.category==2}">收入</c:when>
<c:when test="${account.category==3}">转账</c:when>
<c:when test="${account.category==4}">借出</c:when>
<c:when test="${account.category==5}">借入</c:when>
<c:when test="${account.category==6}">还入</c:when>
<c:when test="${account.category==7}">还出</c:when>
<c:otherwise>不限</c:otherwise>
</c:choose>
</td>
<td>${account.price}</td>
<td>${account.explan}</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${requestScope.accountList.isEmpty()}">
<div align="center">
<h3>没有任何账单记录</h3>
</div>
</c:if>
</table>
${requestScope.message}
</div>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: EVA_01
Date: 2020/7/25 0025
Time: 9:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>记账</title>
<style>
body {
width: 35%;
margin: 0 auto;
}
div {
align-content: center;
}
.title {
text-align: center;
}
.modal-body {
width: auto;
align-content: center;
}
.tp input {
width: 182px;
}
.name input {
width: 420px;
}
.explan textarea {
width: 400px;
}
.button {
margin-left: 160px;
}
</style>
</head>
<body>
<form method="post" action="add">
<div>
<div class="title">
<h2>记账</h2>
</div>
<div class="modal-body">
<div>
类型:
<input type="radio" name="category" value="1">支出
<input type="radio" name="category" value="2">收入
<input type="radio" name="category" value="3">转账
<input type="radio" name="category" value="4">借出
<input type="radio" name="category" value="5">借入
<input type="radio" name="category" value="6">还入
<input type="radio" name="category" value="7">还出
</div>
<div class="name">
标题:<input type="text" name="name">
</div>
<div class="tp">
<span>
日期:<input type="text" name="time">
</span>
<span>
金额:<input type="text" name="price" value="0">
</span>
</div>
<div class="explan">
<span>说明:</span>
<textarea name="explan"></textarea>
</div>
<div class="button">
<input type="reset" value="重置">
<input type="submit" value="保存">
<a href="accountlist.jsp">
<button type="button">返回</button>
</a>
</div>
</div>
</div>
</form>
</body>
</html>
包
本文地址:https://blog.csdn.net/qq_41841482/article/details/107575661
上一篇: Netty框架构建Nio编程
下一篇: java在linux上创建文件,写入内容