当前位置: 首页 > 图灵资讯 > 技术篇> 满汉楼

满汉楼

来源:图灵教育
时间:2023-05-29 14:07:58

1.程序框架

满汉楼_List

2.代码实现com中的代码.stulzl.mhl.包中view界面层MHLView主界面

package com.stulzl.mhl.view;import com.stulzl.mhl.domain.*;import com.stulzl.mhl.service.BillService;import com.stulzl.mhl.service.DiningTableService;import com.stulzl.mhl.service.EmployeeService;import com.stulzl.mhl.service.MenuService;import com.stulzl.mhl.utils.Utility;import javax.rmi.CORBA.Util;import java.util.List;///这是主界面   861public class MHLView {    private boolean loop = true;//控制是否退出菜单    private String key = "";///接收用户输入    //定义Employervice属性  862    private EmployeeService employeeService = new EmployeeService();    ///定义DiningTableservice的属性  863    private DiningTableService diningTableService = new DiningTableService();    //定义Menuservice属性  866    private MenuService menuService = new MenuService();    //定义BillService属性  869    private BillService billService = new BillService();    public static void main(String[] args) {        new MHLView().mainMenu();    }    //完成结账  872    public void payBill(){        System.out.println(========================================));        System.out.print(请选择要结账的餐桌号(-1退出):");        int diningTableId = Utility.readInt();        if(diningTableId==-1){            System.out.println(==============================================);            return;        }        ////验证餐桌是否存在        DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);        if(diningTable==null){            System.out.println(============================);            return;        }        ////验证餐桌是否有结账单        if(!billService.hasPayBillByDiningTableId(diningTableId)){            System.out.println(========================================);            return;        }        System.out.print((结账方式(现金/支付宝/微信)-返回车辆表示退出);        String payMode = Utility.readString(20, "");//如果回车,回归""""""        if("".equals(payMode)){            System.out.println(===============================));            return;        }        char key = Utility.readConfirmSelection();        if(key=='Y'){            //结账            if(billService.payBill(diningTableId,payMode)){                System.out.println(=============================================);            }else{                System.out.println(===============================================);            }        }else{            System.out.println(===============================));        }    }    //显示账单信息  870    public void listBill(){//        List bills = billService.list();//        System.out.println("\n号码\n号码\n号码t/\t菜品号码\/\tt\t菜品量\\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态”;//        for (Bill bill : bills) {//            System.out.println(bill);//        }//        System.out.println(===============================================);        //显示所有账单并带有菜名,价格   873-874        List multiTableBeans = billService.list2();        System.out.println("\n号码\n号码\n号码t/\t菜品号码\/\tt\t菜品量\\t菜品量\t\t金额\t\t桌号\t\t日期\t\t\t\t\t\t\t状态\t\t菜名\t\t价格”;        for (MultiTableBean bill : multiTableBeans) {            System.out.println(bill);        }        System.out.println(===============================================);    }    //完成点餐   869    public void orderMenu(){        System.out.println(===========================));        System.out.println(请输入点餐桌号(-1退出);        int orderDiningTableId = Utility.readInt();        if(orderDiningTableId==-1){            System.out.println(============================n");            return;        }        System.out.println(请输入菜品号(-1退出);        int orderMenuId = Utility.readInt();        if(orderMenuId==-1){            System.out.println(============================n");            return;        }        System.out.println(请输入点菜量(-1退出);        int orderNums = Utility.readInt();        if(orderNums==-1){            System.out.println(============================n");            return;        }        ///开始验证餐桌号是否存在        DiningTable diningTable= diningTableService.getDiningTableById(orderDiningTableId);        if (diningTable==null){            System.out.println(===============;            return;        }        ///验证菜号        Menu menu = menuService.getMenuById(orderMenuId);        if(menu == null){            System.out.println(==============================================);            return;        }        //点餐        if(billService.orderMenu(orderMenuId,orderNums,orderDiningTableId)){            System.out.println(===========================));        }else{            System.out.println(=============================);        }    }    //显示所有菜品  866    public void listMenu(){        List list = menuService.list();        System.out.println(”\t\t菜名\t\t类别\t\t价格”;        for (Menu menu : list) {            System.out.println(menu);        }        System.out.println(==========================================));    }    //完成订座  865    public void orderDiningTable(){        System.out.println(=============================));        System.out.print(请选择预定的餐桌编号(-1退出);        int orderId = Utility.readInt();        if(orderId==-1){            System.out.println(========================);            return;        }        ///这种方法是Y或N        char key = Utility.readConfirmSelection();        if(key=='Y(){//要预订            ///根据orderId返回相应的Diningtable对象,如果是null,说明对象不存在            DiningTable diningTable = diningTableService.getDiningTableById(orderId);            if(diningTable==null)判断餐桌是否存在{///                System.out.println(==============================);                return;            }            ///判断餐桌是否“空”            if(!("空".equals(diningTable.getState())){//为空预订,反之亦然,不是空的,也不是不可预测的                System.out.println(=======================”;                return;            }            //这是可以真正预订的,更新餐桌状态            System.out.print("预定人的名字:");            String orderName = Utility.readString(50);            System.out.print("预定人的电话号码:");            String orderTel = Utility.readString(50);            ///更新餐桌状态            if(diningTableService.orderDiningTable(orderId,orderName,orderTel)){                System.out.println(=====================================);            }else{                System.out.println(======================================);            }        }else{            System.out.println(========================);        }    }    ///显示所有餐桌状态  863    public void listDiningTable(){        List list = diningTableService.list();        System.out.println("\n餐桌号码\nt\t餐桌状态;        ///遍历返回的集合,增强for        for (DiningTable diningTable : list) {            System.out.println(diningTable);        }        System.out.println(==================));    }    //显示主菜单   861    private  void mainMenu(){        while(loop){            System.out.println(=================================));            System.out.println("\t\t 1 登录满汉楼”);            System.out.println("\t\t 2 退出满汉楼”);            System.out.println("请输入您的选择:");            key = Utility.readString(1);            switch(key){                case "1":                    System.out.print(“请输入员工号:”;                    String empId = Utility.readString(50);                    System.out.print("请输入密码:";                    String pwd = Utility.readString(50);                    ///去数据库判断   862                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);                    if(employee!=null){///不得空说明用户存在                        System.out.println(===========成功登录[”+employee.getName()===============n");                        //显示二级菜单, 这里的二级菜单是循环操作,所以做while                        while (loop) {                            System.out.println("\n===============满汉楼(二级菜单)==============;                            System.out.println("\t\t 1 显示餐桌状态”);                            System.out.println("\t\t 2 预订餐桌”);                            System.out.println("\t\t 3 展示所有菜品”);                            System.out.println("\t\t 4 点餐服务”);                            System.out.println("\t\t 5 查看账单”);                            System.out.println("\t\t 6 结账");                            System.out.println("\t\t 9 退出满汉楼”);                            System.out.print(“请输入您的选择: ");                            key = Utility.readString(1);                            switch (key) {                                case "1":  //  863                                    listDiningTable();                                    break;                                case "2":  //864-865                                    orderDiningTable();                                    break;                                case "3":  //866                                    listMenu();                                    break;                                case "4":  //869                                    orderMenu();                                    break;                                case "5":  //870                                    listBill();                                    break;                                case "6":  //871-872                                    payBill();                                    break;                                case "9":                                    loop = false;                                    break;                                default:                                    System.out.println(”你的输入是错误的,请重新输入”);                                    break;                            }                        }                    }else{                        System.out.println(=====================);                    }                    break;                case "2":                    System.out.println(“退出满汉楼”;                    loop = false;                    break;                default:                    System.out.println(”你的输入是错误的,请重新输入:");            }        }        System.out.println(你退出了满汉楼系统~);    }}

