为了账号安全,请及时绑定邮箱和手机立即绑定

JDBC编程(MVC三层架构)(二)

标签:
Java MySQL

接上文“JDBC编程(MVC三层架构)(一) ”
4、控制层

package com.imooc.action;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.imooc.loser.Loser;
import com.imooc.model.Goddess;

/**
 * 控制层
 * 
 * @author Administrator
 * 
 */

public class GoddessAction {

    /**
     * 添加女神
     * 
     * @param goddess
     *            一个女神对象
     * @throws SQLException 
     */
    public void add(Goddess goddess) throws SQLException {
        // 创建一个屌丝操作者
        Loser loser = new Loser();

        // 添加女神
        loser.addGoddess(goddess);
    }

    /**
     * 更新女神
     * 
     * @param goddess
     *            一个女神对象
     * 
     */
    public void update(Goddess goddess) {
        // 创建一个屌丝操作者
        Loser loser = new Loser();

        // 更新女神
        loser.updateGoddess(goddess);

    }

    /**
     * 删除女神
     * 
     * @param ID
     *            女神的ID号
     */
    public void del(Integer ID) {
        // 创建一个屌丝操作者
        Loser loser = new Loser();

        // 删除女神
        loser.delGoddess(ID);
    }

    /**
     * 
     * @return 获取全部女神资料
     */
    public List<Goddess> query() {
        // 创建一个屌丝操作者
        Loser loser = new Loser();

        // 返回女神资料
        return loser.query();
    }

    /**
     * 
     * @param params
     *            自定义查询条件: connection 连接谓词 ,name 字段名 ,relation 关系词, value 字段值
     * @return 获取女神资料
     */
    public List<Goddess> query(List<Map<String, Object>> params) {
        // 创建一个屌丝操作者
        Loser loser = new Loser();

        // 返回女神资料
        return loser.query(params);
    }

    /**
     * 
     * @param id
     *            女神的ID
     * @return 获得一个女神资料
     */
    public Goddess get(Integer id) {
        // 创建一个屌丝操作者
        Loser loser = new Loser();

        // 通过ID获得女神资料
        return loser.get(id);
    }

}

5、视图层

package com.imooc.view;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import com.imooc.action.GoddessAction;
import com.imooc.model.Goddess;

/**
 * 视图层
 * 
 * @author Administrator
 * 
 */
public class View {
    private static final String CONTEXT = "******欢迎来到女神禁区******\n"
            + "下面是女神禁区的功能列表\n" + "[MAIN/M]:主菜单\n" + "[QUERY/Q]:查看全部女神的基本信息\n"
            + "[GET/G]:查看某位女神的详细信息\n" + "[ADD/A]:添加女神信息\n"
            + "[UPDATE/U]:更新女神信息\n" + "[DELETE/D]:删除女神信息\n"
            + "[SERACH/S]:查询女神信息(根据姓名、手机号来查询)\n" + "[EXIT/E]:退出女神禁区\n"
            + "[BREAK]:退出当前功能,返回主菜单";
    private static final String OPERATION_MAIN = "MAIN";
    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(CONTEXT);

        // Scanner对象
        Scanner scan = new Scanner(System.in);

        // 女神
        Goddess goddess = null;

        // 女神
        Goddess goddessOld = null;

        // 操作
        GoddessAction action = new GoddessAction();

        // 前一个功能
        String previous = null;

        // 输入的内容
        String in = null;

        // 计数
        Integer step = 1;

        // 输入检索字段
        String inputMessage = null;

        // 进入某一个功能的标识,进入true,未进入flase
        boolean into = false;

