使用JDBC技术完成一个简单的账务管理系统

一:账务系统的大致组成:

在这里插入图片描述
简单来说包括app层,controller层,dao层,domain层,service层,tools层,view层。
我的运行环境是:java version “1.8.0_211” 8.0.15 MySQL Community Server - GPL
在这儿也把我建立的mysql基本表给大家说一下
在这里插入图片描述

在这里插入图片描述

二:app层核心代码:

   package cn.yunfeiyang.gip.app;
    
    import cn.yunfeiyang.gip.view.MainView;
    
    /*
     * 主程序类,用来完成本项目的启动
     * */
    public class Mainapp {
    	public static void main(String[] args) {
    		new MainView().run();
    	}
    }

三:controller层核心代码:

 package cn.yunfeiyang.gip.controller;
    
    import java.util.List;
    
    import cn.yunfeiyang.gip.domain.Accounting;
    import cn.yunfeiyang.gip.service.AccountingService;
    
    /*
     * 控制器层
     * 接收视图层的数据  数据传递给Service层
     * 成员位置   创建Service对象
     * */
    public class AccountingController {
    	private AccountingService service = new AccountingService();
    
    	/*
    	 * 实现账务的编辑功能  调用service层
    	 * 由视图层调用
    	 * */
    	public void editAccounting(Accounting a) {
    		service.editAccounting(a);
    	}
    	
    	/*
    	 * 定义方法,实现账务添加功能 由视图层调用,传递参数(传递过来的参数不能是5个数据,传递的是一个Accounting类型的对象)
    	 * 本方法调用service层的方法,传递Accounting对象,获取到添加后的结果集(添加成功影响的行数,int)
    	 * 
    	 */
    	public void addAccounting(Accounting a) {
    		service.addAccounting(a);
    	}
    
    	/*
    	 * 定义方法,实现条件查询账务 方法由视图层调用,传递两个日期的字符串 调用service层的方法,传递两个日期字符串,获取结果集 
    	 * 结果集返回给视图
    	 */
    	public List<Accounting> select(String startDate, String endDate) {
    		return service.select(startDate, endDate);
    	}
    
    	/*
    	 * 控制层定义方法 实现查询所有的账务 方法由视图层调用 方法调用service层
    	 */
    	public List<Accounting> selectAll() {
    		return service.selectAll();
    	}
    
    	public void deleteAccounting(int gaid) {
    		service.deleteAccounting(gaid);
    	}
    }