业务层serviceEmployeserviceereeeer

package com.stulzl.mhl.service;import com.stulzl.mhl.dao.EmployeeDAO;import com.stulzl.mhl.domain.Employee;///完成Employee表的各种操作(通过调用Employeedao对象完成)   862public class EmployeeService {///业务层    //定义EmployeedAO属性    private EmployeeDAO employeeDAO = new EmployeeDAO();    //方法,根据empId 和 pwd 回到Employeee对象    //如果找不到,返回空nullll    public Employee getEmployeeByIdAndPwd(String empId,String pwd){        return employeeDAO.querySingle("select * from employee where empId=? and pwd=md5(? and pwd=md5()                        , Employee.class, empId, pwd);    }}

Diningtableservicer

package com.stulzl.mhl.service;import com.stulzl.mhl.dao.DiningTableDAO;import com.stulzl.mhl.domain.DiningTable;import java.util.List;///完成Diningtable表的各种操作(通过调用Diningtabledao对象完成)   863public class DiningTableService {///业务层    //定义DiningTableDAO对象    private DiningTableDAO diningTableDAO = new DiningTableDAO();    ////返回所有餐桌信息   863    public List list(){        return diningTableDAO.queryMulti("select id, state from diningTable"                , DiningTable.class);    }    如果返回空nulll,//根据id查询相应的餐桌Diningtable对象,这意味着id对应的桌子不存在  864    public DiningTable getDiningTableById(int id){        return diningTableDAO.querySingle("select * from diningTable where id=?"                ,DiningTable.class,id);    }    //如果餐桌可以预订,调用方法更新其状态(包括预订人的姓名和电话号码)   864    public boolean orderDiningTable(int id,String orderName,String orderTel){        ////这里结束update的返回值代表受影响的行数        int update =                diningTableDAO.update("update diningTable set state=已经预定了,orderName=?,orderTel=?,orderTel=? where id=?", orderName, orderTel,id);        return update>0;//update>0代表更新成功,否则不成功    }    ///需要提供更新餐桌状态的方法  868    public boolean updateDiningTableState(int id,String state){        int update = diningTableDAO.update("update diningTable set state=? where id=?"                , state, id);        return update>0;//update>0说明更新成功,返回true,否则返回false    }    //提供方法,将指定的餐桌修改为空闲   871    public boolean updateDiningTableToFree(int id,String state){        int update = diningTableDAO.update("update diningTable set state=?,orderName='',orderTel='' where id=?,orderName='',orderTel='' where id=?"                , state, id);        return update>0;//update>0说明更新成功,返回true,否则返回false    }}

