使用JDBC技术完成一个简单的账务管理系统
生活随笔
收集整理的這篇文章主要介紹了
使用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 {// 拼寫添加數據的sqlString sql = "INSERT INTO gip_Accounting (gacname,gamoney,gaaccount,"+ "gacreatetime,gadescription) VALUES(?,?,?,?,?)";// 創建對象數組,處處5個占位符的實際參數// 實際參數來源是傳遞過來的對象AccountingObject[] 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方法 結果集 BeanListHandlerList<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()*/@Overrideprotected Object clone() throws CloneNotSupportedException {// TODO 自動生成的方法存根return super.clone();}/** (非 Javadoc)* * @see java.lang.Object#finalize()*/@Overrideprotected void finalize() throws Throwable {// TODO 自動生成的方法存根super.finalize();}/** (非 Javadoc)* * @see java.lang.Object#toString()*/@Overridepublic 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()*/@Overridepublic 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)*/@Overridepublic 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("沒有查到符合條件的數據");}}}總結
以上是生活随笔為你收集整理的使用JDBC技术完成一个简单的账务管理系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小程序生成图片保存到系统相册
- 下一篇: NAT技术之NAT server