欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

《基于JDBC实现一个简易的人员信息管理系统》

程序员文章站 2024-03-17 16:04:34
...

一、基本思想

基于MVC经典的架构模式,各层之间分工明确,保证了低耦合。具体如下:

  • Model layer:数据库连接,实体类,DAO层
  • View layer:调用Control layer
  • Control layer:调用Model layer

《基于JDBC实现一个简易的人员信息管理系统》



二、需求分析

基于JDBC实现一个简易的人员信息管理系统,该系统基本可以完成人员信息的添加、删除、查询、更新(修改)等过程(即CRUD),视图层使用简单的控制台方式来展示(因为视图层在这里不是重点)。



三、前期准备

1. 创建人员信息数据库

打开Navicat 12 for MySQL,新建一个连接,这里为“mysql”,然后创建一个数据库,这里为“mydatabase”,然后右键该数据库名,进入命令列界面输入如下MySQL语句建立人员信息表:

create table people_information(
     id int primary key auto_increment,
     userName varchar(30) not null,
     sex int,
     age int,
     birthday date,
     email varchar(30),
     mobile varchar(11),
     createUser varchar(30),
     createDate date,
     updateUser varchar(30),
     updateDate date,
     isDel int
     )engine=innodb default charset=utf8 auto_increment=1;

alter database mysql default character set 'utf8';
SET character_set_client='utf8';
SET character_set_connection='utf8';
SET character_set_results='utf8';

执行语句之后,界面如下:

《基于JDBC实现一个简易的人员信息管理系统》

2. 工程目录组织

《基于JDBC实现一个简易的人员信息管理系统》



四、代码实现

1. Model layer

Code:

JDBC_Connection.java

package com.wuchangi.Model_Layer;

import java.sql.*;

/*
 * @program: JDBC_PeopleInformation
 * @description: Tool Class to get the connection to the MySQL Database.
 * @author: WuchangI
 * @create: 2018-05-13-10-23
 **/

public class JDBC_Connection
{

    private static final String URL = "jdbc:mysql://localhost:3306/mysql?serverTimezone=GMT&useSSL=false";
    private static final String USER = "root";
    private static final String PASSWORD = "123456";

    private static Connection connection = null;

    static
    {
        //1.Loading driver using class name by reflection
        //Old loading method: "com.mysql.jdbc.Driver"
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }


        //2.Get the connection to the MySQL Database
        try
        {
            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }

    }

    public static Connection getConnection()
    {
        return connection;
    }

}


Code:

PeopleInformation.java

package com.wuchangi.Model_Layer;

import java.util.Date;


/*
 * @program: JDBC_PeopleInformation
 * @description: PeopleInformation Model Class
 * @author: WuchangI
 * @create: 2018-05-13-10-23
 **/

public class PeopleInformation
{
    //人员唯一的编号,设置为主键且自增
    private Integer id;
    //人员的姓名
    private String userName;
    //人员的性别,0为男性,1为女性
    private Integer sex;
    //人员的年龄
    private Integer age;
    //人员的生日
    private Date birthday;
    //人员的邮箱
    private String email;
    //人员的手机号码
    private String mobile;
    //创建该记录的人的名称
    private String createUser;
    //更新该记录的人的名称
    private String updateUser;
    //该记录创建日期
    private Date createDate;
    //该记录更新日期
    private Date updateDate;
    //是否删除,1为删除,0为不删除
    private Integer isDel;

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getUserName()
    {
        return userName;
    }

    public void setUserName(String userName)
    {
        this.userName = userName;
    }

    public Integer getSex()
    {
        return sex;
    }

    public void setSex(Integer sex)
    {
        this.sex = sex;
    }

    public Integer getAge()
    {
        return age;
    }

    public void setAge(Integer age)
    {
        this.age = age;
    }

    public Date getBirthday()
    {
        return birthday;
    }

    public void setBirthday(Date birthday)
    {
        this.birthday = birthday;
    }