Menuservice菜单

package com.stulzl.mhl.service;import com.stulzl.mhl.dao.MenuDAO;import com.stulzl.mhl.domain.Menu;import java.util.List;///完成Menu表的各种操作(通过调用MenuDAO对象完成)   866public class MenuService {    ///定义MenuDAO属性    private MenuDAO menuDAO = new MenuDAO();    ///返回所有菜肴,给界面使用    public List list(){        return menuDAO.queryMulti("select * from menu", Menu.class);    }    ///需要一种方法根据id返回menu对象  868    public Menu getMenuById(int id){        return menuDAO.querySingle("select * from menu where id = ?",Menu.class,id);    }}

Billservicecececercecellllilllser

package com.stulzl.mhl.service;import com.stulzl.mhl.dao.BillDAO;import com.stulzl.mhl.dao.MultiTableDAO;import com.stulzl.mhl.domain.Bill;import com.stulzl.mhl.domain.MultiTableBean;import java.util.List;import java.util.UUID;///处理与账单相关的业务逻辑  868public class BillService {    //定义BillDAO属性    private BillDAO billDAO = new BillDAO();    //定义Menuservice属性   868    private MenuService menuService = new MenuService();    //定义Diningtableservice属性   868    private DiningTableService diningTableService = new DiningTableService();    //定义MultitableDAO对象  873    private MultiTableDAO multiTableDAO = new MultiTableDAO();    ///编写点餐方法   868    //1. 生成账单    //2. 需要更新相应餐桌的状态    //如果成功返回true,否则,返回false    public boolean orderMenu(int menuId,int nums,int diningTable){        ///生成帐单号,使用UUID方法        String billID = UUID.randomUUID().toString();        //将账单生成到bill表,要求直接计算账单金额        int update = billDAO.update("insert into bill values(null,?,?,?,?,?,?,now()"未结账"                , billID, menuId, nums                , menuService.getMenuById(menuId).getPrice() * nums, diningTable);        if(update<=(0)(///生成账单失败)            return false;        }        ///需要更新相应的餐桌状态        return diningTableService.updateDiningTableState(diningTable,“就餐中”);    }    ///返回所有账单并提供给View  870    public List list(){        return billDAO.queryMulti("select * from bill",Bill.class);    }    ///返回所有账单,并带有菜名和价格  为View提供  873-874    public List list2(){        return multiTableDAO.queryMulti("SELECT bill.*,NAME,price " +                "FROM bill,menu " +                "WHERE bill.menuId=menu.Id",MultiTableBean.class);    }    ///检查餐桌上是否有未结账的账单   871    public boolean hasPayBillByDiningTableId(int diningTableId){        Bill bill =                billDAO.querySingle("SELECT * FROM bill WHERE diningTableId=? AND state='未结账' LIMIT 0,1"                        , Bill.class, diningTableId);        return bill !=null;//如果不等于空,说明有未结账的放回true,否则返回false    }    //完成结账,如果餐桌存在,餐桌上有未结账的账单  871    public boolean payBill(int diningTableId,String payMode){        ///修改bill表        int update =                billDAO.update("update bill set state=? where diningTableId=? and state=“未结账”                        , payMode, diningTableId);        if(update<{///修改billll更新失败            return false;        }        ///修改diningtable表    871        //注意不要直接在这里操作,相反,Diningtableservice应该调用 方法        if(!diningTableService.updateDiningTableToFree(diningTableId,"空")){            return false;//取反代表失败        }        return true;///成功修改    }}

