1. The housekeeper project

Keywords: Java Database SQL MySQL JDBC

01 Project Training Objectives

* A: Project training objectives
    * a: Project objectives
        * Comprehensive use of the knowledge points learned before
        * Skilled in method invocation operation among View layer, Service layer and Dao layer.
        * Skilled dbutils operation database tables to complete add, delete and change checks
        * Understand the company's project development process, fully grasp the project requirements analysis, design and functional code implementation. Improve students'ability to independently analyze needs and realize functions.            
    

Functional modules in project 0 Two

* A: Functional modules in the project    
    * a: Five modules
        * Inquiry Accounts
        * Multi-Conditional Combination Query Accounting
        * Adding Accounts
        * Editorial Accounts
        * Delete accounts
        

Selection of 03 technology and related jar packages

* A: Technology selection and related jar packages
    * a: apache's commons component:
        * commons-dbutils-1.4.jar: Encapsulates and simplifies JDBC;
        * commons-dbcp-1.4.jar: the database connection pool component provided by apache commons, named DBCP;
    * b: commons.pool-1.3.jar: DBCP connection pool depends on the jar package;
        * mysql-connector-java-5.1.28-bin.jar: JDBC driver package for MySQL, which must be used to connect to MySQL database with JDBC.

Tool classes in project 04

* A: Tool classes in projects
    * a: Introduction to Tool Classes    
        * There will be many tool classes in each project. It is not required that each tool class pair can be written independently, but that tool classes should be used.
        * JDBCUtils: Used to create database connection pool objects

Design of 05 Data Table

* A: Design of Data Table
    * a: Design of data tables (see: day34_source/table relationship. JPG)
        * There is a relationship between tables
        * Relations between master and slave tables
        * The primary key in the master table acts as the foreign key in the slave table

                

06 Create database data table to write test data

* A: Create database tables to write test data
    * a: Create database tables
        /*
          Creating a database of housekeepers
          Name gjp
        */
        CREATE DATABASE gjp;

        USE gjp;

        /*
          Create data tables, table name accounts
          Fields, columns
          Primary key
          Category name variable characters
          Amount double
          Account Variable Characters (Payment, Income Method)
          Create date
          Variable Characters for Account Description
        */

        CREATE TABLE gjp_zhangwu(
           -- Primary key
           zwid INT PRIMARY KEY AUTO_INCREMENT,
           -- Category name   
           flname VARCHAR(200),
           -- Amount of money
           money DOUBLE,
           -- account
           zhanghu VARCHAR(100),
           -- Creation date
           createtime DATE,
           -- Account description
           description  VARCHAR(1000)
        );
        SELECT * FROM gjp_zhangwu;
    * b: Write data
        -- Write test data
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (1,'Food expenses',247,'Bank of Communications','2016-03-02','Family dinner');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (2,'Wage Income',12345,'cash','2016-03-15','Pay');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (3,'Clothing expenditure',1998,'cash','2016-04-02','Buy clothes');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (4,'Food expenses',325,'cash','2016-06-18','Friends Dinner');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (5,'Stock income',8000,'Industrial and Commercial Bank of China','2016-10-28','Stocks soared');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (6,'Stock income',5000,'Industrial and Commercial Bank of China','2016-10-28','Stocks soared again');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (7,'Wage Income',5000,'Bank of Communications','2016-10-28','Pay again');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (8,'Gift Expenditure',5000,'cash','2016-10-28','Friends Married');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (9,'Other expenditures',1560,'cash','2016-10-29','Lost money');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (10,'Transportation expenditure',2300,'Bank of Communications','2016-10-29','Oil prices are still rising');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (11,'Food expenses',1000,'Industrial and Commercial Bank of China','2016-10-29','Eat again');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (12,'Wage Income',1000,'cash','2016-10-30','Open capital');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (13,'Transportation expenditure',2000,'cash','2016-10-30','Tickets are expensive');
        INSERT  INTO gjp_zhangwu(zwid,flname,money,zhangHu,createtime,description) VALUES (14,'Wage Income',5000,'cash','2016-10-30','Open up funds again');

Hierarchical Design in 07 Project