四:dao层核心代码:

 package cn.yunfeiyang.gip.dao;
    /*
     * 实现对数据表gip_Accounting的增删改查操作
     * JDBCUtils工具类帮助实现  类成员创建QueryRunner对象   指定数据源
     * */
    
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import cn.yunfeiyang.gip.domain.Accounting;
    import cn.yunfeiyang.gip.tools.JDBCUtils;
    
    public class Accountingdao {
    	private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
    
    	/*
    	 * 编辑功能 由业务层 service层调用 更新数据库的操作
    	 */
    	public void editAccounting(Accounting a) {
    		try {
    		String sql = "UPDATE gip_Accounting SET gacname=?, gamoney=?"
    				+ ",gaaccount=?,gacreatetime=?,gadescription=? WHERE gaid =?";
    		Object [] params = {a.getGacname(), a.getGamoney(), a.getGaaccount(), a.getGacreatetime(),
    				a.getGadescription(),a.getGaid()};
    		// 调用qr对象方法  update 执行更新操作
    		qr.update(sql, params);
    		}catch (Exception ex) {
    			System.out.println(ex);
    			throw new RuntimeException("编辑账务失败");
    		}
    	}
    
    	/*
    	 * 定义方法,实现添加账务功能 由业务层调用,传递Accounting对象 将Accounting对象中的数据,添加到数据库
    	 */
    	public void addAccounting(Accounting a) {
    		try {
    			// 拼写添加数据的sql
    			String sql = "INSERT INTO gip_Accounting (gacname,gamoney,gaaccount,"
    					+ "gacreatetime,gadescription) VALUES(?,?,?,?,?)";
    			// 创建对象数组,处处5个占位符的实际参数
    			// 实际参数来源是传递过来的对象Accounting
    			Object[] params = { a.getGacname(), a.getGamoney(), a.getGaaccount(), a.getGacreatetime(),
    					a.getGadescription() };
    			// 调用qr对象中的方法update执行添加
    			qr.update(sql, params);
    		} catch (Exception ex) {
    			System.out.println(ex);
    			throw new RuntimeException("账务添加失败");
    		}
    	}
    
    	/*
    	 * 定义方法,查询数据库,带有条件去查询账务表 由业务层调用,查询结果集存储到Bean对象,存储到List集合 调用者传递2个日期字符串
    	 */
    	public List<Accounting> select(String startDate, String endDate) {
    		try {
    			String sql = "SELECT * FROM  gip_Accounting WHERE gacreatetime BETWEEN ? AND ?";
    			Object[] params = { startDate, endDate };
    			return qr.query(sql, new BeanListHandler<>(Accounting.class), params);
    		} catch (Exception ex) {
    			System.out.println(ex);
    			throw new RuntimeException("条件查询失败");
    		}
    	}
    
    	/*
    	 * 定义方法 查询数据库 获得所有的账务 数据 该方法 由Service业务层调用 结果集 将所有的账务数据 储存在Bean对象中 
    	 * 再将这些对象储存在 集合中
    	 */
    	public List<Accounting> selectAll() {
    		try {
    			// 查询账务数据的SQL语句
    			String sql = "SELECT * FROM  gip_Accounting";
    			// 调用qr对象的方法 query方法 结果集 BeanListHandler
    			List<Accounting> list = qr.query(sql, new BeanListHandler<>(Accounting.class));
    			return list;
    		} catch (Exception ex) {
    			System.out.println(ex);
    			throw new RuntimeException("查询所有账务失败");
    		}
    	}
    
    	public void deleteAccounting(int gaid) {
    		try {
    		String sql = "DELETE FROM gip_Accounting WHERE gaid=?";
    		qr.update(sql,gaid);
    		}catch (Exception ex) {
    			System.out.println(ex);
    			throw new RuntimeException("删除数据失败");
    		}
    	}
    }