各种DAOBasicDAO

package com.stulzl.mhl.dao;import com.stulzl.mhl.utils.JDBCUtils_Druid;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.BeanListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;import java.sql.Connection;import java.sql.SQLException;import java.util.List;//开发Basicdao,是其他DAO的父类   854public class BasicDAO {///指定具体类型的泛型    private QueryRunner qr = new QueryRunner();    //开发通用dml方法,针对任何表    public int update(String sql,Object... parameters){//Object... parameters可变形参        Connection connection = null;        try {            connection = JDBCUtils_Druid.getConnection();            int update = qr.update(connection, sql, parameters);            return update;//回收影响行数        } catch (SQLException e) {            throw new RuntimeException(e);        } finally {            ///关闭连接            JDBCUtils_Druid.close(null,null,connection);        }    }    ///返回多个对象(即查询结果是多行),所以对于任何表格    /**     *     * @param sql sql 句子,可以有 ?     * @param clazz 传入一个类 Class 对象 比如 Actor.class     * @param parameters 传入 ? 具体值可以是多个值     * @return 根据 Actor.class 返回对应的 ArrayList 集合     */    public List queryMulti(String sql, Class clazz, Object...parameters){        Connection connection = null;        try {            connection = JDBCUtils_Druid.getConnection();            return qr.query(connection, sql, new BeanListHandler(clazz), parameters);        } catch (SQLException e) {            throw new RuntimeException(e);        } finally {            ///关闭连接            JDBCUtils_Druid.close(null,null,connection);        }    }    ////查询单行结果的一般方法    public T querySingle(String sql, Class clazz, Object... parameters) {        Connection connection = null;        try {            connection = JDBCUtils_Druid.getConnection();            return qr.query(connection, sql, new BeanHandler(clazz), parameters);        } catch (SQLException e) {            throw new RuntimeException(e); //编译异常->运行异常 ,抛出        } finally {            JDBCUtils_Druid.close(null, null, connection);        }    }    ///单行单列查询方法,即返回单值的方法    public Object queryScalar(String sql, Object... parameters){        Connection connection = null;        try {            connection = JDBCUtils_Druid.getConnection();            return qr.query(connection, sql, new ScalarHandler(), parameters);        } catch (SQLException e) {            throw new RuntimeException(e); //编译异常->运行异常 ,抛出        } finally {            JDBCUtils_Druid.close(null, null, connection);        }    }}

Employeeedao

package com.stulzl.mhl.dao;import com.stulzl.mhl.domain.Employee;//  862//这里是Employeedao继承Basicdao(Basicdao具有基本功能,如果需要其他功能,可以在Employeedao中添加)public class EmployeeDAO extends BasicDAO{    ///也可以在这里写独特的操作}

Diningtabledaled

package com.stulzl.mhl.dao;import com.stulzl.mhl.domain.DiningTable;//863//这里是DiningTableDAO继承BasicDAO(BasicDAO具有基本功能,如果需要其他功能,可以添加到DiningTabledao中)public class DiningTableDAO extends BasicDAO {    ///也可以在这里写独特的操作}

MenuDAO菜单