* A: Hierarchical Design in Projects
    * a: Introduction to the functions of each layer
        * view Layer Role: view Layer, the interface in a project
        * controller Layer: Control Layer, which acquires data on the interface and sets data for the interface; The functions to be implemented are handled by the business layer.
        * service Layer Function: Business Layer, Function Implementation, Interaction with controller Control Layer and DAO Data Access Layer, Handling the Operation of Database to DAO Data Access Layer
        * dao Layer: Data Access Layer for manipulating data from database tables
        * db database: MySQL
        * domain Entity Package: Stores JavaBean s
        * tools toolkit: Store the toolkit classes used in the project
        * test test packages: code for storing project functional tests

08 Create Project Layer Import jar Package

* A: Create the project hierarchy import the jar package
    * a: Create engineering packages
        * cn.itcast.gjp.app: Stores main method classes;
        * cn.itcast.gjp.domain: Store JavaBean s;
        * cn.itcast.gjp.view: Storage interface and presentation layer class;
        * cn.itcast.gjp.service: stores business layer classes;
        * cn.itcast.gjp.dao: Store data access layer class;
        * cn.itcast.gjp.tools: Storage tool class
    * b: Import the jar package
        * Create folder lib under project root path
        * Import the following jar packages
            * mysql-connector-java-5.1.37-bin.jar: database driver
            * commons-dbutils-1.6.jar: Provides QueryRunner classes for easy addition, deletion and modification operations
            * commons-dbcp-1.4.jar: 
            * commons-pool-1.5.6.jar: Providing efficient database connection pool technology                
        * Copy the jar package above, select the jar package / right-click / Build Path/Add to Build Path

09 Create classes in domain packages

* A: Establish domain Classes in packages
    * a: Case code
        public class ZhangWu {
             private int  zwid;
              
             private String flname; 
            
             private double  money; 
              
             private String zhanghu;
            
             private String createtime; 
            
             private String description;
             //Attention should be paid to generating null parameter constructions, parametric constructions, set and get methods, toString methods, etc.
        }
    

10 Create JDBCUtils Tool Class

* A: Establish JDBCUtils Tool class
    * a: Case code
        public class JDBCUtils{
            //Creating BasicDataSource Objects
            private static BasicDataSource datasource = new BasicDataSource();
            //Static Code Block, Implementing Necessary Parameter Settings
            static{
                datasource.setDriverClassName("com.mysql.jdbc.Driver");
                datasource.setUrl("jdbc:mysql://localhost:3306/gjp");
                datasource.setUsername("root");
                datasource.setPassword("123");
                datasource.setMaxActive(10);
                datasource.setMaxIdle(5);
                datasource.setMinIdle(2);
                datasource.setInitialSize(10);
            }
            public static DataSource getDataSource(){
                return datasource;
            }
        }

            

11 Create classes in other packages

* A: Create classes in other packages
    * a: cn.itcast.gjp.dao Create in the package ZhangWuDao class
        /*
         *  Implementing the operation of adding, deleting, altering and checking data in data table gjp_zhangwu
         *  dbuils Tool class completion, class members create QueryRunner objects, specify data sources
         */
        public class ZhangWuDao {
            private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        }
    * b: cn.itcast.gjp.service Create in the package ZhangWuService class
        /*
         *  Business layer class
         *  Receive data from the upper layer, control layer controller
         *  After calculation, it is passed to the dao layer to operate the database.
         *  Call the class in the Dao layer, the class member location, and create the object of the Dao class
         */
        public class ZhangWuService {
            private ZhangWuDao dao = new ZhangWuDao();
            
        }
    * c: cn.itcast.gjp.controller Establishment in Package ZhangWuController class
        /*
         *  Controller Layer
         *  Receive data from view layer and transfer data to service layer
         *  Membership location, creating service objects
         */
        public class ZhangWuController {
            private ZhangWuService service = new ZhangWuService();                
        }
    * d: cn.itcast.gjp.view Establishment in Package MainView class
        /*
         *  Attempt Layer, User Viewing and Operating Interface
         *  Data transfer to controller layer implementation
         *  Membership location, create controller object
         */
        public class MainView {
            private ZhangWuController controller = new ZhangWuController();
            
        }
    * e: cn.itcast.gjp.app Establishment in Package MainApp class
        /*
         *  Main Program Class, Function, Open Software Program
         */
        public class MainApp {
            public static void main(String[] args) {
                new MainView().run();
            }
        }
        

12 Implementing User's Interface Menu