    public String getEmail()
    {
        return email;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }

    public String getCreateUser()
    {
        return createUser;
    }

    public void setCreateUser(String createUser)
    {
        this.createUser = createUser;
    }

    public String getUpdateUser()
    {
        return updateUser;
    }

    public void setUpdateUser(String updateUser)
    {
        this.updateUser = updateUser;
    }

    public Date getCreateDate()
    {
        return createDate;
    }

    public void setCreateDate(Date createDate)
    {
        this.createDate = createDate;
    }

    public Date getUpdateDate()
    {
        return updateDate;
    }

    public void setUpdateDate(Date updateDate)
    {
        this.updateDate = updateDate;
    }

    public Integer getIsDel()
    {
        return isDel;
    }

    public void setIsDel(Integer isDel)
    {
        this.isDel = isDel;
    }

    @Override
    public String toString()
    {
        return "PeopleInformation{" + "id=" + id + ", userName='" + userName + '\'' + ", sex=" + sex + ", age=" + age + ", birthday=" + birthday + ", email='" + email + '\'' + ", mobile='" + mobile + '\'' + ", createUser='" + createUser + '\'' + ", updateUser='" + updateUser + '\'' + ", createDate=" + createDate + ", updateDate=" + updateDate + ", isDel=" + isDel + '}';
    }

}


Code:

DAO.java

package com.wuchangi.Model_Layer;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * @program: JDBC_PeopleInformation
 * @description: DAO Layer
 * @author: WuchangI
 * @create: 2018-05-13-10-23
 **/

public class DAO
{
    //添加一个新的人员信息
    public void addPeopleInformation(PeopleInformation peopleInformation) throws SQLException
    {
        //拿到数据库的连接
        Connection connection = JDBC_Connection.getConnection();

        String sql = "" +
                " insert into mydatabase.people_information(userName, sex, age, birthday, email, mobile," +
                "createUser, createDate, updateUser, updateDate, isDel)"+
                " values(" + "?,?,?,?,?,?,?,current_date(),?,current_date(),?6)";

        //预编译SQL语句,并不直接执行
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //设置SQL语句的参数
        preparedStatement.setString(1, peopleInformation.getUserName());
        preparedStatement.setInt(2, peopleInformation.getSex());
        preparedStatement.setInt(3, peopleInformation.getAge());

        //注意getBirthday返回的是java.util.Date,而setDate的参数是java.sql.Date,需要进行转换
        preparedStatement.setDate(4, new Date(peopleInformation.getBirthday().getTime()));
        preparedStatement.setString(5, peopleInformation.getEmail());
        preparedStatement.setString(6, peopleInformation.getMobile());
        preparedStatement.setString(7, peopleInformation.getCreateUser());
        preparedStatement.setString(8, peopleInformation.getUpdateUser());
        preparedStatement.setInt(9, peopleInformation.getIsDel());

        //执行SQL语句
        preparedStatement.execute();
    }


    //更新id为peopleInformation.getId()的人员的信息
    public void updatePeopleInformation(PeopleInformation peopleInformation) throws SQLException
    {
        //拿到数据库的连接
        Connection connection = JDBC_Connection.getConnection();

        String sql = "" +
                " update mydatabase.people_information"+
                " set userName=?, sex=?, age=?, birthday=?, email=?, mobile=?, updateUser=?, updateDate=current_date(), isDel=?"+
                " where id=?";

        //预编译SQL语句,并不直接执行
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //设置SQL语句的参数
        preparedStatement.setString(1, peopleInformation.getUserName());
        preparedStatement.setInt(2, peopleInformation.getSex());
        preparedStatement.setInt(3, peopleInformation.getAge());
        //注意getBirthday返回的是java.util.Date,而setDate的参数是java.sql.Date,需要进行转换
        preparedStatement.setDate(4, new Date(peopleInformation.getBirthday().getTime()));
        preparedStatement.setString(5, peopleInformation.getEmail());
        preparedStatement.setString(6, peopleInformation.getMobile());
        preparedStatement.setString(7, peopleInformation.getUpdateUser());
        preparedStatement.setInt(8, peopleInformation.getIsDel());
        preparedStatement.setInt(9, peopleInformation.getId());

        //执行SQL语句
        preparedStatement.execute();
    }