package com.stulzl.mhl.dao;import com.stulzl.mhl.domain.Menu;//866//这里是Menudao继承Basicdao(Basicdao具有基本功能,可在Menudao中添加其他功能)public class MenuDAO extends BasicDAO{    ///也可以在这里写独特的操作}

BillDAO账单

package com.stulzl.mhl.dao;import com.stulzl.mhl.domain.Bill;//868//这里是Bildao继承Basicdao的类别(Basicdao具有基本功能,BillDAO可以添加其他功能)public class BillDAO extends BasicDAO{    ///也可以在这里写独特的操作}

MultitabledAO

package com.stulzl.mhl.dao;import com.stulzl.mhl.domain.MultiTableBean;//873//这里是Multitabledao继承Basicdao(Basicdao具有基本功能,可在Multitabledao中添加其他功能)public class MultiTableDAO extends BasicDAO{}

各种domain(javabean)Employeeeee

package com.stulzl.mhl.domain;///这是javabean  与employee相对应  862public class Employee {    private Integer id;    private String empId;    private String pwd;    private String name;    private String job;    public Employee() {//无参构造器,底层需要apache-dbutils反射    }    public Employee(Integer id, String empId, String pwd, String name, String job) {        this.id = id;        this.empId = empId;        this.pwd = pwd;        this.name = name;        this.job = job;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getEmpId() {        return empId;    }    public void setEmpId(String empId) {        this.empId = empId;    }    public String getPwd() {        return pwd;    }    public void setPwd(String pwd) {        this.pwd = pwd;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getJob() {        return job;    }    public void setJob(String job) {        this.job = job;    }}

Diningtable

package com.stulzl.mhl.domain;///这是javabean 与diningtable相对应  863public class DiningTable {    private Integer id;    private String state;    private String orderName;    private String orderTel;    public DiningTable() {    }    public DiningTable(Integer id, String state, String orderName, String orderTel) {        this.id = id;        this.state = state;        this.orderName = orderName;        this.orderTel = orderTel;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getState() {        return state;    }    public void setState(String state) {        this.state = state;    }    public String getOrderName() {        return orderName;    }    public void setOrderName(String orderName) {        this.orderName = orderName;    }    public String getOrderTel() {        return orderTel;    }    public void setOrderTel(String orderTel) {        this.orderTel = orderTel;    }    @Override    public String toString() {        return id + "\t\t\t" + state;    }}

Menu菜单

package com.stulzl.mhl.domain;///这一类是javabean  对应menu表  866public class Menu {    private Integer id;    private String name;    private String type;    private Double price;    public Menu() {    }    public Menu(Integer id, String name, String type, Double price) {        this.id = id;        this.name = name;        this.type = type;        this.price = price;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public Double getPrice() {        return price;    }    public void setPrice(Double price) {        this.price = price;    }    @Override    public String toString() {        return id + "\t\t" + name + "\t\t" + type + "\t\t" + price;    }}

Bill账单

package com.stulzl.mhl.domain;import java.util.Date;///这是javabean 对应bill表  868public class Bill {    private Integer id;    private String billId;    private Integer menuId;    private Integer nums;    private Double money;    private Integer diningTableId;    private Date billDate;    private String state;    public Bill() {    }    public Bill(Integer id, String billId, Integer menuId, Integer nums            , Double money, Integer diningTableId, Date billDate, String state) {        this.id = id;        this.billId = billId;        this.menuId = menuId;        this.nums = nums;        this.money = money;        this.diningTableId = diningTableId;        this.billDate = billDate;        this.state = state;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getBillId() {        return billId;    }    public void setBillId(String billId) {        this.billId = billId;    }    public Integer getMenuId() {        return menuId;    }    public void setMenuId(Integer menuId) {        this.menuId = menuId;    }    public Integer getNums() {        return nums;    }    public void setNums(Integer nums) {        this.nums = nums;    }    public Double getMoney() {        return money;    }    public void setMoney(Double money) {        this.money = money;    }    public Integer getDiningTableId() {        return diningTableId;    }    public void setDiningTableId(Integer diningTableId) {        this.diningTableId = diningTableId;    }    public Date getBillDate() {        return billDate;    }    public void setBillDate(Date billDate) {        this.billDate = billDate;    }    public String getState() {        return state;    }    public void setState(String state) {        this.state = state;    }    @Override    public String toString() {        return  id +                "\t\t" + menuId +                "\t\t\t" + nums +                "\t\t\t" + money +                "\t" + diningTableId +                "\t\t" + billDate +                "\t\t" + state;    }}