五:domain层核心代码:

  package cn.yunfeiyang.gip.domain;
    
    /*
     * 存放JavaBean
     * */
    public class Accounting {
    	private int gaid;
    	private String gacname;
    	private double gamoney;
    	private String gaaccount;
    	private String gacreatetime;
    	private String gadescription;
    
    	public Accounting() {
    	}
    
    	public Accounting(int gaid, String gacname, double gamoney, String gaaccount, String gacreatetime,
    			String gadescription) {
    		super();
    		this.gaid = gaid;
    		this.gacname = gacname;
    		this.gamoney = gamoney;
    		this.gaaccount = gaaccount;
    		this.gacreatetime = gacreatetime;
    		this.gadescription = gadescription;
    	}
    
    	/*
    	 * (非 Javadoc)
    	 * 
    	 * @see java.lang.Object#clone()
    	 */
    	@Override
    	protected Object clone() throws CloneNotSupportedException {
    		// TODO 自动生成的方法存根
    		return super.clone();
    	}
    
    	/*
    	 * (非 Javadoc)
    	 * 
    	 * @see java.lang.Object#finalize()
    	 */
    	@Override
    	protected void finalize() throws Throwable {
    		// TODO 自动生成的方法存根
    		super.finalize();
    	}
    
    	/*
    	 * (非 Javadoc)
    	 * 
    	 * @see java.lang.Object#toString()
    	 */
    	@Override
    	public String toString() {
    		return "Accounting [gaid=" + gaid + ", gacname=" + gacname + ", gamoney=" + gamoney + ", gaaccount=" + gaaccount
    				+ ", gacreatetime=" + gacreatetime + ", gadescription=" + gadescription + ", hashCode()=" + hashCode()
    				+ ", getGaid()=" + getGaid() + ", getGacname()=" + getGacname() + ", getGamoney()=" + getGamoney()
    				+ ", getGaaccount()=" + getGaaccount() + ", getGacreatetime()=" + getGacreatetime()
    				+ ", getGadescription()=" + getGadescription() + ", getClass()=" + getClass() + ", toString()="
    				+ super.toString() + "]";
    	}
    
    	/*
    	 * (非 Javadoc)
    	 * 
    	 * @see java.lang.Object#hashCode()
    	 */
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + ((gaaccount == null) ? 0 : gaaccount.hashCode());
    		result = prime * result + ((gacname == null) ? 0 : gacname.hashCode());
    		result = prime * result + ((gacreatetime == null) ? 0 : gacreatetime.hashCode());
    		result = prime * result + ((gadescription == null) ? 0 : gadescription.hashCode());
    		result = prime * result + gaid;
    		long temp;
    		temp = Double.doubleToLongBits(gamoney);
    		result = prime * result + (int) (temp ^ (temp >>> 32));
    		return result;
    	}
    
    	/*
    	 * (非 Javadoc)
    	 * 
    	 * @see java.lang.Object#equals(java.lang.Object)
    	 */
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		Accounting other = (Accounting) obj;
    		if (gaaccount == null) {
    			if (other.gaaccount != null)
    				return false;
    		} else if (!gaaccount.equals(other.gaaccount))
    			return false;
    		if (gacname == null) {
    			if (other.gacname != null)
    				return false;
    		} else if (!gacname.equals(other.gacname))
    			return false;
    		if (gacreatetime == null) {
    			if (other.gacreatetime != null)
    				return false;
    		} else if (!gacreatetime.equals(other.gacreatetime))
    			return false;
    		if (gadescription == null) {
    			if (other.gadescription != null)
    				return false;
    		} else if (!gadescription.equals(other.gadescription))
    			return false;
    		if (gaid != other.gaid)
    			return false;
    		if (Double.doubleToLongBits(gamoney) != Double.doubleToLongBits(other.gamoney))
    			return false;
    		return true;
    	}
    
    	/**
    	 * @return gaid
    	 */
    	public int getGaid() {
    		return gaid;
    	}
    
    	/**
    	 * @param gaid 要设置的 gaid
    	 */
    	public void setGaid(int gaid) {
    		this.gaid = gaid;
    	}
    
    	/**
    	 * @return gacname
    	 */
    	public String getGacname() {
    		return gacname;
    	}
    
    	/**
    	 * @param gacname 要设置的 gacname
    	 */
    	public void setGacname(String gacname) {
    		this.gacname = gacname;
    	}
    
    	/**
    	 * @return gamoney
    	 */
    	public double getGamoney() {
    		return gamoney;
    	}
    
    	/**
    	 * @param gamoney 要设置的 gamoney
    	 */
    	public void setGamoney(double gamoney) {
    		this.gamoney = gamoney;
    	}
    
    	/**
    	 * @return gaaccount
    	 */
    	public String getGaaccount() {
    		return gaaccount;
    	}
    
    	/**
    	 * @param gaaccount 要设置的 gaaccount
    	 */
    	public void setGaaccount(String gaaccount) {
    		this.gaaccount = gaaccount;
    	}
    
    	/**
    	 * @return gacreatetime
    	 */
    	public String getGacreatetime() {
    		return gacreatetime;
    	}
    
    	/**
    	 * @param gacreatetime 要设置的 gacreatetime
    	 */
    	public void setGacreatetime(String gacreatetime) {
    		this.gacreatetime = gacreatetime;
    	}
    
    	/**
    	 * @return gadescription
    	 */
    	public String getGadescription() {
    		return gadescription;
    	}
    
    	/**
    	 * @param gadescription 要设置的 gadescription
    	 */
    	public void setGadescription(String gadescription) {
    		this.gadescription = gadescription;
    	}
    }

六:service层核心代码:

  package cn.yunfeiyang.gip.service;
    
    import java.util.List;
    
    import cn.yunfeiyang.gip.dao.Accountingdao;
    import cn.yunfeiyang.gip.domain.Accounting;
    
    /*
     *服务层(业务层)  接收上一层---->  controller 层的数据
     *经过计算  传给dao层    操作数据库
     *调用dao层的类  类成员位置  创建  dao类的对象 
     * */
    public class AccountingService {
    	private Accountingdao dao = new Accountingdao();
    
    	/*
    	 * 定义方法,实现添加账务 是由控制层调用,传递Accounting对象
    	 */
    	public void addAccounting(Accounting a) {
    		dao.addAccounting(a);
    	}
    
    	/*
    	 * 定义方法,实现条件查询账务 方法由控制层调用,传递2个日期字符串 调用dao层的方法,传递2个日期字符串 获取到查询结果集
    	 */
    	public List<Accounting> select(String startDate, String endDate) {
    		return dao.select(startDate, endDate);
    	}
    
    	/*
    	 * 定义方法 查询所有的账务数据 此方法 由控制层 调用 去调用dao层方法 返回存储Accounting 对象的List集合
    	 */
    	public List<Accounting> selectAll() {
    		return dao.selectAll();
    	}
    	/*
    	 * 调用dao层方法  由控制层调用
    	 * */
    	public void editAccounting(Accounting a) {
    		dao.editAccounting(a);
    	}
    
    	public void deleteAccounting(int gaid) {
    		// TODO 自动生成的方法存根
    		dao.deleteAccounting(gaid);
    	}
    }