    //删除指定id的人员的信息
    public void deletePeopleInformation(Integer id) throws SQLException
    {
        //拿到数据库的连接
        Connection connection = JDBC_Connection.getConnection();

        String sql = "" +
                " delete from mydatabase.people_information" +
                " where id=?";

        //预编译SQL语句,并不直接执行
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //设置SQL语句的参数
        preparedStatement.setInt(1, id);

        //执行SQL语句
        preparedStatement.execute();

    }


    //查询数据库中所有人员的基本信息(id + 姓名 + 年龄)
    public List<PeopleInformation> queryPeopleInformation() throws SQLException
    {
        Connection connection = JDBC_Connection.getConnection();

        //通过数据库的连接操纵数据库,实现增删查改
        Statement stmt = connection.createStatement();

        //其中ResultSet使用的是java.sql.ResultSet
        ResultSet resultSet = stmt.executeQuery("select id, userName, age from mydatabase.people_information");

        List<PeopleInformation> peopleInformations = new ArrayList<PeopleInformation>();
        PeopleInformation peopleInformation = null;

        while(resultSet.next())
        {
            peopleInformation = new PeopleInformation();
            peopleInformation.setId(resultSet.getInt("id"));
            peopleInformation.setUserName(resultSet.getString("userName"));
            peopleInformation.setAge(resultSet.getInt("age"));
            peopleInformations.add(peopleInformation);
        }

        return peopleInformations;
    }

    //根据指定条件检索数据库中的所有人员的信息
    //一个Map存储一个条件表达式,比如 userName = '小美'
    public List<PeopleInformation> queryPeopleInformation(List<Map<String, Object>> params) throws SQLException
    {
        //拿到数据库的连接
        Connection connection = JDBC_Connection.getConnection();

        StringBuilder sql = new StringBuilder();
        sql.append("select * from mydatabase.people_information where 1=1");

        //设置SQL语句的参数
        if(params!=null &&params.size() > 0)
        {
            for(int i = 0; i < params.size(); i++)
            {
                Map<String, Object> map = params.get(i);
                sql.append(" and " + map.get("name") + " " + map.get("relation") + " " + map.get("value"));
            }
        }

        //预编译SQL语句,并不直接执行
        PreparedStatement preparedStatement = connection.prepareStatement(sql.toString());

        ResultSet resultSet = preparedStatement.executeQuery();

        List<PeopleInformation> peopleInformations = new ArrayList<PeopleInformation>();
        PeopleInformation peopleInformation = null;

        while(resultSet.next())
        {
            peopleInformation = new PeopleInformation();

            peopleInformation.setId(resultSet.getInt("id"));
            peopleInformation.setUserName(resultSet.getString("userName"));
            peopleInformation.setAge(resultSet.getInt("age"));
            peopleInformation.setSex(resultSet.getInt("sex"));
            //getDate()返回的是java.sql.Date,不过不必转换,直接使用,因为java.sql.Date是java.util.Date的子集
            peopleInformation.setBirthday(resultSet.getDate("birthday"));
            peopleInformation.setEmail(resultSet.getString("email"));
            peopleInformation.setMobile(resultSet.getString("mobile"));
            peopleInformation.setCreateUser(resultSet.getString("createUser"));
            peopleInformation.setCreateDate(resultSet.getDate("createDate"));
            peopleInformation.setUpdateUser(resultSet.getString("updateUser"));
            peopleInformation.setUpdateDate(resultSet.getDate("updateDate"));
            peopleInformation.setIsDel(resultSet.getInt("isDel"));

            peopleInformations.add(peopleInformation);
        }

        return peopleInformations;
    }


