1.创建数据库
/*
SQLyog Ultimate v12.4.0 (64 bit)
MySQL - 5.5.49
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;
create table `gjp_zhangwu` (
`zwid` int (11),
`flname` varchar (600),
`money` double ,
`zhangHu` varchar (300),
`createtime` date ,
`description` varchar (3000)
);
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('1','吃饭支出','247','交通银行','2016-03-02','家庭聚餐');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('2','工资收入','12345','现金','2016-03-15','开工资了');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('3','服装支出','1998','现金','2016-04-02','买衣服');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('4','吃饭支出','325','现金','2016-06-18','朋友聚餐');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('5','股票收入','8000','工商银行','2016-10-28','股票大涨');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('6','股票收入','5000','工商银行','2016-10-28','股票又大涨');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('7','工资收入','5000','交通银行','2016-10-28','又开工资了');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('8','礼金支出','5000','现金','2016-10-28','朋友结婚');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('9','其他支出','1560','现金','2016-10-29','丢钱了');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('10','交通支出','2300','交通银行','2016-10-29','油价还在涨啊');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('11','吃饭支出','1000','工商银行','2016-10-29','又吃饭');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('12','工资收入','1000','现金','2016-10-30','开资');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('13','交通支出','2000','现金','2016-10-30','机票好贵');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('14','工资收入','5000','现金','2016-10-30','又开资');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('17','发压岁钱','500','现金','2017-01-27','给小朋友们发的压岁钱');
insert into `gjp_zhangwu` (`zwid`, `flname`, `money`, `zhangHu`, `createtime`, `description`) values('18','干活','100','现金','2017-01-01','好开心啊');
2.导入需要的jar包
3.创建JDBC连接数据库的工具类
名字为JDBCUtils.java
package cn.itcast.gjp.tools;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class JDBCUtils {
public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/gjp";
public static final String USERNAME = "root";
public static final String PASSWORD = "123456";
private static BasicDataSource dataSource = new BasicDataSource();
static {
dataSource.setDriverClassName(DRIVER_CLASS_NAME);
dataSource.setUrl(URL);
dataSource.setUsername(USERNAME);
dataSource.setPassword(PASSWORD);
}
public static DataSource getDataSource() {
return dataSource;
}
}
4.创建数据库表的domain类
package cn.itcast.gjp.domain;
/**
* 账务类
* @author Administrator
*
*/
public class ZhangWu {
private int zwid;// id
private String flname;// 分类名称
private String zhangHu;// 账户名称
private double money;// 金额
private String createtime;// 创建时间
private String description;// 说明
public ZhangWu(int zwid, String flname, String zhangHu, double money,
String createtime, String description) {
super();
this.zwid = zwid;
this.flname = flname;
this.zhangHu = zhangHu;
this.money = money;
this.createtime = createtime;
this.description = description;
}
public ZhangWu() {
super();
// TODO Auto-generated constructor stub
}
public String getFlname() {
return flname;
}
public void setFlname(String flname) {
this.flname = flname;
}
public String getZhangHu() {
return zhangHu;
}
public void setZhangHu(String zhangHu) {
this.zhangHu = zhangHu;
}
public int getZwid() {
return zwid;
}
public void setZwid(int zwid) {
this.zwid = zwid;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public String getCreatetime() {
return createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "ZhangWu [zwid=" + zwid + ", flname=" + flname + ", zhangHu="
+ zhangHu + ", money=" + money + ", createtime=" + createtime
+ ", description=" + description + "]";
}
}
5.程序的逻辑视图,存放逻辑代码
package cn.itcast.gjp.view;
import java.util.List;
import java.util.Scanner;
import cn.itcast.gjp.controller.ZhangWuController;
import cn.itcast.gjp.domain.ZhangWu;
public class MainView {
//本项目中view依赖service
private ZhangWuController controller = new ZhangWuController();
public void run() {
/*
* 运行方法
* 1.打印菜单,2,获取用户输入,3,调用对应方法
*/
boolean flag = true;
Scanner sc = new Scanner(System.in);
while(flag) {
System.out.println("---------------管家婆家庭记账软件---------------");
System.out.println("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统");
System.out.println("请输入要操作的功能序号[1-5]:");
int choose = sc.nextInt();
switch (choose) {
case 1:
//添加账务信息
addZhangWu();
break;
case 2:
//编辑账务信息
editZhangWu();
break;
case 3:
//删除账务
deleteZhangWu();
break;
case 4:
//查询账务
selectZhangWu();
break;
case 5:
//退出系统
System.out.println("再见!");
flag = false;
break;
default:
System.out.println("输入错误,请重新输入");
}
}
}
/**
* 删除账务中的某一条数据
*/
public void deleteZhangWu() {
//先显示出表中的所有的数据
selectAll();
Scanner sc = new Scanner(System.in);
System.out.println("请输入要删除数据的ID值");
int zwid = sc.nextInt();
//调用控制层的删除方法
int num = controller.deleteZhangWu(zwid);
if(num > 0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
/**
* 编辑账务的方法
*/
public void editZhangWu() {
selectAll();
System.out.println("您选择的是编辑账务,请输入数据");
Scanner sc = new Scanner(System.in);
System.out.println("请输入编辑的ID值");
int zwid = sc.nextInt();
System.out.println("请输入类别:");
String flname = sc.next();
System.out.println("请输入金额:");
double money = sc.nextDouble();
System.out.println("请输入账户:");
//next和nextLine有什么区别
String zhangHu = sc.next();
System.out.println("请输入时间:");
String createtime = sc.next();
System.out.println("请输入描述:");
String description = sc.next();
//封装为ZhuangWu对象
ZhangWu zhangWu = new ZhangWu(zwid, flname, zhangHu, money, createtime, description);
//调用控制层的editZhangWu方法
int num = controller.editZhangWu(zhangWu);
if(num > 0) {
System.out.println("编辑成功");
}else {
System.out.println("编辑失败");
}
selectAll();
}
/**
* 查询账务方法
*/
public void selectZhangWu() {
System.out.println("1.查询所有 2.按条件查询");
Scanner in = new Scanner(System.in);
int selectChoose = in.nextInt();
switch (selectChoose) {
case 1:
//查询所有的账务
selectAll();
break;
case 2:
//按条件查询账务
select();
break;
default:
System.out.println("输入错误");
}
}
/**
* 添加账务的方法,addZhangWu()
* 键盘输入新添加的账务信息
调用ZhangWuController类中addZhangWu方法,用来指定账务的添加
添加完毕后,使用输出语句,提示“添加账务成功!”
*
*/
public void addZhangWu() {
System.out.println("添加账户功能");
Scanner sc = new Scanner(System.in);
System.out.println("请输入类别:");
String flname = sc.next();
System.out.println("请输入金额:");
double money = sc.nextDouble();
System.out.println("请输入账户:");
//next和nextLine有什么区别
String zhangHu = sc.next();
System.out.println("请输入时间:");
String createtime = sc.next();
System.out.println("请输入描述:");
String description = sc.next();
//将输入的内容封装成ZhangWu对象
ZhangWu zhangWu = new ZhangWu(0, flname, zhangHu, money, createtime, description);
//调用控制层的addZhangWu的方法
int num = controller.addZhangWu(zhangWu);
if(num > 0) {
System.out.println("添加账务成功");
}else {
System.out.println("添加账务失败");
}
}
/**
* 查询所有账务方法
*/
public void selectAll() {
//调用控制层的方法,查询所有的账务数据
List<ZhangWu> list = controller.selectAll();
print(list);
}
private void print(List<ZhangWu> list) {
//打印一条表头
System.out.println("ID\t类别\t\t账户\t\t金额\t\t时间\t\t说明");
//遍历集合拿到账务中所有的数据
//一定要用ZhangWu类中的get方法获取字段,不然拿到的就是每一条数据的地址值
for (ZhangWu zw : list) {
System.out.println(zw.getZwid() + "\t" + zw.getFlname() + "\t\t"
+ zw.getZhangHu() + "\t\t" + zw.getMoney() + "\t\t"
+ zw.getCreatetime() + "\t" + zw.getDescription());
}
}
/**
* 定义方法,实现条件查询账务
* 提供用户的输入日期,开始日期和结束日期,将两个日期传递到controller层,传递两个日期参数
* 获取到controller层的查询结果,遍历集合
*/
public void select() {
System.out.println("请输入条件查询的开始和结束日期");
Scanner sc = new Scanner(System.in);
System.out.print("请输入开始日期:");
String startDate = sc.nextLine();
System.out.print("请输入结束日期:");
String endDate = sc.nextLine();
//调用controller层条件查询的方法,获取查询结果集
List<ZhangWu> list = controller.select(startDate, endDate);
if(list.size() > 0) {
print(list);
}else {
System.out.println("日期输入错误,请重新操作");
}
}
}
6.账务的数据层
package cn.itcast.gjp.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import cn.itcast.gjp.domain.ZhangWu;
import cn.itcast.gjp.tools.JDBCUtils;
/**
* 账务数据层类
*
* @author Administrator
*
*/
public class ZhangWuDao {
// 获取数据库连接池,得到操作表数据的对象 QueryRunner
private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
/**
* 删除账务的方法
*/
public int deleteZhangWu(int zwid) {
try{
String sql = "DELETE FROM gjp_zhangwu WHERE zwid=?";
Object[] params = {zwid};
return qr.update(sql, params);
}catch(SQLException ex) {
System.out.println(ex);
throw new RuntimeException("删除账务失败");
}
}
/**
* 编辑账务的方法
*/
public int editZhangWu(ZhangWu zhangWu) {
try {
// 编辑的sql语句
String sql = "UPDATE gjp_zhangwu SET flname=?,money=?,zhangHu=?,createtime=?,description=? WHERE zwid=?;";
// 创建对象数组,存储占位符中的内容
Object[] params = { zhangWu.getFlname(), zhangWu.getMoney(),
zhangWu.getZhangHu(), zhangWu.getCreatetime(),
zhangWu.getDescription(), zhangWu.getZwid() };
return qr.update(sql, params);
} catch (SQLException ex) {
System.out.println(ex);
throw new RuntimeException("编辑账务失败,请重现选择");
}
}
/**
* 添加账务的方法
*/
public int addZhangWu(ZhangWu zhangWu) {
try {
// 定义添加账务的sql语句
String sql = "INSERT INTO gjp_zhangwu(flname,money,zhangHu,createtime,description)VALUES (?,?,?,?,?)";
// 定义占位符的对象数组
Object[] params = { zhangWu.getFlname(), zhangWu.getMoney(),
zhangWu.getZhangHu(), zhangWu.getCreatetime(),
zhangWu.getDescription() };
//
return qr.update(sql, params);
} catch (SQLException ex) {
System.out.println(ex);
throw new RuntimeException("添加账务失败");
}
}
/**
* 定义方法,实现条件查询
*
* 返回查询到的list集合
*/
public List<ZhangWu> select(String startDate, String endDate) {
try {
// 写出需要查询的条件sql语句
String sql = "SELECT *FROM gjp_zhangwu WHERE createtime BETWEEN ? AND ?";
// 定义对象数组,存储?占位符的数据
Object[] params = { startDate, endDate };
return qr.query(sql, new BeanListHandler<ZhangWu>(ZhangWu.class),
params);
} catch (SQLException ex) {
System.out.println(ex);
throw new RuntimeException("条件查询失败");
}
}
/**
* 定义方法,实现查询所有的数据 此方法由业务层调用 结果集,将所有的账务数据,存储到Bean对象中,然后再存储到集合中
*/
public List<ZhangWu> selectAll() {
try {
// 查询所有账务的sql语句
String sql = "SELECT *FROM gjp_zhangwu";
// 调用QueryRunner的query方法
List<ZhangWu> list = qr.query(sql, new BeanListHandler<ZhangWu>(
ZhangWu.class));
return list;
} catch (SQLException ex) {
System.out.println(ex);
throw new RuntimeException("查询所有账务失败");
}
}
}
7.账务的业务层类
package cn.itcast.gjp.service;
import java.util.List;
import cn.itcast.gjp.dao.ZhangWuDao;
import cn.itcast.gjp.domain.ZhangWu;
/**
* 账务业务层类
* @author Administrator
*
*/
public class ZhangWuService {
//service都依赖dao
private ZhangWuDao dao = new ZhangWuDao();
/**
* 删除账务的方法
*/
public int deleteZhangWu(int zwid) {
return dao.deleteZhangWu(zwid);
}
/**
* 编辑账务的方法
*
*/
public int editZhangWu(ZhangWu zhangWu) {
return dao.editZhangWu(zhangWu);
}
/**
* 添加账务的方法
*/
public int addZhangWu(ZhangWu zhangWu) {
return dao.addZhangWu(zhangWu);
}
/**
* 定义方法,实现条件查询
* 调用dao层的条件查询方法
* 返回查询到的list集合
*/
public List<ZhangWu> select(String startDate, String endDate) {
return dao.select(startDate, endDate);
}
/**
* 定义方法,实现查询所有的账务数据
* 此方法,由控制层调用,去调用dao层的方法
*
*/
public List<ZhangWu> selectAll() {
return dao.selectAll();
}
}
8.账务的控制器层
package cn.itcast.gjp.controller;
import java.util.List;
import cn.itcast.gjp.domain.ZhangWu;
import cn.itcast.gjp.service.ZhangWuService;
/*
* 控制器层
* 接收用户的视图层的数据,数据传递给Service层
* 成员位置,创建service对象
*
*
*/
public class ZhangWuController {
private ZhangWuService service = new ZhangWuService();
/**
* 定义方法,实现条件查询
* 调用service层的条件查询方法
* 返回查询到的list集合
*/
public List<ZhangWu> select(String startDate, String endDate) {
return service.select(startDate,endDate);
}
/**
* 删除账务的方法
*/
public int deleteZhangWu(int zwid) {
return service.deleteZhangWu(zwid);
}
/**
* 编辑账务的方法
*
*/
public int editZhangWu(ZhangWu zhangWu) {
return service.editZhangWu(zhangWu);
}
/**
* 添加账务的方法
*/
public int addZhangWu(ZhangWu zhangWu) {
return service.addZhangWu(zhangWu);
}
/**
* 控制层定义方法,实现查询所有的账务数据
* 方法由视图层调用,方法调用service层
*/
public List<ZhangWu> selectAll() {
return service.selectAll();
}
}
9.程序的主入口方法类
package cn.itcast.gjp.app;
import cn.itcast.gjp.view.MainView;
/**
* 主方法类
* @author Administrator
*
*/
public class MainApp {
public static void main(String[] args) {
new MainView().run();
}
}
具体的操作