        while (scan.hasNext()) {
            // 键盘输入的值
            in = scan.next().toString();

            if ((OPERATION_EXIT.equals(in.toUpperCase())// ***退出***
                    || OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase()))
                    && (into == false)) {
                System.out.println("您已成功退出女神禁区!");
                break;
            } else if (OPERATION_BREAK.equals(in.toUpperCase()))// ***中断**务必放在其他功能的前面**
            {
                step = 1; // 恢复到初始值
                previous = null; // 恢复到初始值
                goddess = null; // 恢复到初始值
                into = false; // 恢复到初始值
                System.out.println("您已退出当前功能,且已返回主菜单!!!");
            } else if (((OPERATION_SEARCH.equals(in.toUpperCase())// ***检索***
            || OPERATION_SEARCH.substring(0, 1).equals(in.toUpperCase())) && (into == false))
                    || OPERATION_SEARCH.equals(previous)) {
                previous = OPERATION_SEARCH;
                List<Map<String, Object>> maps = null;
                Map<String, Object> map = null;
                List<Goddess> goddesses = null;
                if (step == 1) {
                    into = true;
                    System.out.println("请输入要查询的字段:\n" + "[NAME]姓名\n"
                            + "[MOBILE]手机号");
                }
                if (step == 2) {
                    inputMessage = in.toUpperCase();
                    if ("NAME".equals(in.toUpperCase())) {
                        System.out.println("请输入要查询女神的姓名:");
                    } else if ("MOBILE".equals(in.toUpperCase())) {
                        System.out.println("请输入查询女神的手机号:");
                    } else {
                        System.out.println("您输入的字段名错误,请重新输入");
                        step = 1;
                    }
                }
                if (step == 3) {
                    if ("NAME".equals(inputMessage)) {
                        System.out.println("请输入要查询的姓名:");
                        maps = new ArrayList<Map<String, Object>>();
                        map = new HashMap<String, Object>();
                        map.put("connection", "AND");
                        map.put("name", "user_name");
                        map.put("relation", "LIKE");
                        map.put("value", "'%" + in + "%'");
                        maps.add(map);
                        goddesses = action.query(maps);

                    } else if ("MOBILE".equals(inputMessage)) {
                        maps = new ArrayList<Map<String, Object>>();
                        map = new HashMap<String, Object>();
                        map.put("connection", "AND");
                        map.put("name", "mobile");
                        map.put("relation", "LIKE");
                        map.put("value", "'%" + in + "%'");
                        maps.add(map);
                        goddesses = action.query(maps);
                    }
                    for (Goddess g : goddesses) {
                        // 显示女神详细资料
                        System.out.print("编号:");
                        if (g.getId() != null) {
                            System.out.print(g.getId());
                        }
                        System.out.print("\n姓名:");
                        if (g.getUser_name() != null) {
                            System.out.print(g.getUser_name());
                        }
                        System.out.print("\n性别:");
                        if (g.getSex() != null) {
                            if (g.getSex() > 0) {
                                System.out.print("女");
                            } else {
                                System.out.print("男");
                            }
                        }
                        System.out.print("\n年龄:");
                        if (g.getAge() != null) {
                            System.out.print(g.getAge());
                        }

                        System.out.print("\n生日:");
                        if (g.getBirthday() != null) {
                            System.out.print(g.getBirthday());
                        }

                        System.out.print("\n邮箱:");
                        if (g.getEmail() != null) {
                            System.out.print(g.getEmail());
                        }
                        System.out.print("\n电话号码:");
                        if (g.getMobile() != null) {
                            System.out.print(g.getMobile() + "\n");
                        } else {
                            System.out.println("\n");
                        }
                    }
                    step = 1; // 恢复初始值
                    previous = null;// 恢复初始值
                    into = false; // 恢复初始值
                }
                if (OPERATION_SEARCH.equals(previous)) {
                    step++;
                }
            } else if ((OPERATION_MAIN.equals(in.toUpperCase())// ***返回主菜单***
                    || OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase()))
                    && (into == false)) {
                System.out.println(CONTEXT);
            } else if ((OPERATION_QUERY.equals(in.toUpperCase())// ***查询***
                    || OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase()))
                    && (into == false)) {
                List<Goddess> g = action.query();
                System.out.println("**下面是禁区女神的基本信息**");
                for (Goddess go : g) {
                    System.out.println("编号:" + go.getId() + ",姓名:"
                            + go.getUser_name());
                }

            } else if (((OPERATION_DELETE.equals(in.toUpperCase())// ***删除***
            || OPERATION_DELETE.substring(0, 1).equals(in.toUpperCase())) && (into == false))
                    || OPERATION_DELETE.equals(previous)) {
                previous = OPERATION_DELETE;
                if (step == 1) {
                    into = true;
                    System.out.println("请输入女神的编号(可删除多个女神资料,用英文逗号分隔,如:1,2,3):");
                } else if (step == 2) {
                    goddess = new Goddess();
                    boolean flag = false; // true 找到女神的资料,false未找到女神的资料
                    try {
                        String[] ids = in.split(",");
                        for (String id : ids) {
                            // 获得女神的资料
                            goddess = action.get(Integer.parseInt(id));
                            if (goddess != null) {
                                // 删除女神资料
                                flag = true;
                                action.del(Integer.parseInt(id));
                            }
                        }
                        if (flag) {
                            System.out.println("女神信息删除成功!!!");
                        } else {
                            System.out.println("未找到女神信息,删除失败!!!");
                        }
                        step = 1; // 恢复到初始值
                        previous = null; // 恢复到初始值
                        goddess = null; // 恢复到初始值
                        into = false; // 恢复到初始值

                    } catch (NumberFormatException e) {// 捕获异常
                        step = 1;
                        System.out.println("请输入数字,用英文逗号分隔,如:1,2,3!!!");
                    }

                }
                if (OPERATION_DELETE.equals(previous)) {
                    step++;
                }

            } else if (((OPERATION_GET.equals(in.toUpperCase())// ***查询一个女神详细信息***
                    || OPERATION_GET.substring(0, 1).equals(in.toUpperCase())) && (into == false))
                    || OPERATION_GET.equals(previous)) {
                previous = OPERATION_GET;
                if (step == 1) {
                    into = true;
                    System.out.println("请输入女神的编号来查看女神的详细信息:");
                } else if (step == 2) {
                    goddess = new Goddess();
                    try { // 检测是否正确输入数字
                        goddess = action.get(Integer.parseInt(in));
                        if (goddess != null) {

                            // 显示女神详细资料
                            System.out.print("编号:");
                            if (goddess.getId() != null) {
                                System.out.print(goddess.getId());
                            }
                            System.out.print("\n姓名:");
                            if (goddess.getUser_name() != null) {
                                System.out.print(goddess.getUser_name());
                            }
                            System.out.print("\n性别:");
                            if (goddess.getSex() != null) {
                                if (goddess.getSex() > 0) {
                                    System.out.print("女");
                                } else {
                                    System.out.print("男");
                                }
                            }
                            System.out.print("\n年龄:");
                            if (goddess.getAge() != null) {
                                System.out.print(goddess.getAge());
                            }

                            System.out.print("\n生日:");
                            if (goddess.getBirthday() != null) {
                                System.out.print(goddess.getBirthday());
                            }

                            System.out.print("\n邮箱:");
                            if (goddess.getEmail() != null) {
                                System.out.print(goddess.getEmail());
                            }
                            System.out.print("\n电话号码:");
                            if (goddess.getMobile() != null) {
                                System.out.print(goddess.getMobile() + "\n");
                            } else {
                                System.out.println("\n");
                            }

                        } else {
                            System.out.println("编号是" + Integer.parseInt(in)
                                    + "的女神资料不存在!!!");
                        }
                        step = 1; // 恢复到初始值
                        previous = null; // 恢复到初始值
                        goddess = null; // 恢复到初始值
                        into = false; // 恢复到初始值

                    } catch (NumberFormatException e) {// 捕获异常
                        step = 1;
                        System.out.println("请输入数字!!!");
                    }

                }
                if (OPERATION_GET.equals(previous)) {
                    step++;
                }
            } else if (((OPERATION_UPDATE.equals(in.toUpperCase())// ***更新***
            || OPERATION_UPDATE.substring(0, 1).equals(in.toUpperCase())) && (into == false))
                    || OPERATION_UPDATE.equals(previous)) {
                previous = OPERATION_UPDATE;

                if (step == 1) {
                    into = true;
                    System.out.println("请输入要更新的女神编号:");
                } else if (step == 2) {
                    try {
                        // 旧值
                        goddessOld = action.get(Integer.parseInt(in));
                        if (goddessOld != null) {
                            goddess = new Goddess();
                            goddess.setId(Integer.parseInt(in));
                            System.out.println("请输入女神的[姓名](如果不更新该字段,请输入null):");
                        } else {
                            System.out.println("女神资料不存在,请重新输入女神的编号!!!");
                            step = 1; // 提示重新输入女神编号
                        }

                    } catch (NumberFormatException e) {
                        step = 1; // 提示重新输入女神编号
                        System.out.println("请输入数字!!!");
                    }

                } else if (step == 3) {
                    if (!in.equals("null")) {
                        goddess.setUser_name(in);
                    } else {
                        goddess.setUser_name(goddessOld.getUser_name());
                    }
                    System.out.println("请输入女神的[年龄](如果不更新该字段,请输入-1):");

                } else if (step == 4) {
                    try {
                        if (Integer.parseInt(in) != -1) {
                            goddess.setAge(Integer.parseInt(in));
                        } else {
                            goddess.setAge(goddessOld.getAge());
                        }
                        System.out
                                .println("请输入女神的[生日],格式如:yyyy-MM-dd(如果不更新该字段,请输入0000-00-00):");
                    } catch (NumberFormatException e) {
                        step = 3; // 提示重新输入年龄
                        System.out.println("请输入数字!!!");
                    }

                } else if (step == 5) {
                    // 格式化日期
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        // 日期对象
                        Date birthday = sdf.parse(in);
                        if (!birthday.toString().equals("0000-00-00")) {
                            goddess.setBirthday(birthday);
                        } else {
                            goddess.setBirthday(goddessOld.getBirthday());
                        }
                        System.out.println("请输入女神的[邮箱](如果不更新该字段,请输入null):");
                    } catch (ParseException e) {
                        step = 4;// 重新提示输入日期信息
                        System.out.println("您输入的日期格式错误,请重新输入!");
                    }

                } else if (step == 6) {
                    if (!in.equals("null")) {
                        goddess.setEmail(in);
                    } else {
                        goddess.setEmail(goddessOld.getEmail());
                    }
                    System.out.println("请输入女神的[手机](如果不更新该字段,请输入null):");

                } else if (step == 7) {
                    if (!in.equals("null")) {
                        goddess.setMobile(in);
                    } else {
                        goddess.setMobile(goddessOld.getMobile());
                    }
                    action.update(goddess);
                    System.out.println("女神信息更新成功!!!");
                    step = 1; // 恢复到初始值
                    previous = null; // 恢复到初始值
                    goddess = null; // 恢复到初始值
                    goddessOld = null; // 恢复到初始值
                    into = false; // 恢复到初始值
                }
                if (OPERATION_UPDATE.equals(previous)) {
                    step++;
                }
            } else if (((OPERATION_ADD.equals(in.toUpperCase())// ***新增***
                    || OPERATION_ADD.substring(0, 1).equals(in.toUpperCase())) && (into == false))
                    || OPERATION_ADD.equals(previous)) {

                previous = OPERATION_ADD;
                if (step == 1) {
                    System.out.println("请输入女神的[姓名]:");
                    into = true;
                } else if (step == 2) {
                    goddess = new Goddess();
                    goddess.setUser_name(in);
                    System.out.println("请输入女神的[年龄]:");
                } else if (step == 3) {
                    try {
                        goddess.setAge(Integer.parseInt(in));
                        System.out.println("请输入女神的[生日],格式如:yyyy-MM-dd:");
                    } catch (NumberFormatException e) {
                        step = 2; // 提示重新输入年龄
                        System.out.println("请输入数字!!!");
                    }
                } else if (step == 4) {
                    // 格式化日期
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        // 日期对象
                        Date birthday = sdf.parse(in);

                        // 添加日期
                        goddess.setBirthday(birthday);

                        System.out.println("请输入女神的[邮箱]:");
                    } catch (ParseException e) {
                        step = 3;// 重新提示输入日期信息
                        System.out.println("您输入的日期格式错误,请重新输入!");
                    }
                } else if (step == 5) {
                    goddess.setEmail(in);
                    System.out.println("请输入女神的[手机号]:");
                } else if (step == 6) {
                    goddess.setMobile(in);
                    try {
                        action.add(goddess);
                        System.out.println("女神信息添加成功!!!");

                    } catch (SQLException e) {
                        e.printStackTrace();
                        System.out.println("女神信息添加失败!!!");
                    }
                    step = 1; // 恢复到初始值
                    previous = null; // 恢复到初始值
                    goddess = null; // 恢复到初始值
                    into = false; // 恢复到初始值
                }
                // 只有是增加时才计数
                if (OPERATION_ADD.equals(previous)) {
                    step++;
                }
            } else {
                System.out.println("您输入的代号有误,请重新输入!!!");
            }
        }

    }
}
点击查看更多内容
5人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消