    //查询指定id的人员的信息(只能有一个)
    public PeopleInformation getPeopleInformation(Integer id) throws SQLException
    {

        PeopleInformation peopleInformation = null;

        //拿到数据库的连接
        Connection connection = JDBC_Connection.getConnection();

        String sql = "" +
                " select * from mydatabase.people_information"+
                " where id=?";

        //预编译SQL语句,并不直接执行
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //设置SQL语句的参数
        preparedStatement.setInt(1, id);

        //执行查询操作
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next())
        {
            peopleInformation = new PeopleInformation();

            peopleInformation.setId(resultSet.getInt("id"));
            peopleInformation.setUserName(resultSet.getString("userName"));
            peopleInformation.setAge(resultSet.getInt("age"));
            peopleInformation.setSex(resultSet.getInt("sex"));
            //getDate()返回的是java.sql.Date,不过不必转换,直接使用,因为java.sql.Date是java.util.Date的子集
            peopleInformation.setBirthday(resultSet.getDate("birthday"));
            peopleInformation.setEmail(resultSet.getString("email"));
            peopleInformation.setMobile(resultSet.getString("mobile"));
            peopleInformation.setCreateUser(resultSet.getString("createUser"));
            peopleInformation.setCreateDate(resultSet.getDate("createDate"));
            peopleInformation.setUpdateUser(resultSet.getString("updateUser"));
            peopleInformation.setUpdateDate(resultSet.getDate("updateDate"));
            peopleInformation.setIsDel(resultSet.getInt("isDel"));
        }

        return peopleInformation;
    }

}




2. View layer

Code:

View.java

package com.wuchangi.View_Layer;


import com.wuchangi.Model_Layer.PeopleInformation;
import com.wuchangi.Control_Layer.Control;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*
 * @program: JDBC_PeopleInformation
 * @description: View Layer
 * @author: WuchangI
 * @create: 2018-05-13-10-23
 **/

//使用控制台作为视图层
public class View
{

    //主菜单提示信息
    private static final String MAIN_MENU_CONTENT = "\t\t****欢迎使用人员信息管理系统!****\n\n" +
            "下面是该系统的功能列表:\n\n" +
            "[QUERY/Q]:  查看全部人员的基本信息(id、姓名、年龄)\n" +
            "[GET/G]:    查看某个人的具体信息\n" +
            "[ADD/A]:    添加新的人员信息\n" +
            "[UPDATE/U]: 更新人员信息\n" +
            "[DELETE/D]: 删除人员信息\n" +
            "[SEARCH/S]: 查询指定的人员信息(根据姓名、手机号码查询)\n" +
            "[EXIT/E]:   退出系统\n" +
            "\n请输入您想使用的功能项:";

    //相关操作标记
    private static final String OPERATION_QUERY = "QUERY";
    private static final String OPERATION_GET = "GET";
    private static final String OPERATION_ADD = "ADD";
    private static final String OPERATION_UPDATE = "UPDATE";
    private static final String OPERATION_DELETE = "DELETE";
    private static final String OPERATION_SEARCH = "SEARCH";
    private static final String OPERATION_EXIT = "EXIT";
    private static final String OPERATION_BREAK = "BREAK";