七:tools层核心代码:

   package cn.yunfeiyang.gip.tools;
    
    import javax.sql.DataSource;
    
    import org.apache.commons.dbcp.BasicDataSource;
    
    /*
     * 获取数据库连接的工具类
     * 实现连接池  dbcp连接池
     * 创建BasicDataSource对象
     * 静态代码块  设置必要的参数
     * */
    public class JDBCUtils {
    	private static BasicDataSource dataSource = new BasicDataSource();
    	
    	static {
    		dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    		dataSource.setUrl("jdbc:mysql://localhost:3306/gip?useUnicode="
    				+ "true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false");
    		dataSource.setUsername("root");
    		dataSource.setPassword("nyist123");
    		dataSource.setInitialSize(10);
    		dataSource.setMaxActive(8);
    		dataSource.setMaxIdle(3);
    		dataSource.setMinIdle(1);
    	}
    	public static DataSource getDataSource() {
    		return dataSource;
    	}
    }

八:view层核心代码:

package cn.yunfeiyang.gip.view;

import java.util.List;
import java.util.Scanner;

import cn.yunfeiyang.gip.controller.AccountingController;
import cn.yunfeiyang.gip.domain.Accounting;

/*
 * 视图层:用户看到和操作的界面
 * 数据传递给Controller层实现
 * 类成员位置   创建Controller层的对象
 * */
public class MainView {
	private AccountingController Controller = new AccountingController();

	/*
	 * 实现界面的效果 接收用户的输入 根据 用户输入的数据 调用对应的方法
	 */
	public void run() {
		// 创建Scanner类对象,反复键盘输入
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		while (true) {
			System.out.println("--------欢迎使用gip家庭记账软件------------------------------------"); //gip是我随意起的一个名字。
			System.out.println("---------------gip家庭记账软件---------------------------------");
			System.out.println("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统");
			System.out.println("请输入要操作的功能序号1--5:");
			// 接收用户的菜单选择
			int choose = in.nextInt();
			// 对选择的菜单判断,调用不同的功能
			switch (choose) {
			case 1:
				addAccounting();
				// 选择添加账务,调用添加账务的方法
				break;
			case 2:
				editAccounting();
				// 选择的编辑账务,调用编辑账务方法
				break;
			case 3:
				deleteAccounting();
				// 选择的删除账务,调用删除账务方法
				break;
			case 4:
				// 选择的是查询账务,调用查询方法
				selectAccounting();
				break;
			case 5:
				System.out.println("bye!!!");
				System.exit(0);
			}
		}
	}

	/*
	 * 定义方法 selectAccounting() 显示查询的方式 1 所有查询 2 条件查询 接收用户的选择
	 */
	public void selectAccounting() {
		System.out.println("1. 查询所有-----------2. 条件查询");
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		int selectChoice = in.nextInt();
		// 判断根据用户的选择,调用不同的功能
		switch (selectChoice) {
		case 1:
			// 选择的查询所有,调用查询所有的方法
			selectAll();
			break;
		case 2:
			// 选的条件查询,调用带有查询条件的方法
			select();
			break;
		}
	}

	/*
	 * 定义方法 实现删除功能
	 */
	public void deleteAccounting() {
		// 首先先输出所有的账务数据 选择主键ID值进行删除
		selectAll();
		System.out.println("选择的是删除功能.............");
		System.out.println("请输入待删除的账务的ID值.....");
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		int gaid = in.nextInt();
		System.out.println("您真的要删除吗? 【1】yes 【2】 no");
		int a = 0;  int i = 0;
	     a = in.nextInt();
		if(a==1) {
			i=1;
		}
		if(a==0) {
			
		}
		if (i == 1) {
			Controller.deleteAccounting(gaid);
			System.out.println("删除账务成功");
		}
		if(i==0) {
			System.out.println("请重新选择...............");
		}
	}