* A: Implementing User's Interface Menu
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView Add to Class run Method
        /*
         *  Implementing Interface Effect
         *  Receive user input
         *  Call different functional methods based on data
         */
        public void run(){
            //Create Scanner class objects and repeat keyboard input
            Scanner sc = new Scanner(System.in);
            while(true){
                System.out.println("---------------Housekeeping software---------------");
                System.out.println("1.Adding Accounts 2.Editorial Accounts 3.Delete accounts 4.Inquiry Accounts 5.Exit System");
                System.out.println("Please enter the serial number of the function to be operated on.[1-5]:");
                //Receiving User's Menu Selection
                int choose = sc.nextInt();
                //To judge the menu selected, call different functions
                switch(choose){
                case 1:
                   // Select Add Account and Call the Method of Add Account
                    break;
                case 2:
                    // Select the Edit Account and Call the Edit Account Method
                    break;
                case 3:
                    // Select Delete Account and Call Delete Account Method
                    break;
                case 4:
                    // The choice is to query the accounts and invoke the query method.
                    //selectZhangWu();
                    break;
                case 5:
                    System.exit(0);
                    break;
                }
            }
        }
        

        

13. Interface menu for query

* A: Interface Menu for Implementing Queries
    * a: Case Core Code
        *  cn.itcast.gjp.view Establishment in Package MainView Add to Class selectZhangWu Methods selectAll Methods select Method
            /*
             * Define method selectZhangWu()
             * Way to Display Queries 1 All Queries 2 Conditional Queries
             * Selection of Receiving Users
             */
             public void selectZhangWu(){
                 System.out.println("1. Query all    2. Conditional Query");
                 Scanner sc = new Scanner(System.in);
                 int selectChooser = sc.nextInt();
                 //Decide to call different functions according to the user's choice
                 switch(selectChooser){
                 case 1:
                     //Select all queries, call all query methods
                     selectAll();
                     break;
                 case 2:
                     //Selected condition query, calling method with query condition
                     select();
                     break;
                 }
             }
             /*
              * Define methods to query all accounting data
              */
             public void selectAll(){
                 
             }
            
             /*
              * Define method to realize conditional query of accounting data
              * Provide user input date, start date and end date
              * For two dates, pass to the controller layer
              * Call the controller method to pass two date parameters
              * Get the result set of the controller query and print it out
              */
             public void select(){
                
             }
    

14 Realize the control of inquiring all accounts and the realization of business layer

* A: Realize the control of inquiring all accounts,Implementation of Business Layer
    * a: Case Core Code
        * a: cn.itcast.gjp.dao Create in the package ZhangWuDao class
        /*
         *  Implementing the operation of adding, deleting, altering and checking data in data table gjp_zhangwu
         *  dbuils Tool class completion, class members create QueryRunner objects, specify data sources
         */
        public class ZhangWuDao {
            private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
            /*
             * Define method, query database, get all accounting data
             * Method, invoked by the business layer
             * The result set stores all the accounting data in the Bean object and in the collection
             */
            public List<ZhangWu> selectAll(){                    
                return null;
            }
        }
        * b: cn.itcast.gjp.service Create in the package ZhangWuService class
            /*
             *  Business layer class
             *  Receive data from the upper layer, control layer controller
             *  After calculation, it is passed to the dao layer to operate the database.
             *  Call the class in the Dao layer, the class member location, and create the object of the Dao class
             */
            public class ZhangWuService {
                private ZhangWuDao dao = new ZhangWuDao();
                /*
                 *  Define methods to query all accounting data
                 *  This method is called by the control layer to call the method of the dao layer.
                 *  Returns the List collection that stores ZhangWu objects
                 */
                public List<ZhangWu> selectAll(){
                    return dao.selectAll();
                }
            }
        * c: cn.itcast.gjp.controller Establishment in Package ZhangWuController class
            /*
             *  Controller Layer
             *  Receive data from view layer and transfer data to service layer
             *  Membership location, creating service objects
             */
            public class ZhangWuController {
                private ZhangWuService service = new ZhangWuService();        
                /*
                 * Control layer class definition method to query all accounting data
                 * Method is invoked by the attempt layer, and method invokes the service layer.
                 */
                public List<ZhangWu> selectAll(){
                    return service.selectAll();
                }                    
            }
        

15. Implementing dao Layer to Query All Accounts

