记账本,C,Github,Dao
package dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List; import entity.Category;
import util.DBUtil; /**
* catebory表的ORM映射
*
* @author 于修彦
*
*/
public class CategoryDAO { public int getTotal() {
int total = 0;
try (Connection c = DBUtil.getConn(); Statement s = c.createStatement();) { String sql = "select count(*) from category"; ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
} } catch (SQLException e) { e.printStackTrace();
}
return total;
} public void add(Category category) { String sql = "insert into category(name) values(?)";
try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setString(1, category.getName()); ps.execute(); ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
category.setId(rs.getInt(1));
}
} catch (SQLException e) { e.printStackTrace();
}
} public void update(Category category) { String sql = "update category set name= ? where id = ?";
try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setString(1, category.getName());
ps.setInt(2, category.getId()); ps.execute(); } catch (SQLException e) { e.printStackTrace();
} } public void delete(int id) { try (Connection c = DBUtil.getConn(); Statement s = c.createStatement();) { String sql = "delete from category where id = " + id; s.execute(sql); } catch (SQLException e) { e.printStackTrace();
}
} public Category get(int id) {
Category category = null; try (Connection c = DBUtil.getConn(); Statement s = c.createStatement();) { String sql = "select * from category where id = " + id; ResultSet rs = s.executeQuery(sql); if (rs.next()) {
category = new Category();
String name = rs.getString(2);
category.setName(name);
category.setId(id);
} } catch (SQLException e) { e.printStackTrace();
}
return category;
} public List<Category> list() {
return list(0, Short.MAX_VALUE);
} public List<Category> list(int start, int count) {
List<Category> categorys = new ArrayList<Category>(); String sql = "select * from category order by id desc limit ?,? "; try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, start);
ps.setInt(2, count); ResultSet rs = ps.executeQuery(); while (rs.next()) {
Category category = new Category();
int id = rs.getInt(1);
String name = rs.getString(2);
category.setId(id);
category.setName(name);
categorys.add(category);
}
} catch (SQLException e) { e.printStackTrace();
}
return categorys;
} }
dao1
package dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import entity.Config;
import util.DBUtil; /**
* 专门用于把Config实例与Config表进行ORM映射
*
* @author 于修彦
*
*/
public class ConfigDAO {
/**
* 获取总数
*
* @return
*/
public int getTotal() {
int total = 0;
String sql = "select count(*) from config";
try (Connection conn = DBUtil.getConn(); PreparedStatement ps = conn.prepareStatement(sql);) {
ResultSet rs = ps.executeQuery();
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return total;
} /**
* 添加配置
*
* @param config
* 配置信息
* @return 成功返回true,失败返回false
*/
public boolean add(Config config) {
String sql = "insert into config(myKey,myValue) values(?,?)";
boolean flag = false;
try (Connection conn = DBUtil.getConn(); PreparedStatement ps = conn.prepareStatement(sql);) {
ps.setString(1, config.getMyKey());
ps.setString(2, config.getMyValue());
flag = ps.execute();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
config.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
} /**
* 删除配置信息
*
* @param config
* 配置
* @return 成功返回true,失败返回false
*/
public boolean delete(Config config) {
String sql = "delete from config where id = ?";
boolean flag = false;
try (Connection conn = DBUtil.getConn(); PreparedStatement ps = conn.prepareStatement(sql);) {
ps.setInt(1, config.getId());
flag = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
} /**
* 更新配置信息
*
* @param config
* 配置信息
* @return 成功返回true,失败返回false
*/
public boolean update(Config config) {
boolean flag = false;
String sql = "update config set myKey=?,myValue=? where id=?";
Connection conn = DBUtil.getConn();
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, config.getMyKey());
ps.setString(2, config.getMyValue());
ps.setInt(3, config.getId());
flag = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
} /**
* 通过id获取config实例
*
* @param id
* @return config实例
*/
public Config get(int id) {
Config config = null;
String sql = "select * from config where id=?"; try (Connection conn = DBUtil.getConn(); PreparedStatement ps = conn.prepareStatement(sql);) {
ps.setInt(1, id);
ResultSet rs = ps.executeQuery(); if (rs.next()) {
config = new Config();
config.setId(id);
config.setMyKey(rs.getString("myKey"));
config.setMyValue(rs.getString("myValue"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return config;
} /**
* 用于分页查询
*
* @param start
* 开始id
* @param count
* 每页条数
* @return config的列表
*/
public List<Config> list(int start, int count) {
List<Config> configs = new ArrayList<Config>();
String sql = "select * from config order by id desc limit ?,?"; try (Connection conn = DBUtil.getConn(); PreparedStatement ps = conn.prepareStatement(sql);) { ps.setInt(1, start);
ps.setInt(2, count);
ResultSet rs = ps.executeQuery(); while (rs.next()) {
Config config = new Config();
config.setId(rs.getInt("id"));
config.setMyKey(rs.getString("myKey"));
config.setMyValue(rs.getString("myValue")); configs.add(config);
}
} catch (SQLException e) {
e.printStackTrace();
}
return configs;
} /**
* 获取
*
* @return
*/
public List<Config> list() {
return list(0,Short.MAX_VALUE);
} /**
* 通过键获取Config实例,比如预算对应的Config实例,就会通过这种方式获取: new
* ConfigDAO().getByKey("budget");
*
* @param key
* @return config实例
*/
public Config getByKey(String key) {
Config config = null;
String sql = "select * from config where myKey=?"; try (Connection conn = DBUtil.getConn(); PreparedStatement ps = conn.prepareStatement(sql);) {
ps.setString(1, key);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
config = new Config();
config.setId(rs.getInt("id"));
config.setMyKey(key);
config.setMyValue(rs.getString("myValue"));
}
} catch (SQLException e) {
e.printStackTrace();
} return config;
} }
dao2
package dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import entity.Record;
import util.DBUtil;
import util.DateUtil; /**
* record表的映射
*
* @author 于修彦
*
*/
public class RecordDAO {
/**
* 获取总数
*
* @return 记录条数
*/
public int getTotal() {
int total = 0;
try (Connection c = DBUtil.getConn(); Statement s = c.createStatement();) { String sql = "select count(*) from record"; ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
total = rs.getInt(1);
}
} catch (SQLException e) { e.printStackTrace();
}
return total;
} /**
* 增加记录
*
* @param record
*/
public void add(Record record) { String sql = "insert into record values(null,?,?,?,?)";
try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setInt(1, record.getSpend());
ps.setInt(2, record.getCid());
ps.setString(3, record.getComment());
ps.setDate(4, DateUtil.util2sql(record.getMyDate())); ps.execute(); ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
int id = rs.getInt(1);
record.setId(id);
}
} catch (SQLException e) { e.printStackTrace();
}
} /**
* 更新记录
*
* @param record
*/
public void update(Record record) { String sql = "update record set spend= ?, cid= ?, comment =?, myDate = ? where id = ?";
try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, record.getSpend());
ps.setInt(2, record.getCid());
ps.setString(3, record.getComment());
ps.setDate(4, DateUtil.util2sql(record.getMyDate()));
ps.setInt(5, record.getId()); ps.execute(); } catch (SQLException e) { e.printStackTrace();
} } /**
* 删除记录
*
* @param id
*/
public void delete(int id) { try (Connection c = DBUtil.getConn(); Statement s = c.createStatement();) { String sql = "delete from record where id = " + id; s.execute(sql); } catch (SQLException e) { e.printStackTrace();
}
} /**
* 根据id获取记录
*
* @param id
* @return record实例
*/
public Record get(int id) {
Record record = null; try (Connection c = DBUtil.getConn(); Statement s = c.createStatement();) { String sql = "select * from record where id = " + id; ResultSet rs = s.executeQuery(sql); if (rs.next()) {
record = new Record();
int spend = rs.getInt("spend");
int cid = rs.getInt("cid");
String comment = rs.getString("comment");
Date date = rs.getDate("myDate"); record.setSpend(spend);
record.setCid(cid);
record.setComment(comment);
record.setMyDate(date);
record.setId(id);
} } catch (SQLException e) { e.printStackTrace();
}
return record;
} /**
* 获取所有记录
*
* @return
*/
public List<Record> list() {
return list(0, Short.MAX_VALUE);
} /**
* 用于分页查询
*
* @param start
* @param count
* @return
*/
public List<Record> list(int start, int count) {
List<Record> records = new ArrayList<Record>(); String sql = "select * from record order by id desc limit ?,? "; try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, start);
ps.setInt(2, count); ResultSet rs = ps.executeQuery(); while (rs.next()) {
Record record = new Record();
int id = rs.getInt("id");
int spend = rs.getInt("spend");
int cid = rs.getInt("cid"); String comment = rs.getString("comment");
Date date = rs.getDate("myDate"); record.setSpend(spend);
record.setCid(cid);
record.setComment(comment);
record.setMyDate(date);
record.setId(id);
records.add(record);
}
} catch (SQLException e) { e.printStackTrace();
}
return records;
} /**
* 获取某一项分类的记录列表
*
* @param cid
* @return
*/
public List<Record> list(int cid) {
List<Record> records = new ArrayList<Record>(); String sql = "select * from record where cid = ?"; try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) { ps.setInt(1, cid); ResultSet rs = ps.executeQuery(); while (rs.next()) {
Record record = new Record();
int id = rs.getInt("id");
int spend = rs.getInt("spend"); String comment = rs.getString("comment");
Date date = rs.getDate("myDate"); record.setSpend(spend);
record.setCid(cid);
record.setComment(comment);
record.setMyDate(date);
record.setId(id);
records.add(record);
}
} catch (SQLException e) { e.printStackTrace();
}
return records;
} /**
* 获取今天的消费记录列表
*
* @return
*/
public List<Record> listToday() {
return list(DateUtil.getToday());
} /**
* 根据日期获取某一天的消费记录列表
*
* @param day
* @return
*/
public List<Record> list(Date day) {
List<Record> records = new ArrayList<Record>();
String sql = "select * from record where myDate =?";
try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setDate(1, DateUtil.util2sql(day)); ResultSet rs = ps.executeQuery();
while (rs.next()) {
Record record = new Record();
int id = rs.getInt("id");
int cid = rs.getInt("cid");
int spend = rs.getInt("spend"); String comment = rs.getString("comment");
Date date = rs.getDate("myDate"); record.setSpend(spend);
record.setCid(cid);
record.setComment(comment);
record.setMyDate(date);
record.setId(id);
records.add(record);
}
} catch (SQLException e) { e.printStackTrace();
}
return records;
} /**
* 获取本月份消费记录
*
* @return
*/
public List<Record> listThisMonth() {
return list(DateUtil.getMonthBegin(), DateUtil.getMonthEnd());
} /**
* 获取从开始日期到结束日期的消费记录
*
* @param start
* @param end
* @return
*/
public List<Record> list(Date start, Date end) {
List<Record> records = new ArrayList<Record>();
String sql = "select * from record where myDate >=? and myDate <= ?";
try (Connection c = DBUtil.getConn(); PreparedStatement ps = c.prepareStatement(sql);) {
ps.setDate(1, DateUtil.util2sql(start));
ps.setDate(2, DateUtil.util2sql(end));
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Record record = new Record();
int id = rs.getInt("id");
int cid = rs.getInt("cid");
int spend = rs.getInt("spend"); String comment = rs.getString("comment");
Date date = rs.getDate("myDate"); record.setSpend(spend);
record.setCid(cid);
record.setComment(comment);
record.setMyDate(date);
record.setId(id);
records.add(record);
}
} catch (SQLException e) { e.printStackTrace();
}
return records;
} }
dao3
记账本,C,Github,Dao的更多相关文章
- 家庭记账本之Github账号注册与安装(二)
好多程序猿都在使用github用来存放自己的代码:但是如果仅仅用github的web版本的话:有一些功能还是需要使用git客户端工具才能操作的: 那么今天将指导大家如何安装GitHub for win ...
- 家庭记账本之GitHub账号注册与安装(一)
账号注册 1.github是世纪上最大的开源代码托管网站.因为是国外网站,很多人在注册的时候因为不熟悉英语而犯了难. 2.百度搜索github进入官网.如果你已经有账号密码,那么点击右上角的sign ...
- 安卓开发实战-记账本APP(六)
记账本APP开发---终结篇 昨天的动态数字录屏奉上:在抖音上拍了一个(ps:欢迎点赞) https://v.douyin.com/poEjmG/ 今天将图表的内容进行了制作,我用的是MPChart的 ...
- 家庭版记账本app开发完成
经过这几天关于android的相关学习,对于家庭版记账本app以及开发结束. 实现的功能为:用户的注册.登录.添加支出账单.添加收入账单.显示所有的该用户的账单情况(收入和支出).生产图表(直观的显示 ...
- 记账本NABCD分析
学生记账本NABCD分析 N(Need,需求) 随着我们进入大学开始逐步的扩大自己的消费水平,而我们每天无法准确的记住一笔一笔的消费记录.常常,每一个月末时我们在宿舍楼道听到不少学生抱怨这个月怎么花钱 ...
- 记账本APP(2)
今天下载了Hbuiler,生成了一个记账本APP,目前里面只可以 输入今日消费 明天将会做出来记录以及计算总额于月消费.
- 简单记账本APP开发一
在对Android的一些基础的知识有了一定了解,以及对于AndroidStudio的如何使用有了 一定的熟悉后,决定做一个简单的记账本APP 开发流程 1.记账本的页面 2.可以添加新的账目 (一)页 ...
- Android开发实战——记账本(2)
开发日志(2)——Bean目录以及数据库 首先编写一些自己生成的数据进行测试,看一下能否显示在模拟器上.那前提就是先写出bean目录,这和之前学的Javaweb步骤差不多.bean目录有三个变量事件. ...
- 进度1_家庭记账本App
今天完成了昨天的初步构想,详细介绍见上一篇博客,具体项目结构和案例如下: MainActivity.java: package com.example.familybooks; import andr ...
随机推荐
- Linux命令行下:把程序放后台执行,以及从后台继续执行程序
把任务放到后台用 & 和 Ctrl+z 让后台任务从停止状态转为运行状态用 bg %N 把后台任务调回到前台用 fg %N 查看所有任务用jobs
- python学习之路03
一.常量和变量 1.python中的数据类型 分类: Number:数字型[整型,浮点型,复数] String:字符串型 Boolean:布尔型[True,False] None:空值 ...
- mysql命令行使用
连接数据库 mysql -P 端口号 -h 远程机地址/ip -u 用户名 -p mysql -uroot -p123456 修改数据库密码 mysqladmin -uroot -p123456 ...
- WebSocket是什么原理?为什么可以实现持久连接?
作者:Ovear 链接:https://www.zhihu.com/question/20215561/answer/40316953来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- ubuntu16.04下怎么关闭x server
按住ctrl+alt+f1,进入命令行. 输入sudo /etc/init.d/lightdm stopsudo /etc/init.d/lightdm status 重启xserver?输入sudo ...
- Custom Grid Columns - FireMonkey Guide
原文 http://monkeystyler.com/guide/Custom-Grid-Columns ack to FireMonkey Topics As we saw in TGrid a F ...
- 格式化输出=========》format 和 %
str.format() 实现格式化输出的功能 s1 = "i am {0},gae{1}".format("alex",18) 普通版,直接输入元祖 ...
- Scrapy学习篇(十一)之设置随机User-Agent
大多数情况下,网站都会根据我们的请求头信息来区分你是不是一个爬虫程序,如果一旦识别出这是一个爬虫程序,很容易就会拒绝我们的请求,因此我们需要给我们的爬虫手动添加请求头信息,来模拟浏览器的行为,但是当我 ...
- git一个分布式版本工具的使用
1.git和cvs的区别 分支更快,更容易 支持离线工作,本地提交可以稍后提交到服务器上 git提交是原子的,且是整个项目范围的,而不像cvs是对每个文件 git中的每个工作树都包含一个具有完整项目历 ...
- IIS小知识
1.IIS的默认配置路径 IIS5.0默认配置文件路径C:\WINNT\system32\inetsrv\MetaBase.binIIS6.0默认配置文件路径C:\WINDOWS\system32\i ...