    public static void main(String[] args)
    {
        //展示主菜单界面
        System.out.println(MAIN_MENU_CONTENT);

        Scanner scan = new Scanner(System.in);

        Control control = new Control();

        //记录之前执行的条件分支
        String previousBranch = "";

        Integer step = 1;

        String inputValue = null;

        PeopleInformation peopleInformation = new PeopleInformation();

        List<Map<String, Object>> params = new ArrayList<Map<String, Object>>();

        Map<String, Object> map = new HashMap<String, Object>();

        while (scan.hasNext())
        {
            inputValue = scan.next().toString();

            if (inputValue.toUpperCase().equals(OPERATION_EXIT) || inputValue.toUpperCase().equals(OPERATION_EXIT.substring(0, 1)))
            {
                System.out.println("您已成功退出系统!");
                break;
            }
            else if (inputValue.toUpperCase().equals(OPERATION_QUERY) || inputValue.toUpperCase().equals(OPERATION_QUERY.substring(0, 1)))
            {
                try
                {
                    System.out.println("当前数据库中已有的人员基本信息如下:\n");
                    List<PeopleInformation> plist = control.query();
                    for (PeopleInformation p : plist)
                    {
                        System.out.println("id:" + p.getId() + " , 姓名:" + p.getUserName() + ", 年龄:" + p.getAge());
                    }

                    System.out.println("\n退出当前功能,返回主菜单,请输入[BREAK/B]; 退出系统,请输入[EXIT/E]:");
                }
                catch (SQLException e)
                {
                    System.out.println("查看人员基本信息失败!");
                    e.printStackTrace();
                }

            }
            else if (inputValue.toUpperCase().equals(OPERATION_GET) || inputValue.toUpperCase().equals(OPERATION_GET.substring(0, 1)) || previousBranch.equals(OPERATION_GET))
            {
                previousBranch = OPERATION_GET;

                switch (step)
                {
                    case 1:
                        System.out.println("请输入您所要查询的人员的id号:");
                        break;

                    case 2:
                        Integer idTobeSearched = Integer.valueOf(inputValue);
                        try
                        {
                            PeopleInformation p = control.get(idTobeSearched);
                            System.out.println("该人员的具体信息如下:");
                            System.out.println("id:" + p.getId() + " ,姓名:" + p.getUserName() + " ,性别:" + p.getSex() +
                            " ,年龄:" + p.getAge() + " ,生日:" + p.getBirthday() + " ,邮箱:" + p.getEmail() + " ,手机号码:" +
                            p.getMobile());
                        }
                        catch (SQLException e)
                        {
                            System.out.println("查询人员信息失败!");
                            e.printStackTrace();
                        }
                        break;
                }

                if (step == 2)
                {
                    step = 1;
                    previousBranch = "";
                    System.out.println("\n退出当前功能,返回主菜单,请输入[BREAK/B]; 退出系统,请输入[EXIT/E]:");
                }
                else
                {
                    step++;
                }
            }
            //添加新的人员信息(只需添加新增人员的 姓名 + 性别 + 年龄+ 生日 + 邮箱 + 手机号码)
            else if (inputValue.toUpperCase().equals(OPERATION_ADD) || inputValue.toUpperCase().equals(OPERATION_ADD.substring(0, 1)) || previousBranch.equals(OPERATION_ADD))
            {
                previousBranch = OPERATION_ADD;

                switch (step)
                {
                    case 1:
                        System.out.println("请输入新增人员的[姓名]:");
                        break;

                    case 2:
                        peopleInformation.setUserName(inputValue);
                        System.out.println("请输入新增人员的[性别]:(0为男性,1为女性)");
                        break;

                    case 3:
                        System.out.println(Integer.valueOf(inputValue));
                        peopleInformation.setSex(Integer.valueOf(inputValue));
                        System.out.println("请输入新增人员的[年龄]:");
                        break;

                    case 4:
                        peopleInformation.setAge(Integer.valueOf(inputValue));
                        System.out.println("请输入新增人员的[生日]:(格式为:yyyy-MM-dd)");
                        break;

                    case 5:
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date birthday = null;

                        try
                        {
                            birthday = sdf.parse(inputValue);
                            peopleInformation.setBirthday(birthday);
                            System.out.println("请输入新增人员的[邮箱]:");
                        }
                        catch (ParseException e)
                        {
                            System.out.println("您输入的格式有误!");
                            e.printStackTrace();
                        }
                        break;

                    case 6:
                        peopleInformation.setEmail(inputValue);
                        System.out.println("请输入新增人员的[手机号码]:");
                        break;

                    case 7:
                        peopleInformation.setMobile(inputValue);
                        try
                        {
                            control.add(peopleInformation);
                            System.out.println("添加新的人员信息成功!");
                        }
                        catch (SQLException e)
                        {
                            System.out.println("添加新的人员信息失败!");
                            e.printStackTrace();
                        }
                        break;
                }

                if (step == 7)
                {
                    step = 1;
                    previousBranch = "";
                    System.out.println("\n退出当前功能,返回主菜单,请输入[BREAK/B]; 退出系统,请输入[EXIT/E]:");
                }
                else
                {
                    step++;
                }
            }
            else if (inputValue.toUpperCase().equals(OPERATION_UPDATE) || inputValue.toUpperCase().equals(OPERATION_UPDATE.substring(0, 1)) || previousBranch.equals(OPERATION_UPDATE))
            {
                previousBranch = OPERATION_UPDATE;

                switch (step)
                {
                    case 1:
                        System.out.println("请输入待更新的人员的[姓名]:");
                        break;

                    case 2:
                        peopleInformation.setUserName(inputValue);
                        System.out.println("请输入待更新人员的[性别]:(0为男性,1为女性)");
                        break;

                    case 3:
                        peopleInformation.setSex(Integer.valueOf(inputValue));
                        System.out.println("请输入待更新人员的[年龄]:");
                        break;

                    case 4:
                        peopleInformation.setAge(Integer.valueOf(inputValue));
                        System.out.println("请输入待更新人员的[生日]:(格式为:yyyy-MM-dd)");
                        break;

                    case 5:
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date birthday = null;

                        try
                        {
                            birthday = sdf.parse(inputValue);
                            peopleInformation.setBirthday(birthday);
                            System.out.println("请输入待更新人员的[邮箱]:");
                        }
                        catch (ParseException e)
                        {
                            e.printStackTrace();
                            System.out.println("您输入的格式有误,请重新输入!");
                            step = 4;
                        }
                        break;

                    case 6:
                        peopleInformation.setEmail(inputValue);
                        System.out.println("请输入待更新人员的[手机号码]:");
                        break;

                    case 7:
                        peopleInformation.setMobile(inputValue);
                        System.out.println("请输入待更新人员的[id]:");
                        break;

                    case 8:
                        peopleInformation.setId(Integer.valueOf(inputValue));
                        try
                        {
                            control.update(peopleInformation);
                            System.out.println("更新人员信息成功!");

                        }
                        catch (SQLException e)
                        {
                            System.out.println("更新人员信息失败!");
                            e.printStackTrace();
                        }
                        break;
                }

                if (step == 8)
                {
                    step = 1;
                    previousBranch = "";
                    System.out.println("\n退出当前功能,返回主菜单,请输入[BREAK/B]; 退出系统,请输入[EXIT/E]:");
                }
                else
                {
                    step++;
                }

            }
            else if (inputValue.toUpperCase().equals(OPERATION_DELETE) || inputValue.toUpperCase().equals(OPERATION_DELETE.substring(0, 1)) || previousBranch.equals(OPERATION_DELETE))
            {
                previousBranch = OPERATION_DELETE;

                switch (step)
                {
                    case 1:
                        System.out.println("请输入您要删除的人员的id号:");
                        break;

                    case 2:
                        Integer idTobeSearched = Integer.valueOf(inputValue);
                        try
                        {
                            control.del(idTobeSearched);
                            System.out.println("删除该人员信息成功!");
                        }
                        catch (SQLException e)
                        {
                            System.out.println("删除该人员信息失败!");
                            e.printStackTrace();
                        }
                        break;
                }

                if (step == 2)
                {
                    step = 1;
                    previousBranch = "";
                    System.out.println("\n退出当前功能,返回主菜单,请输入[BREAK/B]; 退出系统,请输入[EXIT/E]:");
                }
                else
                {
                    step++;
                }

            }
            else if (inputValue.toUpperCase().equals(OPERATION_SEARCH) || inputValue.toUpperCase().equals(OPERATION_SEARCH.substring(0, 1)) || previousBranch.equals(OPERATION_SEARCH))
            {
                previousBranch = OPERATION_SEARCH;

                switch(step)
                {
                    case 1:
                        System.out.println("请输入所要查找的人员的姓名:");
                        map.put("name", "userName");
                        map.put("relation", "=");
                        break;
                    case 2:
                        map.put("value", inputValue);
                        params.add(map);
                        System.out.println("请输入所要查找的人员的手机号码:");
                        map.put("name", "mobile");
                        map.put("relation", "=");
                        break;
                    case 3:
                        map.put("value", inputValue);
                        params.add(map);
                        break;
                }

                if (step == 3)
                {
                    try
                    {
                        System.out.println("符合您查询条件的人员信息如下:");
                        List<PeopleInformation> pList = control.query(params);
                        for(PeopleInformation p: pList)
                        {
                            System.out.println("id:" + p.getId() + " ,姓名:" + p.getUserName() + " ,性别:" + p.getSex() +
                                    " ,年龄:" + p.getAge() + " ,生日:" + p.getBirthday() + " ,邮箱:" + p.getEmail() + " ,手机号码:" +
                                    p.getMobile());
                        }
                    }
                    catch (SQLException e)
                    {
                        System.out.println("查询人员信息失败!");
                        e.printStackTrace();
                    }

                    step = 1;
                    previousBranch = "";
                    params = new ArrayList<Map<String, Object>>();
                    System.out.println("\n退出当前功能,返回主菜单,请输入[BREAK/B]; 退出系统,请输入[EXIT/E]:");
                }
                else
                {
                    step++;
                }
            }
            else if (inputValue.toUpperCase().equals(OPERATION_BREAK) || inputValue.toUpperCase().equals(OPERATION_BREAK.substring(0, 1)))
            {
                System.out.println("\n" + MAIN_MENU_CONTENT);
            }
        }

    }

}