* A: Implementing queries for all accounts dao Implementation of Layer
    * a: Case Core Code
        * a: cn.itcast.gjp.dao Create in the package ZhangWuDao class selectAll Method
        /*
         *  Implementing the operation of adding, deleting, altering and checking data in data table gjp_zhangwu
         *  dbuils Tool class completion, class members create QueryRunner objects, specify data sources
         */
        public class ZhangWuDao {
            private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
            /*
             * Define method, query database, get all accounting data
             * Method, invoked by the business layer
             * The result set stores all the accounting data in the Bean object and in the collection
             */
            public List<ZhangWu> selectAll(){
                try{
                    //SQL statement for querying accounting data
                        String sql = "SELECT * FROM gjp_zhangwu";
                        //Call the method of qr object, query method, result set BeanListHandler
                        List<ZhangWu> list = qr.query(sql, new BeanListHandler<>(ZhangWu.class));
                        return list;
                    }catch(SQLException ex){
                        System.out.println(ex);
                        throw new RuntimeException("Inquiry for all accounts failed");
                    }
            }
        }

        

16. Implementing view Layer to Query All Accounts

* A: Implementing queries for all accounts view Implementation of Layer
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class selectAll Method
        /*
          * Define methods to query all accounting data
          */
         public void selectAll(){
             //Call methods in the control layer to query all accounting data
             List<ZhangWu> list = controller.selectAll();
            //Output header
             System.out.println("ID\t\t category\t\t account\t\t Amount of money\t\t time\t\t Explain");
             //Traverse the collection, output the result console
             for(ZhangWu zw : list){
                 System.out.println(zw.getZwid()+"\t\t"+zw.getFlname()+"\t\t"+zw.getZhanghu()+"\t\t"+
                 zw.getMoney()+"\t\t"+zw.getCreatetime()+"\t"+zw.getDescription());
             }
         }

17 menu implementation of conditional query accounting

* A: Menu Realization of Conditional Query Accounting
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class select Method
          /*
          * Define method to realize conditional query of accounting data
          * Provide user input date, start date and end date
          * For two dates, pass to the controller layer
          * Call the controller method to pass two date parameters
          * Get the result set of the controller query and print it out
          */
         public void select(){
             System.out.println("Selection Conditions Query,Input date format XXXX-XX-XX");
             Scanner sc = new Scanner(System.in);
             System.out.print("Please enter the start date:");
             String startDate = sc.nextLine();
             System.out.print("Please enter the result date:");
             String endDate = sc.nextLine();
             //Call the method of the controller layer, pass the date, and get the query result set
             
         }
         

18. Implementation of Control Layer and Business Layer for Conditional Query Accounting

* A: Control Layer to Realize Conditional Query Accounting,Business layer implementation
    * a: Case Core Code
        * a: cn.itcast.gjp.dao Create in the package ZhangWuDao class
            /*
             *  Implementing the operation of adding, deleting, altering and checking data in data table gjp_zhangwu
             *  dbuils Tool class completion, class members create QueryRunner objects, specify data sources
             */
            public class ZhangWuDao {
                private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
                /*
                 * Define methods, query databases, and query accounting tables with conditions
                 * Called by the business layer, the query result set is stored in the Bean object and in the List collection.
                 * The caller passes two date strings
                 */
                public List<ZhangWu> select(String startDate,String endDate){
                    return null;
                }
            }
        * b: cn.itcast.gjp.service Create in the package ZhangWuService class
            /*
             *  Business layer class
             *  Receive data from the upper layer, control layer controller
             *  After calculation, it is passed to the dao layer to operate the database.
             *  Call the class in the Dao layer, the class member location, and create the object of the Dao class
             */
            public class ZhangWuService {
                private ZhangWuDao dao = new ZhangWuDao();
                /*
                 * Define method to realize conditional query accounting
                 * Method is called by the control layer, passing two date strings
                 * Call the method of dao layer and pass two date strings
                 * Get the query result set
                 */
                public List<ZhangWu> select(String startDate,String endDate){
                    return dao.select(startDate, endDate);
                }
            }
        * c: cn.itcast.gjp.controller Establishment in Package ZhangWuController class
            /*
             *  Controller Layer
             *  Receive data from view layer and transfer data to service layer
             *  Membership location, creating service objects
             */
            public class ZhangWuController {
                private ZhangWuService service = new ZhangWuService();        
                /*
                 * Define method to realize conditional query accounting
                 * Method is called by the attempt layer, passing a string of two dates
                 * Call the service layer method, pass two date strings, get the result set
                 * The result set is returned to the attempt
                 */
                public List<ZhangWu> select(String startDate,String endDate){
                    return service.select(startDate, endDate);
                }                    
            }

