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的更多相关文章

  1. 家庭记账本之Github账号注册与安装(二)

    好多程序猿都在使用github用来存放自己的代码:但是如果仅仅用github的web版本的话:有一些功能还是需要使用git客户端工具才能操作的: 那么今天将指导大家如何安装GitHub for win ...

  2. 家庭记账本之GitHub账号注册与安装(一)

    账号注册 1.github是世纪上最大的开源代码托管网站.因为是国外网站,很多人在注册的时候因为不熟悉英语而犯了难. 2.百度搜索github进入官网.如果你已经有账号密码,那么点击右上角的sign ...

  3. 安卓开发实战-记账本APP(六)

    记账本APP开发---终结篇 昨天的动态数字录屏奉上:在抖音上拍了一个(ps:欢迎点赞) https://v.douyin.com/poEjmG/ 今天将图表的内容进行了制作,我用的是MPChart的 ...

  4. 家庭版记账本app开发完成

    经过这几天关于android的相关学习,对于家庭版记账本app以及开发结束. 实现的功能为:用户的注册.登录.添加支出账单.添加收入账单.显示所有的该用户的账单情况(收入和支出).生产图表(直观的显示 ...

  5. 记账本NABCD分析

    学生记账本NABCD分析 N(Need,需求) 随着我们进入大学开始逐步的扩大自己的消费水平,而我们每天无法准确的记住一笔一笔的消费记录.常常,每一个月末时我们在宿舍楼道听到不少学生抱怨这个月怎么花钱 ...

  6. 记账本APP(2)

    今天下载了Hbuiler,生成了一个记账本APP,目前里面只可以 输入今日消费 明天将会做出来记录以及计算总额于月消费.

  7. 简单记账本APP开发一

    在对Android的一些基础的知识有了一定了解,以及对于AndroidStudio的如何使用有了 一定的熟悉后,决定做一个简单的记账本APP 开发流程 1.记账本的页面 2.可以添加新的账目 (一)页 ...

  8. Android开发实战——记账本(2)

    开发日志(2)——Bean目录以及数据库 首先编写一些自己生成的数据进行测试,看一下能否显示在模拟器上.那前提就是先写出bean目录,这和之前学的Javaweb步骤差不多.bean目录有三个变量事件. ...

  9. 进度1_家庭记账本App

    今天完成了昨天的初步构想,详细介绍见上一篇博客,具体项目结构和案例如下: MainActivity.java: package com.example.familybooks; import andr ...

随机推荐

  1. vue.js--遇到的一些错误

    1. <sapn> - did you register the component correctly? For recursive components, make sure to p ...

  2. latex之矩阵表示

    $ \begin{matrix} 1 & 0 & 0\\ 0 & 1 & 0\\ 0 & 0 & 1 \end{matrix}\quad \begin{ ...

  3. arm-linux-ld:u-boot.lds:1: ignoring invalid character `#' in expression

    在裁剪uboot的时候出现下面错误: LDS u-boot.lds LD u-boot arm-linux-: ignoring invalid character `#' in expression ...

  4. 【BZOJ2555】SubString

    算是学会sam了吧…… 原题: 懒得写背景了,给你一个字符串init,要求你支持两个操作        (1):在当前字符串的后面插入一个字符串        (2):询问字符串s在当前字符串中出现了 ...

  5. java_注解

    注解1    注解的概念    jdk自带的注解    声明与使用注解的基本语法        注解的概念        在javaEE与开源框架中广泛使用,泛型在集合框架中广泛使用        注 ...

  6. Dynamics 365 CRM 添加自定义按钮

    在添加自定义按钮之前,我们需要下载这个工具 RibbonWorkbench, 它是专门针对自定义命令栏和Ribbon区域. 下载之后是一个zip压缩包. 怎样安装RibbonWorkbench: Se ...

  7. 对数据进行GZIP压缩或解压缩

    /** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...

  8. EXCEL统计不重复值的数量

    如这一列中,有多少不重复值? 1.可以点击,数据,删除重复项,清除重复值,然后剩下的统计一下即可知道:       ===> 2.用公式:=SUMPRODUCT((MATCH(E3:E20,E3 ...

  9. 算法实践--最小生成树(Prim算法)

    前一篇介绍了一种最小生成树的算法--Kruskal算法,本篇介绍另一种Prim算法 算法描述 定义V为端点的集合,A为最小生成树,初始为空.对于每个端点v初始的Key[v]=∞, Parent[v]= ...

  10. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...