	/*
	 * 定义方法,实现对账务的编辑功能 实现思想: 接收用户的输入的信息 封装成Accounting对象 调用控制层的方法,传递Accounting对象,实现编辑
	 * 编辑账务 操作 首先 打印所有账务信息
	 */
	public void editAccounting() {
		// 调用查询所有账务数据的功能,显示出来
		// 看到所有数据,从中选择一项,进行修改
		selectAll();
		System.out.println("选择的是编辑功能............");
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		System.out.println("请输入ID:");
		int gaid = in.nextInt();
		System.out.println("输入分类名称:");
		String gacname = in.next();
		System.out.println("输入金额:");
		double gamoney = in.nextDouble();
		System.out.println("输入账户:");
		String gaaccount = in.next();
		System.out.println("输入日期:格式XXXX-XX-xx:");
		String gacreatetime = in.next();
		System.out.println("输入具体描述:");
		String gadescription = in.next();
		// 将用户输入的数据,封装到ZhangWu对象中
		// 用户输入的ID,必须封装到到对象中
		Accounting a = new Accounting(gaid, gacname, gamoney, gaaccount, gacreatetime, gadescription);
		// 调用controller层中的方法,实现编辑账务
		Controller.editAccounting(a);
		System.out.println("账务编辑成功");
	}

	/*
	 * 定义方法addAccounting 添加账务的方法,用户在界面中选择菜单1的时候调用、 实现思想: 接收键盘输入,5项输入,调用controller层方法
	 */
	public void addAccounting() {
		System.out.println("已选择添加账务功能");
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		System.out.println("输入分类名称");
		String gacname = in.next();
		System.out.println("输入金额");
		double gamoney = in.nextDouble();
		System.out.println("输入账户");
		String gaaccount = in.next();
		System.out.println("输入日期:格式XXXX-XX-xx");
		String gacreatetime = in.next();
		System.out.println("输入具体描述");
		String gadescription = in.next();
		// 将接收到的数据,调用controller层的方法,传递参数,实现数据添加
		// 将用户输入的所有参数,封装成Accounting 对象
		Accounting a = new Accounting(0, gacname, gamoney, gaaccount, gacreatetime, gadescription);
		Controller.addAccounting(a);
		;
		System.out.println("恭喜添加账务成功");
	}

	/*
	 * 定义方法,实现查询所有的账务数据
	 */
	public void selectAll() {
		// 调用Controller 控制层中的方法,查询所有的账务数据
		List<Accounting> list = Controller.selectAll();
		if (list.size() != 0) {
			print(list);
		} else {
			System.out.println("没有查到符合条件的数据");
		}
	}

	private void print(List<Accounting> list) {
		// 输出表头
		System.out.println("gaid\tgacname\tgamoney\tgaaccount\tgacreatetime\tgadescription");
		// 遍历集合 输出 控制台
		for (Accounting a : list) {
			System.out.println(a.getGaid() + "\t" + a.getGacname() + "\t" + a.getGamoney() + "\t" + a.getGaaccount()
					+ "\t\t" + a.getGacreatetime() + "\t" + a.getGadescription());
		}
	}

	/*
	 * 定义方法,实现条件查询账务数据 提供用户的输入日期,开始日期结束日期 就2个日期,传递到controller层
	 * 调用controller的方法,传递2个日期参数 获取到controller查询的结果集,打印出来
	 */
	public void select() {
		System.out.println("选择条件查询,日期的格式为xxxx-xx-xx");
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		System.out.print("请输入开始日期:");
		String startDate = in.nextLine();
		System.out.print("请输入结束日期:");
		String endDate = in.nextLine();
		// 调用Controller层的方法 传递日期 获取查询的结果集
		List<Accounting> list = Controller.select(startDate, endDate);
		if (list.size() != 0) {
			print(list);
		} else {
			System.out.println("没有查到符合条件的数据");
		}
	}

}
发布了19 篇原创文章 · 获赞 24 · 访问量 1378

猜你喜欢

转载自blog.csdn.net/qq_43612538/article/details/96478632