19 Implementation of dao Layer for Conditional Query Accounting

* A: Achieving Conditional Accounting Inquiry dao Layer Implementation
    * a: Case Core Code
        * a: cn.itcast.gjp.dao Create in the package ZhangWuDao class select Method
            /*
             *  Implementing the operation of adding, deleting, altering and checking data in data table gjp_zhangwu
             *  dbuils Tool class completion, class members create QueryRunner objects, specify data sources
             */
            public class ZhangWuDao {
                private QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
                /*
                 * Define methods, query databases, and query accounting tables with conditions
                 * Called by the business layer, the query result set is stored in the Bean object and in the List collection.
                 * The caller passes two date strings
                 */
                public List<ZhangWu> select(String startDate,String endDate){
                    try{
                        //SQL Statement for Spelling Conditional Query
                        String sql = "SELECT * FROM gjp_zhangwu WHERE createtime BETWEEN ? AND ?";
                        //Define object arrays, store? Placeholders
                        Object[] params = {startDate,endDate};
                        //Invoke the method query of qr object to query the data table and get the result set
                        return qr.query(sql, new BeanListHandler<>(ZhangWu.class),params);
                    }catch(SQLException ex){
                        System.out.println(ex);
                        throw new RuntimeException("Conditional Query Failed");
                    }
                }
            }

Realization of view Layer for Conditional Query Accounting

* A: Achieving Conditional Accounting Inquiry view Layer Implementation
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class selectAll Method optimization and extraction print Methods select Method
        /*
          * Define methods to query all accounting data
          */
         public void selectAll(){
             //Call methods in the control layer to query all accounting data
             List<ZhangWu> list = controller.selectAll();
             if(list.size()!=0)
                 print(list);
             else
                 System.out.println("No data was queried");
         }
        
         /*
          * Define method to realize conditional query of accounting data
          * Provide user input date, start date and end date
          * For two dates, pass to the controller layer
          * Call the controller method to pass two date parameters
          * Get the result set of the controller query and print it out
          */
         public void select(){
             System.out.println("Selection Conditions Query,Input date format XXXX-XX-XX");
             Scanner sc = new Scanner(System.in);
             System.out.print("Please enter the start date:");
             String startDate = sc.nextLine();
             System.out.print("Please enter the result date:");
             String endDate = sc.nextLine();
             //Call the method of the controller layer, pass the date, and get the query result set
             List<ZhangWu> list = controller.select(startDate, endDate);
             if(list.size()!=0)
                 print(list);
             else
                 System.out.println("No data was queried");
         }
         
         //Output Accounting Data Method, Receive List Collection, Travel Collection, Output Table
         private void print(List<ZhangWu> list) {
                //Output header
                 System.out.println("ID\t\t category\t\t account\t\t Amount of money\t\t time\t\t Explain");
                 //Traverse the collection, output the result console
                 for(ZhangWu zw : list){
                     System.out.println(zw.getZwid()+"\t\t"+zw.getFlname()+"\t\t"+zw.getZhanghu()+"\t\t"+
                     zw.getMoney()+"\t\t"+zw.getCreatetime()+"\t"+zw.getDescription());
                 }
            }
            

21 Adding Accounting Function Analysis

* A: Adding Accounting Function Analysis
    * a: Write addZhangWu method in MainView class
        * Keyboard input new account information
        * Call the addZhangWu method in the ZhangWuService class to specify the addition of accounts
        * After adding, use the output statement to prompt "Add Account Successfully!"
    * b: Write the addZhangWu method in ZhangWuService class
        * Call the addZhangWu method in the ZhangWuDao class to specify the addition of accounts
    * c: Writing the addZhangWu method in ZhangWuDao class
        * Update the database table gjp_zhangwu by calling update method through QueryRunner object to complete the addition of specified accounts to the database table
    
    

22 Adding Accounting Function Menu and Implementing Input Function