3. Control layer

Code:

Control.java

package com.wuchangi.Control_Layer;

import com.wuchangi.Model_Layer.DAO;
import com.wuchangi.Model_Layer.PeopleInformation;

import java.sql.SQLException;
import java.util.*;

/*
 * @program: JDBC_PeopleInformation
 * @description: Control Layer
 * @author: WuchangI
 * @create: 2018-05-13-10-23
 **/

public class Control
{
    //查看某个人的详细信息
    public PeopleInformation get(Integer id) throws SQLException
    {
        DAO dao = new DAO();
        return dao.getPeopleInformation(id);
    }

    //添加某个人的信息
    public void add(PeopleInformation peopleInformation) throws SQLException
    {
        DAO dao = new DAO();

        peopleInformation.setCreateUser("ADMIN");
        peopleInformation.setUpdateUser("ADMIN");
        peopleInformation.setIsDel(0);

        dao.addPeopleInformation(peopleInformation);
    }

    //更新某个人(id为peopleInformation.getId())的人的信息
    public void update(PeopleInformation peopleInformation) throws SQLException
    {
        DAO dao = new DAO();

        peopleInformation.setCreateUser("ADMIN");
        peopleInformation.setUpdateUser("ADMIN");
        peopleInformation.setIsDel(0);

        dao.updatePeopleInformation(peopleInformation);
    }

    //删除某个人的信息
    public void del(Integer id) throws SQLException
    {
        DAO dao = new DAO();
        dao.deletePeopleInformation(id);
    }

    //查询数据库中所有人员的基本信息(id + 姓名 + 年龄)
    public List<PeopleInformation> query() throws SQLException
    {
        DAO dao = new DAO();
        List<PeopleInformation> peopleInformationList =dao.queryPeopleInformation();
        return peopleInformationList;
    }

    //查询数据库中符合指定条件的所有人员的信息
    public List<PeopleInformation> query(List<Map<String, Object>> params) throws SQLException
    {
        DAO dao = new DAO();
        List<PeopleInformation> peopleInformationsList = dao.queryPeopleInformation(params);
        return peopleInformationsList;
    }
}