In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to use Java to make accounting software. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
1. Project introduction 1.1. Project schedule 1.1.1. Project goal
This project is a comprehensive project of JAVAEE basic class, which includes several knowledge points, which achieves the comprehensive use of the knowledge learned in the basic class, and improves our understanding of the project and the application of knowledge points.
Proficient in View layer, Service layer, Dao layer method mutual transfer operation, proficient dbutils operation database table to complete additions, deletions and changes through this project, let us understand the company's project development process, fully grasp the project requirements analysis, design and functional code implementation. Improve students' ability to analyze needs and function realization independently.
1.1.2. Project time
This project is completed through a 1-day course.
1.2. Project function analysis 1.2.1. Project demonstration 1.2.2. Introduction of project function
L query accounts
L query accounts by combination of multiple conditions
L add accounts
L Editing accounts
L delete accounts
two。 The project environment is set up 2.1. Technology selection and introduction of jar package
Each project uses mature technologies, usually open source free technologies provided by professional organizations or teams. In the future learning process, we will gradually understand these professional organizations. The technologies used in this project are as follows: l apache's commons component: commons-dbutils-1.4.jar: encapsulates and simplifies the database connection pool component provided by JDBC; ®commons-dbcp-1.4.jar:apache commons, which is named DBCP; ®commons.pool-1.3.jar:DBCP connection pool depends on the jar package; l mysql-connector-java-5.1.28-bin.jar:MySQL 's JDBC driver package, the JAR package must be used to connect to the MySQL database with JDBC.
2.2. Introduction to tool class
Some tool classes are used for each project, and this project is no exception. We will not teach you how to implement these classes, but to let you know how we use these tool classes in the project, the following is the tool class used in this project and the introduction: l JDBCUtils: used to create database connection pool objects; in the implementation process of the project, we will explain the use of tool classes, which will not be introduced too much here.
JDBCUtils.javapublic class JDBCUtils {public static final StringDRIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; public static final StringURL = "jdbc:mysql://localhost:3306/gjp"; public static final StringUSERNAME = "root"; public static final StringPASSWORD = "root"; private static BasicDataSourcedataSource = new BasicDataSource (); static {dataSource.setDriverClassName (DRIVER_CLASS_NAME); dataSource.setUrl (URL); dataSource.setUsername (USERNAME); dataSource.setPassword (PASSWORD);} public static DataSource getDataSource () {return dataSource;}} 2.3. Datasheet creation
Table design is very important for a project, because all operations in the application are based on database tables, so our first step is to create database tables. The database design of the housekeeper project is very simple, we just need to find the gjp.sql file and execute it. The following is the SQL statement to create the library and table:
2.3.1. Create a database
Create a housekeeper database gjpCREATE DATABASE gjp
2.3.2. Create a database table
Create an account table gjp_zhangwu
CREATE TABLE gjp_zhangwu (zwid INT PRIMARY KEY AUTO_INCREMENT, flname VARCHAR, money DOUBLE, zhangHu VARCHAR, createtime DATE, description VARCHAR (1000); 2.3.3. Add table data
Add account table data
INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (1m 'food expenses', 247 'Bank of Communications', '2016-03-02' 'zwid,flname,money,zhangHu,createtime,description' family dinner'); INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (2 'salary', '12345' cash', '2016-03-15' salary') INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (3 INSERT INTO gjp_zhangwu 'clothing expenses', 1998 INSERT INTO gjp_zhangwu 'cash', '1998 04-02' clothing 'buying clothes'); INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (4 'food expenses', 325 'cash', '2016-06-18' Girl 'friends' dinner') INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (5 minus' stock earnings', 8000 INSERT INTO gjp_zhangwu 'ICBC', '2016-10-28' stocks soar'); INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (6 'stock earnings', 5000 mins' ICBC', '2016-10-28' stocks soar again') INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (7 'salary income', 5 2016 'Bank of Communications','50-10-28 'salary'); INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (8 'gift payment', 5 000 'cash', '2016-10-28' friend marriage') INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (9 'other expenses', 1560 'cash', '2016-10-29' cash'); INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (10 'traffic expenses', 2300 'Bank of Communications', '2016-10-29' fuel prices are still rising') INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (1114 'meal expenses', 1000 'Industrial and Commercial Bank', '2016-10-29' INSERT INTO gjp_zhangwu 'eat again'); INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (12 'wages', 1000 'cash', '2016-10-30' cash') INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (13 'transportation expenses', 2000 'cash', '2016-10-30' ticket 'expensive'), INSERT INTO gjp_zhangwu (zwid,flname,money,zhangHu,createtime,description) VALUES (14 'salary', 5000 'cash', '2016-10-30' cash') The role of project layering (subcontracting)
Why should the program be subcontracted and layered?
A case study of customers going to a restaurant for dinner:
Small restaurant: a waiter takes care of it (receives customers to order and stir-fry)
Grand Hotel:
L usher (whether there is a reservation to ask for Chinese food, western food or barbecue to lead the way to the designated private room to ask the food clerk to order for the customer)
L order clerk (record the contents of customer orders, whether there are taboos, etc., ask the chef to cook for the customer)
L chef (stir-fry according to the customer's dish list)
Through the case, it is found that when the scale of the program is small, it can be completed by one person; but when the scale of the program is large, it is difficult for one person to complete, so it is necessary to use the way of multi-person cooperation to complete the program development.
Multi-person cooperation will encounter the problem of task assignment, and we will think that each person will be responsible for completing a piece of the project. In that case, the division of this piece of content, we need to use a hierarchical (subcontracting) way to complete.
Use the following figure (user registration function) to explain the layering (subcontracting) that is common in the project.
L view layer function: view layer, that is, the interface in the project
The function of the controller layer: the control layer obtains the data on the interface and sets the data for the interface; the functions to be implemented are handed over to the business layer for processing
The function of the service layer: the business layer, the realization of the function, interacts with the controller control layer and the data access layer DAO, and gives the operation to the database to the DAO data access layer to deal with.
L dao layer function: data access layer, which is used to manipulate the data of database tables.
L db database: in this case, MySQL
L domain entity package: stores JavaBean
L tools toolkit: stores the tool classes used in the project
L test test package: the code that stores the functional tests of the project
2.5. Project creation and package management
1. Create a Java project using Eclipse, named gjp
two。 Create a project package
·cn.itcast.gjp.app: stores the main method class
Angular cn.itcast.gjp.domain: store the JavaBean
Angular cn.itcast.gjp.view: storage interface, and presentation layer classes
·cn.itcast.gjp.service: stores the business layer class
Angular cn.itcast.gjp.dao: stores the data access layer class
Angular cn.itcast.gjp.tools: store the utility class
3. Create a lib folder to store the jar packages used
3. Function module 3.1. Related class creation
Complete the creation of the class in this project, there is no need to add code to the class.
1. Copy the written utility class JDBCUtils.java into the tools package
two。 Copy the jar packages mysql-connector-java-5.1.28-bin.jar, commons-dbutils-1.4.jar, commons-dbcp-1.4.jar, commons-pool-1.3.jar to the lib folder, add them to the classPath path through the Build Path operation, and provide them to JDBCUtils for use
3. In the app package, create the class MainApp.java and write the main main method to complete the startup of the project
4. In the domain package, create the class ZhangWu.java, which is the JavaBean used to encapsulate accounting information.
5. In the dao package, create the class ZhangWuDao.java and add a member variable QueryRunner object to the ZhangWuDao class, because we use dbutils to manipulate the database.
6. In the service package, create the class ZhangWuService.java and add a member variable of type ZhangWuDao to the ZhangWuService class, because service depends on dao.
7. In the view package, create the class MainView.java and add a member variable of type ZhangWuService to the MainView class, because view depends on service in this project.
Write MainApp.java in app package
/ * * main method class * / public class MainApp {public static void main (String [] args) {}}
Write ZhangWu.java in domain package
/ * account class * / public class ZhangWu {}
Write ZhangWuDao.java in Dao package
/ * Accounting data layer class * / public class ZhangWuDao {/ / get the database connection pool and get the object QueryRunnerQueryRunner qr = new QueryRunner (JDBCUtils.getDataSource ()) that manipulates the table data;}
Write Service package, ZhangWuService.java
/ * Accounting business layer class * / public class ZhangWuService {/ / service all depend on daoprivate ZhangWuDao zhangWuDao = new ZhangWuDao ();}
Write view packages
MainView.javapublic class MainView {/ / view in this project depends on serviceprivate ZhangWuServicezhangWuService = new ZhangWuService ();} 3.2. Accounting JavaBean
JavaBean refers to the class in Java, in which the member variables correspond to the fields in the database table (the variable name corresponds to the database table field name, and the variable data type corresponds to the database table field type), and provides empty parameter construction methods, set, get methods. Let's now complete the coding of the accounting class, as follows
/ * account class * / public class ZhangWu {private int zwid;// idprivate Stringflname; / / category name private StringzhangHu; / / account name private double money;// amount private String createtime;// creation time private String description;// description public String getFlname () {return flname;} public void setFlname (Stringflname) {this.flname = flname;} public String getZhangHu () {return zhangHu;} public void setZhangHu (StringzhangHu) {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 (Stringcreatetime) {this.createtime = createtime;} public String getDescription () {return description;} public void setDescription (Stringdescription) {this.description = description;}} 3.3. Function interface menu
The completion of the interface menu is the first step in writing the project. Through the output statement, we complete the output of the interface menu, and then call and execute the corresponding function according to the input function serial number.
3.3.1. Function realization steps
1 write the run method of MainView class
1.1 complete the display of the functional interface menu
1.2 functional options for receiving keyboard input
1.3 call the corresponding function method according to the option value
2 write the main main method of the MainApp class
2.1call the run method in the MainView class to execute the program and display the function interface menu.
3.3.2. Function implementation code
1. Write run methods in the MainView class of the view package
3.4. Query all accounts 3.4.1. Function realization steps
1 write the selectZhangWu method in the MainView class
1.1 output statements that show how the accounts are to be queried
1.2 receive the input from the keyboard and call the corresponding method (1. Query all 2. Query by condition)
2 write selectAll query methods for all accounts in the MainView class
2.1Call the selectAll method of the ZhangWuService class to return the List collection containing all the accounting data
2.2 call the print method in the MainView class to display all the accounting data in the console.
3 write the print method in the MainView class
3.1 use the output statement to print out the header name of the account table
3.2 iterate through the account set and output and print each account information
4 write the selectAll method in the ZhangWuService class
4.1 call the selectAll method in the ZhangWuDao class and return the List collection containing all the accounting data
5. Write the selectAll () method in the ZhangWuDao class
5.1 through the QueryRunner object, call the query method to query the database table gjp_zhangwu, and return the List collection containing all the accounting data
3.4.2. Function implementation code
1. Write selectZhangWu methods in the MainView class
/ * query accounting method * / public void selectZhangWu () {System.out.println ("1. Query all 2. Query by condition"); Scanner in = new Scanner (System.in); int op = in.nextInt (); switch (op) {case 1:selectAll (); break;case 2:select (); break;default:System.out.println ("input error!") ;}}
two。 Write selectAll methods in the MainView class
/ * query all accounting methods * / public void selectAll () {List zhangWuList = zhangWuService.selectAll (); print (zhangWuList);}
3. Write print methods in the MainView class
/ * * output account method * / private void print (ListzhangWuList) {System.out.println ("time description of ID account amount") For (ZhangWu zw: zhangWuList) {System.out.println (zw.getZwid () + "+ zw.getFlname () +" + zw.getZhangHu () + "+ zw.getMoney () +" + zw.getCreatetime () + "+ zw.getDescription ());}}
4. Write selectAll methods in the ZhangWuService class
/ * * query all * / public List selectAll () {return zhangWuDao.selectAll ();}
5. Write the selectAll () method in the ZhangWuDao class
/ * query all accounts * / public List selectAll () {String sql = "select * from gjp_zhangwu"; try {return qr.query (sql,new BeanListHandler (ZhangWu.class));} catch (SQLExceptione) {throw new RuntimeException (e);}} 3.5. Multi-condition inquiry account 3.5.1. Functional analysis
1 write the select method in the MainView class
1.1 enter the range of query dates through the keyboard
1.2 call the select method in the ZhangWuSerice class to return the collection of accounting information within the query date range List
1.3 call the print method in the MainView class to display the contents of the accounting information collection in the console
2 write the select method in the ZhangWuService class
2.1Call the select method in the ZhangWuDao class to return the billing information collection List within the query date range
3 write the select method in the ZhangWuDao class
3.1 through the QueryRunner object, call the query method to query the database table gjp_zhangwu, and return the List collection containing the accounting data within the query date range
3.5.2. Function realization steps
1. Write select methods in the MainView class
/ * query account method by condition * / public void select () {/ * * 1. Gets the range of query dates entered by the user. * 2. Call service's select () method to complete conditional query function * / Scanner in = new Scanner (System.in); System.out.print ("Please enter query start time:"); String start = in.next (); System.out.print ("Please enter query end time:"); String end = in.next (); List zhangWuList = zhangWuService.select (start, end); print (zhangWuList);}
two。 Write select methods in the ZhangWuService class
/ * * query accounting method by condition * @ param start * @ param end * @ return * / public List select (Stringstart, String end) {return zhangWuDao.select (start,end);}
3. Write select methods in the ZhangWuDao class
/ * query accounting method by condition * @ param start * @ param end * @ return * / public List select (Stringstart, String end) {String sql = "select * from gjp_zhangwu where createtime between? and?"; Object [] params = {start,end}; try {return qr.query (sql,new BeanListHandler (ZhangWu.class), params);} catch (SQLExceptione) {throw new RuntimeException (e);}} 3.6.6. Add account 3.6.1. Functional analysis
1 write the addZhangWu method in the MainView class
1.1 enter the newly added accounting information on the keyboard
1.2 call the addZhangWu method in the ZhangWuService class to specify the addition of accounts.
1.3 after adding, use the output statement to prompt "add account successfully!"
2 write the addZhangWu method in the ZhangWuService class
2.1Call the addZhangWu method in the ZhangWuDao class to specify the addition of accounts
3 write the addZhangWu method in the ZhangWuDao class
3.1 through the QueryRunner object, call the update method to update the database table gjp_zhangwu, and add the specified accounts to the database table.
3.6.2. Function realization steps
1. Write addZhangWu methods in the MainView class
/ * add accounting method * / public void addZhangWu () {/ * * 1. Get user input and encapsulate it in bean. * 2. Call service's addZhangWu () method to complete the add function * / Scanner in = new Scanner (System.in); ZhangWu zw = new ZhangWu (); System.out.print ("Please enter Category:"); zw.setFlname (in.next ()); System.out.print ("Please enter account:"); zw.setZhangHu (in.next ()); System.out.print ("Please enter amount:"); zw.setMoney (in.nextDouble ()) System.out.print ("Please enter time:"); zw.setCreatetime (in.next ()); System.out.print ("Please enter description:"); zw.setDescription (in.next ()); zhangWuService.addZhangWu (zw); System.out.println ("account added successfully!") ;}
two。 Write addZhangWu methods in the ZhangWuService class
/ * add accounts * @ param zw * / public void addZhangWu (ZhangWuzw) {zhangWuDao.addZhangWu (zw);}
3. Write addZhangWu methods in the ZhangWuDao class
/ * add accounts * @ param zw * / public void addZhangWu (ZhangWuzw) {String sql = "insert into gjp_zhangwu (flname,money,zhanghu,createtime,description) values"; try {Object [] params = {zw.getFlname (), zw.getMoney (), zw.getZhangHu (), zw.getCreatetime (), zw.getDescription ()}; qr.update (sql,params);} catch (SQLExceptione) {throw new RuntimeException (e);}} 3.7. Edit the account 3.7.1. Functional analysis
1 write the editZhangWu method in the MainView class
1.1 enter the ID number of the accounting information to be edited by the keyboard
1.2 enter the content of the accounting information to be modified by the keyboard
1.3 call the editZhangWu method in the ZhangWuService class to update the specified accounting information
1.4 after the update, use the output statement to prompt "Edit account successful!"
2 write the editZhangWu method in the ZhangWuService class
2.1Call the editZhangWu method in the ZhangWuDao class to update the specified accounting information
3 write the editZhangWu method in the ZhangWuDao class
3.1 through the QueryRunner object, call the update method to update the database table gjp_zhangwu and complete the specified account update operation in the database table.
3.7.2. Function realization steps
1. Write editZhangWu methods in the MainView class
/ * Edit accounting method * / public void editZhangWu () {/ * * 1. Get user input and encapsulate it in bean. , 2. Call the editZhangWu () method of service to complete the add function * / Scanner in = new Scanner (System.in); ZhangWu zw = new ZhangWu (); System.out.print ("Please enter ID:"); zw.setZwid (in.nextInt ()); System.out.print ("Please enter New Category:"); zw.setFlname (in.next ()); System.out.print ("Please enter new account:"); zw.setZhangHu (in.next ()) System.out.print ("Please enter New amount:"); zw.setMoney (in.nextDouble ()); System.out.print ("Please enter New time:"); zw.setCreatetime (in.next ()); System.out.print ("Please enter New description:"); zw.setDescription (in.next ()); zhangWuService.editZhangWu (zw); System.out.println ("Edit account successful!") ;}
two。 Write editZhangWu methods in the ZhangWuService class
/ * Edit accounts * @ param zw * / public void editZhangWu (ZhangWuzw) {zhangWuDao.editZhangWu (zw);}
3. Write editZhangWu methods in the ZhangWuDao class
/ * Edit accounts * @ param zw * / public void editZhangWu (ZhangWuzw) {String sql = "update gjp_zhangwu set flname=?, money=?,zhanghu=?,createtime=?,description=? where zwid=?"; try {Object [] params = {zw.getFlname (), zw.getMoney (), zw.getZhangHu (), zw.getCreatetime (), zw.getDescription (), zw.getZwid ()}; qr.update (sql,params);} catch (SQLExceptione) {throw new RuntimeException (e);}} 3.8. Delete account 3.8.1. Functional analysis
1 write the deleteZhangWu method in the MainView class
1.1 enter the ID number of the account information to be deleted by keyboard
1.2 call the deleteZhangWu method in the ZhangWuService class to delete the specified accounting information
1.3 after deletion, use the output statement to prompt "delete the account successfully!"
2 write the deleteZhangWu method in the ZhangWuService class
2.1Call the deleteZhangWu method in the ZhangWuDao class to delete the specified accounting information
3 write the deleteZhangWu method in the ZhangWuDao class
3.1 through the QueryRunner object, call the update method to update the database table gjp_zhangwu and complete the specified account deletion operation in the database table.
3.8.2. Function realization steps
1. Write deleteZhangWu methods in the MainView class
/ * delete account method * / public void deleteZhangWu () {/ * * 1. Get user input and encapsulate it in bean. * 2. Call the deleteZhangWu () method of service to complete the add function * / Scanner in = new Scanner (System.in); System.out.print ("Please enter ID:"); zhangWuService.deleteZhangWu (in.nextInt ()); System.out.println ("account deleted successfully!") ;}
two。 Write deleteZhangWu methods in the ZhangWuService class
/ * delete accounts * @ param zwid * / public void deleteZhangWu (int zwid) {zhangWuDao.deleteZhangWu (zwid);}
3. Write deleteZhangWu methods in the ZhangWuDao class
/ * Delete accounts * @ param zwid * / public void deleteZhangWu (int zwid) {String sql = "delete from gjp_zhangwu where zwid=?"; try {qr.update (sql,zwid);} catch (SQLExceptione) {throw new RuntimeException (e);}} 3.9. Summary of functional modules
Congratulations, we have completed all the functions of the classification module. Through the implementation of the function of the classification module, you may find the following problems:
L difficult place: lack of self-functional analysis ability, need to be analyzed by lecturer
L code implementation:
·View (Controller): the largest amount of code
Angular Service layer: the amount of code is minimum
Angular DAO layer: the similarity is high, but it is not difficult
The function of the view layer is the "interface", which is used to complete the data display to the user. The view layer of the current project contains the Controller layer code.
The function of the Controller layer is "scheduling", which schedules the presentation layer view and the business layer Service. The main functions are divided into: one is to give the data of the presentation layer to the business layer for processing; the other is to give the data returned by the business layer to the presentation layer for display. As for why there is a large amount of code in this layer of View (Controller), I would like to explain here that it is not a large amount of code in View (Controller) layer, but a small amount of code in other layers, so you will feel that the amount of code in View (Controller) layer is large.
The role of the Service layer is "business", and we can also think of "business" as "function". So why is there so little code in the Service layer? The reason is that the project we write now is very small and there is no complex business, and the largest amount of code for the large-scale project we are going to write in the future is the Service layer.
The DAO layer is to manipulate the database, and now we use commons-dbutils tools to simplify JDBC, so we find that there is not much code, which is relatively simple.
Finally, we will learn about other DAO layer tools, such as hibernate and mybatis, which are wrappers for JDBC and are used to simplify JDBC.
This is the end of this article on "how to make accounting software using Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.