* A: Adding Accounting Function Menu and Implementing Input Function    
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class addZhangWu Method
        /*
         * Definition method addZhangWu
         * The method of adding account is called when the user chooses menu 1 in the interface.
         * Realizing Thought:
         *       Receive keyboard input, 5 inputs, call the controller layer method
         */
        public void addZhangWu() {
            System.out.println("Select Add Accounting Function, please enter the following");
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter a Category Name");
            String flname = sc.next();
            System.out.println("Input amount");
            double money = sc.nextDouble();
            System.out.println("Input account");
            String zhanghu = sc.next();
            System.out.println("Input date: format XXXX-XX-xx");
            String createtime = sc.next();
            System.out.println("Input specific description");
            String description = sc.next();
            //Call the method of controller layer to transfer the received data and realize data addition.
            
        }
        

23 Adding Accounting Function Control Layer and Implementing Business Layer

* A: Add Accounting Function Control Layer,Business layer implementation
    * a: Case Core Code
        * cn.itcast.gjp.controller In the package ZhangWuController class addZhangWu Method
            /*
             * Define the method to realize the function of adding accounts
             * Called by the view layer, passing parameters (the passed parameters can not be 5 data, passing an object of ZhangWu type)
             * This method calls the service layer method, passes the ZhangWu object, and gets the added result set (the number of rows with successful impact, int)
             * 
             */
            public void addZhangWu(ZhangWu zw) {
                service.addZhangWu(zw);
            }
        * cn.itcast.gjp.service In the package ZhangWuService class addZhangWu Method
            /*
             * Define methods to add accounts
             * It is called by the control layer to pass ZhangWu objects.
             */
            public void addZhangWu(ZhangWu zw) {
                dao.addZhangWu(zw);
            }
        * cn.itcast.gjp.dao In the package ZhangWuDao class addZhangWu Method
            /*
             * Define method to add accounting function
             * Transfer ZhangWu object by business layer call
             * Add data from ZhangWu object to database
             */
            public void addZhangWu(ZhangWu zw) {
                 
            }
            

24 Adding Accounting Function to dao Layer Implementation

* A: Add Accounting Function dao Layer Implementation
    * a: Case Core Code    
        * cn.itcast.gjp.dao In the package ZhangWuDao Class addZhangWu Method
            public void addZhangWu(ZhangWu zw) {
                try{
                     //sql for splicing and adding data
                    String sql = "INSERT INTO gjp_zhangwu (flname,money,zhanghu,createtime,description) VALUES(?,?,?,?,?)";
                    //Create an array of objects with the actual parameters of five placeholders
                    //The actual parameter source is the passed object Zhang Wu
                    Object[] params = {zw.getFlname(),zw.getMoney(),zw.getZhanghu(),zw.getCreatetime(),zw.getDescription()};
                    //Call the method update in the qr object to perform the addition
                    qr.update(sql, params);
                }catch(SQLException ex) {
                    System.out.println(ex);
                    throw new RuntimeException("Account addition failure");
                }
            }
    

25 Add Accounting Function view Layer Implementation

* A: Add Accounting Function view Layer Implementation
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class addZhangWu Method
            public void addZhangWu() {
                System.out.println("Select Add Accounting Function, please enter the following");
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter a Category Name");
                String flname = sc.next();
                System.out.println("Input amount");
                double money = sc.nextDouble();
                System.out.println("Input account");
                String zhanghu = sc.next();
                System.out.println("Input date: format XXXX-XX-xx");
                String createtime = sc.next();
                System.out.println("Input specific description");
                String description = sc.next();
                //Call the method of controller layer to transfer the received data and realize data addition.
                //Encapsulate all parameters entered by users into ZhangWu objects
                ZhangWu zw = new ZhangWu(0, flname, money, zhanghu, createtime, description);
                controller.addZhangWu(zw);
                System.out.println("Congratulations on the success of adding accounts");
            }

                

26 Editor Accounting Function Analysis

* A: Analysis of Editorial Accounting Function
    * a: Write editZhangWu method in MainView class
        * Keyboard input account information ID number to edit
        * Keyboard input to modify the accounting information content
        * Call the editZhangWu method in the ZhangWuService class to update the specified accounting information
        * After updating, use the output statement to prompt "Edit Account Successfully!"
    * b: Writing editZhangWu method in ZhangWuService class
        * Call the editZhangWu method in the ZhangWuDao class to update the specified accounting information
    * c: Writing editZhangWu method in ZhangWuDao class
        * Update the database table gjp_zhangwu by calling update method through QueryRunner object to complete the specified account updating operation in the database table

27 Implement all queries before editing the account function