多表查询Multitablebeane

package com.stulzl.mhl.domain;import java.util.Date;//这个javabean可以与多张表相映射,用于多表查询    873public class MultiTableBean {    private Integer id;    private String billId;    private Integer menuId;    private Integer nums;    private Double money;    private Integer diningTableId;    private Date billDate;    private String state;    ///添加menu表的name字段   873    private String name;    ///添加menu表中的price  874    private Double price;    public MultiTableBean() {    }    public MultiTableBean(Integer id, String billId, Integer menuId, Integer nums,                          Double money, Integer diningTableId, Date billDate,                          String state, String name, Double price) {        this.id = id;        this.billId = billId;        this.menuId = menuId;        this.nums = nums;        this.money = money;        this.diningTableId = diningTableId;        this.billDate = billDate;        this.state = state;        this.name = name;        this.price = price;    }    ///给price生成setter和getter方法  874    public Double getPrice() {        return price;    }    public void setPrice(Double price) {        this.price = price;    }    ///给name  setter和getter方法   873    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getBillId() {        return billId;    }    public void setBillId(String billId) {        this.billId = billId;    }    public Integer getMenuId() {        return menuId;    }    public void setMenuId(Integer menuId) {        this.menuId = menuId;    }    public Integer getNums() {        return nums;    }    public void setNums(Integer nums) {        this.nums = nums;    }    public Double getMoney() {        return money;    }    public void setMoney(Double money) {        this.money = money;    }    public Integer getDiningTableId() {        return diningTableId;    }    public void setDiningTableId(Integer diningTableId) {        this.diningTableId = diningTableId;    }    public Date getBillDate() {        return billDate;    }    public void setBillDate(Date billDate) {        this.billDate = billDate;    }    public String getState() {        return state;    }    public void setState(String state) {        this.state = state;    }    @Override    public String toString() {        return  id +                "\t\t" + menuId +                "\t\t\t" + nums +                "\t\t\t" + money +                "\t" + diningTableId +                "\t\t" + billDate +                "\t\t" + state +                "\t\t" + name +                "\t\t" + price;    }}

工具类UtilsJDBCUtils_Druid

package com.stulzl.mhl.utils;import com.alibaba.druid.pool.DruidDataSourceFactory;import javax.sql.DataSource;import java.io.FileInputStream;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;///这是基于druid的数据库连接池的工具类  845//将 JDBCUtils 工具类改成 Druid(德鲁伊)实现public class JDBCUtils_Druid {    private static DataSource ds;    ////在静态代码块中完成ds的初始化    static{        Properties properties = new Properties();        try {            properties.load(new FileInputStream("src\\druid.properties"));            ds = DruidDataSourceFactory.createDataSource(properties);        } catch (Exception e) {            e.printStackTrace();        }    }    ///编写getconection方法    public static Connection getConnection() throws SQLException {        return ds.getConnection();    }    //关闭连接, 再次强调: 在数据库连接池技术中,close 并非真的断开了连接    //使用它 Connection 对象放回连接池    public static void close(ResultSet resultSet, Statement statement, Connection connection){        try {            if (resultSet != null) {                resultSet.close();            }            if (statement != null) {                statement.close();            }            if (connection != null) {                statement.close();            }            if (connection != null) {                connection.close();            }        } catch (SQLException e) {            throw new RuntimeException(e);        }    }}

Utility

