1)开发工具:eclipse
2)第三方jar包
3)数据表创建
# 新建数据库
create database jzb;
# 新建数据表
create table jzb_zhangwu(
jid int primary key auto_increment,
jname varchar(200),
jmoney double,
jzhanghu varchar(100),
jcreatetime date,
jdescription varchar(1000)
) default charset=utf8;
# 添加初识数据
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (1,'吃饭支出',247,'交通银行','2016-03-02','家庭聚餐');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (2,'工资收入',12345,'现金','2016-03-15','开工资了');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (3,'服装支出',1998,'现金','2016-04-02','买衣服');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (4,'吃饭支出',325,'现金','2016-06-18','朋友聚餐');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (5,'股票收入',8000,'工商银行','2016-10-28','股票大涨');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (6,'股票收入',5000,'工商银行','2016-10-28','股票又大涨');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (7,'工资收入',5000,'交通银行','2016-10-28','又开工资了');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (8,'礼金支出',5000,'现金','2016-10-28','朋友结婚');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (9,'其他支出',1560,'现金','2016-10-29','丢钱了');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (10,'交通支出',2300,'交通银行','2016-10-29','油价还在涨啊');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (11,'吃饭支出',1000,'工商银行','2016-10-29','又吃饭');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (12,'工资收入',1000,'现金','2016-10-30','开资');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (13,'交通支出',2000,'现金','2016-10-30','机票好贵');
INSERT INTO jzb_zhangwu(jid,jname,jmoney,jzhanghu,jcreatetime,jdescription) VALUES (14,'工资收入',5000,'现金','2016-10-30','又开资');
4)项目分层(分包)
- view:视图层->软件的界面
- controller:控制层->获取界面数据,为界面设置数据,将功能交给业务层。
- service:业务层->实现具体功能,与控制层和数据访问层交互。
- dao:数据访问层->操作数据库
- db:数据库->MySQL等
- domain:实体包->存放JavaBean
- tools:工具包->存放项目中使用到的工具类。
- test:测试包->存放项目功能测试的代码。
package com.jzb.app;
import com.jzb.view.MainView;
/*
* 主方法类
*/
public class Mainapp {
public static void main(String[] args) {
new MainView().run();
}
}
package com.jzb.domain;
/*
* 财务类
*/
public class ZhangWu {
private int jid;
private String jname;
private String jzhanghu;
private double jmoney;
private String jcreatetime;
private String jdescription;
public ZhangWu() {
super();
}
public int getJid() {
return jid;
}
public void setJid(int jid) {
this.jid = jid;
}
public String getJname() {
return jname;
}
public void setJname(String jname) {
this.jname = jname;
}
public String getJzhanghu() {
return jzhanghu;
}
public void setJzhanghu(String jzhanghu) {
this.jzhanghu = jzhanghu;
}
public double getJmoney() {
return jmoney;
}
public void setJmoney(double jmoney) {
this.jmoney = jmoney;
}
public String getJcreatetime() {
return jcreatetime;
}
public void setJcreatetime(String jcreatetime) {
this.jcreatetime = jcreatetime;
}
public String getJdescription() {
return jdescription;
}
public void setJdescription(String jdescription) {
this.jdescription = jdescription;
}
}
package com.jzb.view;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
import com.jzb.domain.ZhangWu;
import com.jzb.service.ZhangWuService;
/*
* 视图层
*/
public class MainView {
// view 依赖 service
private ZhangWuService zWuService = new ZhangWuService();
public void run() {
/*
* 1 打印菜单 2 获取用户输入 3 调用对应方法
*/
boolean flag = true;
Scanner inScanner = new Scanner(System.in);
while (flag) {
System.out.println("--------------- 记账本 ---------------");
System.out.println("1.添加账务 2.编辑账务 3.删除账务 4.查询账务 5.退出系统");
System.out.println("请输入功能序号【1-5】:");
int op = inScanner.nextInt();
switch (op) {
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("输入错误!");
}
}
}
private void deleteZhangWu() {
/*
* 1 用户输入,封装到bean中 2 调用service中方法完成功能
*/
Scanner inScanner = new Scanner(System.in);
System.out.println("请输入ID:");
zWuService.deleteZhangWu(inScanner.nextInt());
System.out.println("删除账务成功!");
}
private void editZhangWu() {
/*
* 1 用户输入,封装到bean中 2 调用service中方法完成功能
*/
Scanner inScanner = new Scanner(System.in);
ZhangWu zw = new ZhangWu();
System.out.println("请输入ID:");
zw.setJid(inScanner.nextInt());
System.out.println("请输入新类别:");
zw.setJname(inScanner.next());
System.out.println("请输入新账户:");
zw.setJzhanghu(inScanner.next());
System.out.println("请输入新金额:");
zw.setJmoney(inScanner.nextDouble());
System.out.println("请输入新时间:");
zw.setJcreatetime(inScanner.next());
System.out.println("请输入新备注:");
zw.setJdescription(inScanner.next());
zWuService.editZhangWu(zw);
System.out.println("编辑账务成功!");
}
private void addZhangWu() {
/*
* 1 用户输入数据,封装进bean中 2 调用service的方法完成添加功能
*/
Scanner inScanner = new Scanner(System.in);
ZhangWu zw = new ZhangWu();
System.out.println("请输入类别:");
zw.setJname(inScanner.next());
System.out.println("请输入账户:");
zw.setJzhanghu(inScanner.next());
System.out.println("请输入金额:");
zw.setJmoney(inScanner.nextDouble());
System.out.println("请输入时间:");
zw.setJcreatetime(inScanner.next());
System.out.println("请输入备注:");
zw.setJdescription(inScanner.next());
zWuService.addZhangWu(zw);
System.out.println("添加账务成功!");
}
public void selectZhangWu() {
System.out.println("1.查询所有 2.按条件查询");
Scanner inScanner = new Scanner(System.in);
int op = inScanner.nextInt();
switch (op) {
case 1:
selectAll();
break;
case 2:
select();
break;
default:
System.out.println("输入错误!");
}
}
private void select() {
/*
* 1 用户输入查询的日期范围 2 调用service的select()方法完成查询功能
*/
Scanner inScanner = new Scanner(System.in);
System.out.println("请输入查询起始时间:");
String start = inScanner.next();
System.out.println("请输入查询结束时间:");
String end = inScanner.next();
List zwList = zWuService.select(start, end);
print(zwList);
}
private void selectAll() {
List zwList = zWuService.selectAll();
print(zwList);
}
private void print(List zwList) {
System.out.println("ID\t类别\t\t账户\t\t金额\t\t时间\t\t备注");
for (ZhangWu zWu : zwList) {
System.out.println(zWu.getJid() + "\t" + zWu.getJname() + "\t" + zWu.getJzhanghu() + "\t" + zWu.getJmoney()
+ "\t" + zWu.getJcreatetime() + "\t" + zWu.getJdescription());
}
}
}
package com.jzb.service;
import java.util.List;
import com.jzb.dao.ZhangWuDao;
import com.jzb.domain.ZhangWu;
/*
* 业务层
*/
public class ZhangWuService {
// service 依赖 dao
private ZhangWuDao zWuDao=new ZhangWuDao();
public List selectAll() {
return zWuDao.selectAll();
}
public List select(String start, String end) {
return zWuDao.select(start,end);
}
public void addZhangWu(ZhangWu zw) {
zWuDao.addZhangWu(zw);
}
public void editZhangWu(ZhangWu zw) {
zWuDao.editZhangWu(zw);
}
public void deleteZhangWu(int nextInt) {
zWuDao.deleteZhangWu(nextInt);
}
}
package com.jzb.tools;
import org.apache.commons.dbcp2.BasicDataSource;
public class JDBCUtils {
public static final String DRIVERNAME = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/my_db";
public static final String USER = "root";
public static final String PASSWORD = "rootroot";
// 1 创建连接池 BasicDataSource
public static BasicDataSource dataSource=new BasicDataSource();
// 2 静态代码块
static {
// 对连接池对象进行进本的配置
dataSource.setDriverClassName(DRIVERNAME);
dataSource.setUrl(URL);
dataSource.setUsername(USER);
dataSource.setPassword(PASSWORD);
}
// 3 返回连接池对象
public static javax.sql.DataSource getDataSource() {
return dataSource;
}
}
package com.jzb.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.jzb.domain.ZhangWu;
import com.jzb.tools.JDBCUtils;
/*
* 数据访问
*/
public class ZhangWuDao {
// 获取数据库连接池,得到操作表数据的对象
QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
/*
* 查询所有账务
*/
public List selectAll() {
String sql = "select * from jzb_zhangwu";
try {
return queryRunner.query(sql, new BeanListHandler(ZhangWu.class));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public List select(String start, String end) {
String sql = "select * from jzb_zhangwu where jcreatetime between ? and ?";
Object[] params = { start, end };
try {
return queryRunner.query(sql, new BeanListHandler(ZhangWu.class),params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public void addZhangWu(ZhangWu zw) {
String sql = "insert into jzb_zhangwu(jname,jmoney,jzhanghu,jcreatetime,jdescription) values(?,?,?,?,?)";
Object[] params = { zw.getJname(), zw.getJmoney(), zw.getJzhanghu(), zw.getJcreatetime(),
zw.getJdescription() };
try {
queryRunner.update(sql, params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void editZhangWu(ZhangWu zw) {
String sql = "update jzb_zhangwu set jname=?,jmoney=?,jzhanghu=?,jcreatetime=?,jdescription=? where jid=?";
Object[] params = { zw.getJname(), zw.getJmoney(), zw.getJzhanghu(), zw.getJcreatetime(), zw.getJdescription(),
zw.getJid() };
try {
queryRunner.update(sql, params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteZhangWu(int nextInt) {
String sql="delete from jzb_zhangwu where jid=?";
try {
queryRunner.update(sql, nextInt);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}