* A: Implementing all queries before editing the account function
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class editZhangWu Method
            public void editZhangWu() {
                //Call the function of querying all accounting data and display it.
                //See all the data, select one of them, and modify it.
                selectAll();
                System.out.println("The editing function is selected. Please enter the data.");
                
                
            }
                    

Implementation of 28 Editing Accounting Function Menu

* A: Implementation of Editing Accounting Function Menu
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class editZhangWu Method
            public void editZhangWu() {
                //Call the function of querying all accounting data and display it.
                //See all the data, select one of them, and modify it.
                selectAll();
                System.out.println("The editing function is selected. Please enter the data.");
                Scanner sc = new Scanner(System.in);
                System.out.print("Please enter ID");
                int zwid = sc.nextInt();
                System.out.println("Enter a Category Name");
                String flname = sc.next();
                System.out.println("Input amount");
                double money = sc.nextDouble();
                System.out.println("Input account");
                String zhanghu = sc.next();
                System.out.println("Input date: format XXXX-XX-xx");
                String createtime = sc.next();
                System.out.println("Input specific description");
                String description = sc.next();
                //Encapsulate user input data into ZhangWu object
                //User input ID must be encapsulated in the object
                ZhangWu zw = new ZhangWu(zwid, flname, money, zhanghu, createtime, description);
                //Call the method in controller layer to realize editing account
            }            

29 Editing Accounting Function Control Layer and Business Layer Implementation

* A: Editing Account Function Control Layer,Business layer implementation
    * a: Case Core Code
        * cn.itcast.gjp.controller In the package ZhangWuController class editZhangWu Method
            /*
             * Define Method to Realize Editing Accounting Function
             * Called by the view layer, passing parameters, also ZhangWu objects
             * The method that calls the service layer is also the ZhangWu object
             */
            public void editZhangWu(ZhangWu zw) {
                service.editZhangWu(zw);
            }
        * cn.itcast.gjp.service In the package ZhangWuService class editZhangWu Method
            /*
             * Define Method to Realize Editing Accounts
             * Called by the control layer to pass ZhangWu objects
             * Call the method of dao layer to pass ZhangWu object
             */
            public void editZhangWu(ZhangWu zw) {
                dao.editZhangWu(zw);
            }
        * cn.itcast.gjp.dao In the package ZhangWuDao class editZhangWu Method
            public void editZhangWu(ZhangWu zw) {
                // TODO Auto-generated method stub
                
            }
    

dao Layer Implementation of 30 Editing Accounting Function

* A: Editorial Accounting Function dao Layer Implementation
    * a: Case Core Code
        * cn.itcast.gjp.dao In the package ZhangWuDao class editZhangWu Method
            /*
             * Define method to realize editing function
             * Transfer ZhangWu object by business layer call
             * Update the data in the object to the data table
             */
            public void editZhangWu(ZhangWu zw) {
                try {
                    //SQL Update Data
                    String sql = "UPDATE zhangwu SET flname=?,money=?,zhanghu=?,createtime=?,description=? WHERE zwid=?";
                    //Define an array of objects to encapsulate all data
                    Object[] params = {zw.getFlname(),zw.getMoney(),zw.getZhanghu(),zw.getCreatetime(),zw.getDescription(),zw.getZwid()};
                    //Call the qr object method update to perform the update
                    qr.update(sql, params);
                } catch (SQLException ex) {
                    System.out.println(ex);
                    throw new RuntimeException("Editorial Account Failure");
                }
                
            }

            

Implementation of view Layer for Editing Accounts Function

* A: Editorial Accounting Function view Layer Implementation
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class editZhangWu Method
            /*
             * Define the method to realize the editing function of accounting reconciliation
             * Realizing Thought:
             *     Receive user input information
             *  Encapsulated as ZhangWu objects
             *  Call the method of control layer, pass ZhangWu object, realize editing
             * 
             */
            public void editZhangWu() {
                //Call the function of querying all accounting data and display it.
                //See all the data, select one of them, and modify it.
                selectAll();
                System.out.println("The editing function is selected. Please enter the data.");
                Scanner sc = new Scanner(System.in);
                System.out.print("Please enter ID");
                int zwid = sc.nextInt();
                System.out.println("Enter a Category Name");
                String flname = sc.next();
                System.out.println("Input amount");
                double money = sc.nextDouble();
                System.out.println("Input account");
                String zhanghu = sc.next();
                System.out.println("Input date: format XXXX-XX-xx");
                String createtime = sc.next();
                System.out.println("Input specific description");
                String description = sc.next();
                //Encapsulate user input data into ZhangWu object
                //User input ID must be encapsulated in the object
                ZhangWu zw = new ZhangWu(zwid, flname, money, zhanghu, createtime, description);
                //Call the method in controller layer to realize editing account
                controller.editZhangWu(zw);
                System.out.println("Account Editor Successful");
            }
    