package com.stulzl.mhl.utils;/**工具类功能:处理各种情况的用户输入,并能根据程序员的需要获得用户控制台输入。*/import java.util.*;/***/public class Utility {///静态属性。。。    private static Scanner scanner = new Scanner(System.in);        /**     * 功能:读取键盘输入的菜单选项,值:1-5范围     * @return 1——5     */public static char readMenuSelection() {        char c;        for (; ; ) {            String str = readKeyBoard(1, false);////字符串包含一个字符            c = str.charAt(0);///将字符串转换为字符char类型            if (c != '1' && c != '1' && c != '2' &&                 c != '3' && c != '4' && c != '5') {                System.out.print(”选错,请重新输入:");            } else break;        }        return c;    }/** * 功能:读取键盘输入的字符 * @return 一个字符 */    public static char readChar() {        String str = readKeyBoard(1, false);//是一个字符        return str.charAt(0);    }    /**     * 功能:读取键盘输入的字符,如果直接按回车,返回指定的默认值;否则,返回输入的字符     * @param defaultValue 指定的默认值     * @return 默认值或输入字符     */        public static char readChar(char defaultValue) {        String str = readKeyBoard(1, true);//要么是空字符串,要么是字符串        return (str.length() == 0) ? defaultValue : str.charAt(0);    }    /**     * 功能:读取键盘输入的整形,长度小于2位     * @return 整数     */    public static int readInt() {        int n;        for (; ; ) {            String str = readKeyBoard(2, false);//一个整数,长度<=2位            try {                n = Integer.parseInt(str);///将字符串转换成整数                break;            } catch (NumberFormatException e) {                System.out.print()数字输入错误,请重新输入:");            }        }        return n;    }    /**     * 功能:读取键盘输入 如果整数或默认值直接返回汽车,则返回默认值,否则,返回输入的整数     * @param defaultValue 指定的默认值     * @return 整数或默认值     */    public static int readInt(int defaultValue) {        int n;        for (; ; ) {            String str = readKeyBoard(10, true);            if (str.equals("")) {                return defaultValue;            }//异常处理...            try {                n = Integer.parseInt(str);                break;            } catch (NumberFormatException e) {                System.out.print()数字输入错误,请重新输入:");            }        }        return n;    }    /**     * 功能:读取键盘输入字符串指定长度     * @param limit 限制的长度     * @return 字符串指定长度     */    public static String readString(int limit) {        return readKeyBoard(limit, false);    }    /**     * 功能:读取键盘输入指定长度的字符串或默认值,如直接返回车辆,则返回默认值,否则,返回字符串     * @param limit 限制的长度     * @param defaultValue 指定的默认值     * @return 字符串指定长度     */    public static String readString(int limit, String defaultValue) {        String str = readKeyBoard(limit, true);        return str.equals("")? defaultValue : str;    }/** * 功能:读取键盘输入的确认选项,Y或N * 小功能,包装在一种方法中. * @return Y或N */    public static char readConfirmSelection() {        System.out.println(”确认是否预定(Y/N): ");        char c;        for (; ; ) {///无限循环        //在这里,将接收到的字符转换为大写字母        //y => Y n=>N            String str = readKeyBoard(1, false).toUpperCase();            c = str.charAt(0);            if (c == 'Y' || c == 'N') {                break;            } else {                System.out.print(”选错,请重新输入:");            }        }        return c;    }    /**     * 功能: 读一个字符串     * @param limit 读取的长度     * @param blankReturn 假如是true ,表示 可读空字符串。      *   假如false表示 不能读空字符串。      *   假如false表示 不能读空字符串。     *  *若输入为空,或输入大于limit的长度,则提示重新输入。     * @return     */    private static String readKeyBoard(int limit, boolean blankReturn) {        ///定义字符串String line = "";//scanner.hasNextLine() 判断是否有下一行        while (scanner.hasNextLine()) {            line = scanner.nextLine();///阅读这一行           //如果line.length=0, 也就是说,用户直接返回if,而不输入任何内容 (line.length() == 0) {                if (blankReturn) return line;/如果blankreturn=true,可返回空串                else continue; /如果blankreturn=false,不接受空串,必须输入内容            }///如果用户输入的内容大于 limit,提示重写输入  //如果用户的内容如此 >0 <= limit ,我就接受            if (line.length() < 1 || line.length() > limit) {                System.out.print(”输入长度(不能大于” + limit + ")错误,请重新输入:");                continue;            }            break;        }        return line;    }}

E中的数据库代码:java学习\初级\course174db_满汉楼mhl

-- CREATEEEE创建满汉楼数据库 DATABASE mhl-- 创建表employee表(主键id,empId,name,pwd,job等)   CREATEEE用户表862# TABLE employee (id INT PRIMARY KEY AUTO_INCREMENT, #empid自增 VARCHAR(50) UNIQUE NOT NULL DEFAULT #员工号pwd, CHAR(32) NOT NULL DEFAULT #密码md5NAME VARCHAR(50) NOT NULL DEFAULT '',#姓名job VARCHAR(50) NOT NULL DEFAULT '' #岗位)CHARSET=utf8; -- 添加数据INSERT INTO employee VALUES(NULL, '6668612', MD5(123456) "张三丰", '经理');INSERT INTO employee VALUES(NULL, '6668622', “小龙女”,MD5(123456) "服务员";INSERT INTO employee VALUES(NULL, '6668633', MD5(123456) 张无忌, "收银员";INSERT INTO employee VALUES(NULL, '666', MD5(123456) '老韩', '经理');SELECT * FROM employeeSELECT * FROM employee WHERE empId=666 AND pwd=MD5(123456)-- 创建dingtable 表(id,state,orderName,orderTel...)   863#餐桌表CRETEEETE TABLE diningTable (id INT PRIMARY KEY AUTO_INCREMENT, #自增, 表示餐桌号statetete表示餐桌编号 VARCHAR(20) NOT NULL DEFAULT '',#餐桌的状态ordername VARCHAR(50) NOT NULL DEFAULT '',#预订人的名字ordertel VARCHAR(20) NOT NULL DEFAULT '')CHARSET=utf8; -- INSERT测试数据 INTO diningTable VALUES(NULL, ",","'');INSERT INTO diningTable VALUES(NULL, "",",";INSERT INTO diningTable VALUES(NULL, "",",";SELECT * FROM diningTableSELECT id, state FROM diningTableSELECT * FROM diningTable WHERE id=1UPDATE diningTable SET state='空',orderName='',orderTel='' WHERE id=1-- 创建menu表(id,name,type,price)   CREETEE66#菜谱 TABLE menu (id INT PRIMARY KEY AUTO_INCREMENT, #自增主键,作为菜谱号(唯一)NAME VARCHAR(50) NOT NULL DEFAULT '',#菜名TYPE VARCHAR(50) NOT NULL DEFAULT '', #菜品类型price DOUBLE NOT NULL DEFAULT 0#价格)CHARSET=utf8; INSERT INTO menu VALUES(NULL, 八宝饭, 主食类, 10);INSERT INTO menu VALUES(NULL, "叉烧包", 主食类, 20);INSERT INTO menu VALUES(NULL, 宫保鸡丁, 热菜类, 30);INSERT INTO menu VALUES(NULL, 山药拨鱼, "凉菜类", 14);INSERT INTO menu VALUES(NULL, "银丝卷", 甜食类, 9);INSERT INTO menu VALUES(NULL, "水煮鱼", 热菜类, 26);INSERT INTO menu VALUES(NULL, "甲鱼汤", '汤类', 100);INSERT INTO menu VALUES(NULL, "鸡蛋汤", '汤类', 16);SELECT * FROM menuSELECT * FROM menu WHERE id = 1-- 增加表billlllll账单表(id, billId, menuId, nums, billDate, money, state, diningTableId)   867#账单流水, 考虑可以分开结账, 并考虑CREEATEEEE的销售情况,分别统计未来不同菜肴的销售情况 TABLE bill (id INT PRIMARY KEY AUTO_INCREMENT, #自增主键billid VARCHAR(50) NOT NULL DEFAULT #账单号可以按照自己的规则生成 UUIDmenuId INT NOT NULL DEFAULT 0、#菜品编号, 也可以使用外键numss SMALLINT NOT NULL DEFAULT 0,#分数money DOUBLE NOT NULL DEFAULT 0, #diningtabledidid金额 INT NOT NULL DEFAULT 0, #餐桌bildateldatet DATETIME NOT NULL ,#state订单日期 VARCHAR(50) NOT NULL DEFAULT '' # 状态 '未结账' , 已经结账了, "现金","支付宝",'坏账')CHARSET=utf8;SELECT * FROM bill-- 检查餐桌上是否有未结账的账单  871SELECT * FROM bill WHERE diningTableId=1 AND state='未结账' LIMIT 0,1-- 写sql语句,您可以返回菜名和账单信息  873SELECT bill.*,NAMEFROM bill,menuWHERE bill.menuId=menu.Id

druid配置文件.properties

#key=valuedriverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mhl?rewriteBatchedStatements=trueusername=rootpassword=lzl#initial connection SizeinitialSize=10#min idle connecton sizeminIdle=5#max active connection sizemaxActive=50#max wait time (5000 mil seconds)maxWait=5000

三个jar文件包