32 Delete Account Function Analysis

* A: Analysis of Delete Account Function
    * a: Write deleteZhangWu method in MainView class
        * Keyboard input account information ID number to delete
        * Call the deleteZhangWu method in the ZhangWuService class to delete the specified account information
        * After deleting, use the output statement to prompt "Delete Account Successfully!"
    * b: Writing deleteZhangWu method in ZhangWuService class
        * Call the deleteZhangWu method in the ZhangWuDao class to delete the specified account information
    * c: Writing deleteZhangWu method in ZhangWuDao class
        * Update the database table gjp_zhangwu by calling update method through QueryRunner object to complete the specified account deletion operation in the database table

33 Delete Accounting Function Menu Implementation

* A: Delete Accounting Function Menu Implementation
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class deleteZhangWu Method
            /*
             * Define Method to Achieve Account Delete
             * Realizing Thought:
             *     Receive user input, enter a primary key data
             *  Call the control layer method to pass a primary key
             */
            public void deleteZhangWu() {
                //Call the function of querying all accounting data and display it.
                //See all the data, select one of them, and modify it.
                selectAll();
                System.out.println("The deletion function is selected. Please enter the serial number.");
                int zwid = new Scanner(System.in).nextInt();
                //Call the control layer method and pass the primary key id.
            }
    

34 Delete Accounting Function Control Layer and Realize Business Layer

* A: Delete Accounting Function Control Layer,Business layer implementation
    * a: Case Core Code
        * cn.itcast.gjp.controller In the package ZhangWuController class deleteZhangWu Method
            /*
             * Define methods to implement deletion function
             * View Layer Call, Passing int Type Primary Key
             * Call the service layer method to pass the int primary key
             */
            public void deleteZhangWu(int zwid) {
                service.deleteZhangWu(zwid);
            }
        * cn.itcast.gjp.service In the package ZhangWuService class deleteZhangWu Method
            /*
             * Define method to delete account function
             * Called by the control layer to pass the primary key id
             * Call the dao layer method to pass the primary key id
             */
            public void deleteZhangWu(int zwid) {
                dao.deleteZhangWu(zwid);
            }
        * cn.itcast.gjp.dao In the package ZhangWuDao class deleteZhangWu Method
            public void deleteZhangWu(int zwid) {
    
            }
        

35 Delete Accounting Function dao Implementation

* A: Delete Accounting Function dao Realization
    * a: Case Core Code
        * cn.itcast.gjp.dao In the package ZhangWuDao class deleteZhangWu Method
            /*
             * Define a method to delete business
             * Business layer call, passing primary key id
             */
            public void deleteZhangWu(int zwid) {
                try {
                    //Spelling Delete Data SQL
                    String sql = "DELETE FROM gjp_zhangwu WHERE zwid=?";
                    qr.update(sql, zwid);
                } catch (SQLException ex) {
                    System.out.println(ex);
                    throw new RuntimeException("Failure to delete accounts");
                }
            }

        

36 Delete Accounting Function view Layer Implementation

* A: Delete Accounting Function view Layer Implementation
    * a: Case Core Code
        * cn.itcast.gjp.view Establishment in Package MainView class editZhangWu Method
            /*
             * Define Method to Achieve Account Delete
             * Realizing Thought:
             *     Receive user input, enter a primary key data
             *  Call the control layer method to pass a primary key
             */
            public void deleteZhangWu() {
                //Call the function of querying all accounting data and display it.
                //See all the data, select one of them, and modify it.
                selectAll();
                System.out.println("The deletion function is selected. Please enter the serial number.");
                int zwid = new Scanner(System.in).nextInt();
                //Call the control layer method and pass the primary key id.
                controller.deleteZhangWu(zwid);
                System.out.println("Successful deletion of accounts");
        

37 Summary

* Summarize today's knowledge points.

Posted by yitanpaocai on Tue, 25 Jun 2019 15:21:16 -0700