👨🎓作者简介:一位喜欢写作,计科专业大三菜鸟
🏡个人主页:starry陆离
如果文章有帮到你的话记得点赞👍+收藏💗支持一下哦
『Java课设』JavaSwing+MySQL实现医院智慧点餐系统
1.功能介绍2.项目包结构3.数据库设计4.JDBC数据库连接类5.登录选择界面(系统入口)6.管理员页面7.用户选餐界面1.功能介绍
这个是大二的Java的课程设计的完整版,功能相对之前发的半成品稍稍丰富些。一开始我们还是按照MVC结构写的,写着写着就视图,用户操作逻辑,数据库数据操作就都写在一起了,比如管理员操作界面(View层)就嵌入了SQL语句直接操作数据库了,好在是一个学习过程。
系统角色分为用户与管理员
用户登录系统可以选购餐品并支付
管理员可以登陆系统管理所有的菜品(增删改查)
管理所有的订单信息(未完成)
2.项目包结构
3.数据库设计
数据库设计了5个表,分别是用户信息表,管理员信息表,菜品信息表,订单信息表,订单详情表
4.JDBC数据库连接类
不同数据库的连接方式的大同小异,根据开发场景灵活变动就好,我这里用的MySQL8.0所以使用如下两行配置。
private static final String USER = "root";//数据库用户名,一般是rootprivate static final String PWD = "lx0411";//数据库密码//连接mysql5.7//private static final String URL="jdbc:mysql://localhost:3306/studentgrade";//private static final String DRIVER="com.mysql.jdbc.Driver"; //mysql5.7//如果用的是mysql8.0,private static final String DRIVER="com.mysql.cj.jdbc.Driver";private static final String URL= "jdbc:mysql://localhost:3306/catersystem?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true"; //如果用的sqlServer数据库连接//private static final String URL="jdbc:sqlserver://localhost:1433;DataBaseName=StudentGrade";//private static final String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";//private static final String USER="sa";//private static final String PWD="lxloveth0411";
5.登录选择界面(系统入口)
package view;import java.awt.AWTException;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import java.awt.SystemTray;import java.awt.TrayIcon;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import .URL;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import handler.LoginChoiceHandler;public class LoginChoiceView extends JFrame{/*** */private static final long serialVersionUID = 1L;JLabel titleNameLabel=new JLabel("医院智慧选餐系统",JLabel.CENTER);JPanel jp1=new JPanel();JPanel jp2=new JPanel(new GridLayout(5,1));JButton userBtn=new JButton ("用户 登录");JButton adminBtn=new JButton("管理员登录");//声明桌面系统托盘SystemTraypublic SystemTray systemTray;public TrayIcon trayIcon;LoginChoiceHandler loginChoiceHandler;public LoginChoiceView() {super("医院智慧选餐系统");loginChoiceHandler=new LoginChoiceHandler(this);//设置标题的字体,格式,大小titleNameLabel.setFont(new Font("华文行楷",Font.PLAIN,40));titleNameLabel.setPreferredSize(new Dimension(0,80));Font centerFont=new Font("楷体",Font.PLAIN,20);userBtn.setFont(centerFont);adminBtn.setFont(centerFont);jp1.add(userBtn);jp1.add(new JPanel());jp1.add(adminBtn);jp2.add(new JPanel());jp2.add(titleNameLabel);jp2.add(new JPanel());jp2.add(jp1);jp2.add(new JPanel());this.add(jp2);userBtn.addActionListener(loginChoiceHandler);adminBtn.addActionListener(loginChoiceHandler);//判断当前系统是否支持系统托盘if(SystemTray.isSupported()){systemTray=SystemTray.getSystemTray();//初始化系统托盘URL imgUrl = UserLoginView.class.getClassLoader().getResource("LoginView.jpg");trayIcon=new TrayIcon(new ImageIcon(imgUrl).getImage());//初始化托盘图标trayIcon.setImageAutoSize(true);//托盘图标可以自动缩放大小try {systemTray.add(trayIcon);//将托盘图标加入到系统托盘组件中,并抛出异常} catch (AWTException e) {e.printStackTrace();}//创建一个事件响应,最小化时销毁资源this.addWindowListener(new WindowAdapter() {@Overridepublic void windowIconified(WindowEvent e) {LoginChoiceView.this.dispose();}});//托盘事件监听(鼠标单击一次托盘,窗口将会正常显示)trayIcon.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {int clickCont=e.getClickCount();if(clickCont==1){LoginChoiceView.this.setExtendedState(JFrame.NORMAL);}LoginChoiceView.this.setVisible(true);}});}//设置窗口图片URL imgUrl=UserLoginView.class.getClassLoader().getResource("LoginView.jpg");setIconImage(new ImageIcon(imgUrl).getImage());//设置窗口基本参数setSize(600,400);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);setResizable(false);setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew LoginChoiceView();}}
6.管理员页面
package view;import javax.swing.*;import ext.MainViewTable;import ext.MainViewTableModel;import handler.AdminMainHandler;import req.UserRequest;import res.TableDTO;import service.FoodService;import service.FoodServiceImpl;import util.DBUtil;import util.DimensionUtil;import java.awt.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.Vector;public class AdminOperateFrame extends JFrame {/*** */private static final long serialVersionUID = 1L;String adminName;String adminId;public String lastPressKey;JLabel adminNameLabel = new JLabel("管理员名:", JLabel.CENTER);JPanel centerPanel = new JPanel(new BorderLayout());JPanel panel1 = new JPanel();JPanel panel2 = new JPanel();JPanel panel3 = new JPanel();JPanel panel4 = new JPanel();//增删改查按钮public JButton btnAdd = new JButton("增加菜品");public JButton btnSave = new JButton("保存菜品");JButton btnDelete = new JButton("删除菜品");JButton btnAlter = new JButton("更改菜品");JButton btnSearch_Food = new JButton("查看菜品");//JButton btnSearch_Order = new JButton("查看订单");//设置字体大小Font font=new Font("楷体",Font.PLAIN,20);Dimension dimension = new Dimension(130,30);//创建一个MainViewTable对象public MainViewTable mianViewTable=new MainViewTable();public MainViewTableModel mainViewTableModel;//创建一个MainOrderTable订单table对象public MainViewTable mianOrderTable=new MainViewTable();public MainViewTableModel mainOrderTableModel;//声明一个MainHandler对象AdminMainHandler adminMainHandler;private int pageNow=1;//默认显示第一页private int pageSize=10;//默认每页显示十条记录//搜索框与搜索按钮public JTextField searchField = new JTextField();JButton searchBt = new JButton("搜索");JButton exitBt = new JButton("退出");public AdminOperateFrame(String adminId) {setTitle("医院营养配餐系统");this.adminId=adminId;adminMainHandler = new AdminMainHandler(this);layNorth(adminId);layCenter();layEast();laySouth();centerPanel.add(panel1, BorderLayout.NORTH);centerPanel.add(panel2, BorderLayout.CENTER);centerPanel.add(panel3, BorderLayout.EAST);centerPanel.add(panel4, BorderLayout.SOUTH);add(centerPanel);init();}private void init() {setSize(600, 400);setLocationRelativeTo(null);setBounds(DimensionUtil.getBounds());//设置窗体完全充满整个屏幕setExtendedState(JFrame.MAXIMIZED_BOTH);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}private void laySouth() {exitBt.setFont(font);exitBt.setPreferredSize(new Dimension(200,50));panel4.add(exitBt);exitBt.addActionListener(adminMainHandler);}private void layEast() {panel3.setLayout(new FlowLayout());panel3.setPreferredSize(new Dimension(200,100));btnAdd.setFont(font);btnAdd.setPreferredSize(dimension);btnSave.setFont(font);btnSave.setPreferredSize(dimension);btnDelete.setFont(font);btnDelete.setPreferredSize(dimension);btnAlter.setFont(font);btnAlter.setPreferredSize(dimension);btnSearch_Food.setFont(font);btnSearch_Food.setPreferredSize(dimension);//btnSearch_Order.setFont(font);//btnSearch_Order.setPreferredSize(dimension);panel3.add(btnAdd);panel3.add(btnSave);btnSave.setVisible(false);panel3.add(btnDelete);panel3.add(btnAlter);panel3.add(btnSearch_Food);//panel3.add(btnSearch_Order);btnAdd.addActionListener(adminMainHandler);btnSave.addActionListener(adminMainHandler);btnDelete.addActionListener(adminMainHandler);btnAlter.addActionListener(adminMainHandler);btnSearch_Food.addActionListener(adminMainHandler);//btnSearch_Order.addActionListener(adminMainHandler);}private void layNorth(String adminId) {panel1.setLayout(new BorderLayout());selectAdminName(adminId);adminNameLabel.setText("管理员名:"+adminName);adminNameLabel.setFont(font);searchField.setFont(font);searchField.setPreferredSize(dimension);searchBt.setFont(font);searchBt.setPreferredSize(new Dimension(200,50));panel1.add(adminNameLabel, BorderLayout.WEST);panel1.add(searchField, BorderLayout.CENTER);panel1.add(searchBt, BorderLayout.EAST);searchBt.addActionListener(adminMainHandler);}private void layCenter() {//调用数据库数据,创建一个FoodService和UserRequest实例//获取从数据库中查询到的data与totalCountFoodService foodService=new FoodServiceImpl();UserRequest request=new UserRequest();request.setPageNow(pageNow);request.setPageSize(pageSize);request.setSearchKey(searchField.getText().trim());TableDTO tableDTO = foodService.retrieveFoods(request);Vector<Vector<Object>> data = tableDTO.getData();//自定义的MainViewTableModelmainViewTableModel=MainViewTableModel.assembleModel(data);//将Table与TableModel关联mianViewTable.setModel(mainViewTableModel);//设置渲染方式mianViewTable.renderRule();JScrollPane jScrollPane = new JScrollPane(mianViewTable);panel2.setLayout(new BorderLayout());panel2.add(jScrollPane, BorderLayout.CENTER);}private void selectAdminName(String adminId) {String sql = "select manaName from managerInfo where manaId = ?";Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {//连接数据库,获取数据库连接,getConn()方法已经提前在DBUtil中写好conn = DBUtil.getConn();System.out.println("数据库连接成功");//如果连接失败,则返回falseif(conn==null) {System.out.println("数据库连接失败");}ps = conn.prepareStatement(sql);ps.setString(1, adminId);rs = ps.executeQuery();//查询遍历while(rs.next()) {adminName = rs.getString(1);}} catch (Exception e) {e.printStackTrace();}finally {//关闭资源DBUtil.closeRs(rs);DBUtil.closeConn(conn);DBUtil.closePs(ps);}}public void setPageNow(int pageNow) {this.pageNow = pageNow;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getPageNow() {return pageNow;}public void reloadTable() {/**调用数据库数据,创建一个FoodService和UserRequest实例*获取从数据库中查询到的data与totalCount*查询完后要更新Model的数据*/FoodService foodService=new FoodServiceImpl();UserRequest request=new UserRequest();request.setPageNow(pageNow);request.setPageSize(pageSize);request.setSearchKey(searchField.getText().trim());TableDTO tableDTO = foodService.retrieveFoods(request);Vector<Vector<Object>> data = tableDTO.getData();// 更新调用updataModel实现更新ModelMainViewTableModel.uptadaModel(data);// 设置渲染方式mianViewTable.renderRule();}public static void main(String[] args) {new AdminOperateFrame("20");}}
7.用户选餐界面
室友在这里做了一个有趣的处理,点击支付的时候弹出他的微信收款码😁。如果用户想白嫖不支付就退出系统还会有窗口提示(当然并不是真正的需要支付money才能退出系统,你任务管理器一杀我们也没辙,只是考虑了这个场景就实现了这个逻辑)
package view;/** 待解决问题:* 1.关闭窗口提示还没支付* 2.同一用户可以选同一个菜品* 3.list没有实现更新* 4.table里的库存量没有实现更新* 5.没有选餐应提示不能实现支付*/import javax.swing.*;import ext.MainViewTable;import ext.MainViewTableModel;import handler.UserMainHandler;import req.UserRequest;import res.TableDTO;import service.FoodService;import service.FoodServiceImpl;import util.DBUtil;import util.DimensionUtil;import java.awt.*;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.Vector;public class UserSelectionFrame extends JFrame {/*** */private static final long serialVersionUID = 1L;public String userName;public int totalMoney;public int selId = 1;public String userId;JLabel userNameLabel = new JLabel("用户名:", JLabel.CENTER);JPanel centerPanel = new JPanel(new BorderLayout());JPanel panel1 = new JPanel();JPanel panel2 = new JPanel();JPanel panel3 = new JPanel();JPanel panel4 = new JPanel();JPanel panel5 = new JPanel();//创建一个MainViewTable对象public MainViewTable mianViewTable=new MainViewTable();public MainViewTableModel mainViewTableModel;//声明一个MainHandler对象UserMainHandler userMainHandler;private int pageNow=1;//默认显示第一页private int pageSize=10;//默认每页显示十条记录JButton btSelect = new JButton("选购");//搜索框与搜索按钮Font centerFont=new Font("楷体",Font.PLAIN,20);Dimension dimension = new Dimension(200,50);JTextField searchField = new JTextField();JButton searchBt = new JButton("搜索");public DefaultListModel<Object> listModel = new DefaultListModel<Object>();public JList<Object> selectedList = new JList<Object>(listModel);JLabel selectedLabel = new JLabel("已选菜品:",JLabel.CENTER);public JLabel totalPriceLabel = new JLabel("总价: 0");JButton payBt = new JButton("支付");public UserSelectionFrame(String userId) {setTitle("医院营养配餐系统");this.userId = userId;userMainHandler = new UserMainHandler(this);//根据用户Id查询出用户名userName,显示在userNameLabel上selectUesrName(userId);layNorth();layCenter();layWest();laySouth();init();}private void selectUesrName(String userId) {String sql = "select userName from userInfo where userId = ?";Connection conn=null;PreparedStatement ps=null;ResultSet rs=null;try {//连接数据库,获取数据库连接,getConn()方法已经提前在DBUtil中写好conn = DBUtil.getConn();System.out.println("数据库连接成功");//如果连接失败,则返回falseif(conn==null) {System.out.println("数据库连接失败");}ps = conn.prepareStatement(sql);ps.setString(1, userId);rs = ps.executeQuery();//查询遍历while(rs.next()) {userName = rs.getString(1);}} catch (Exception e) {e.printStackTrace();}finally {//关闭资源DBUtil.closeRs(rs);DBUtil.closeConn(conn);DBUtil.closePs(ps);}}private void init() {add(centerPanel);setSize(600, 400);setLocationRelativeTo(null);setBounds(DimensionUtil.getBounds());//设置窗体完全充满整个屏幕setExtendedState(JFrame.MAXIMIZED_BOTH);setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);//在关闭窗口的时候强制支付,应该改成提示“暂时还没支付”addWindowListener(new UserMainHandler(this));setVisible(true);}private void layNorth() {panel1.setLayout(new BorderLayout());userNameLabel.setText("用户名:"+userName);panel1.add(userNameLabel, BorderLayout.WEST);userNameLabel.setPreferredSize(dimension);userNameLabel.setFont(centerFont);panel1.add(searchField, BorderLayout.CENTER);searchField.setPreferredSize(dimension);searchField.setFont(centerFont);panel1.add(searchBt, BorderLayout.EAST);searchBt.setPreferredSize(new Dimension(200,50));searchBt.setFont(centerFont);centerPanel.add(panel1, BorderLayout.NORTH);searchBt.addActionListener(userMainHandler);}private void layCenter() {//调用数据库数据,创建一个FoodService和UserRequest实例//获取从数据库中查询到的data与totalCountFoodService foodService=new FoodServiceImpl();UserRequest request=new UserRequest();request.setPageNow(pageNow);request.setPageSize(pageSize);request.setSearchKey(searchField.getText().trim());TableDTO tableDTO = foodService.retrieveFoods(request);Vector<Vector<Object>> data = tableDTO.getData();showPreNext(tableDTO.getTotalCount());panel2.setLayout(new BorderLayout());//自定义的MainViewTableModelmainViewTableModel=MainViewTableModel.assembleModel(data);//将Table与TableModel关联mianViewTable.setModel(mainViewTableModel);//设置渲染方式mianViewTable.renderRule();JScrollPane jScrollPane = new JScrollPane(mianViewTable);panel2.add(jScrollPane,BorderLayout.CENTER);btSelect.setPreferredSize(new Dimension(150,50));btSelect.setFont(centerFont);panel5.add(btSelect);panel2.add(panel5, BorderLayout.EAST);centerPanel.add(panel2, BorderLayout.CENTER);btSelect.addActionListener(userMainHandler);}private void layWest() {panel3.setLayout(new BorderLayout());selectedList.setPreferredSize(dimension);selectedList.setFont(centerFont);panel3.add(selectedLabel, BorderLayout.NORTH);selectedLabel.setPreferredSize(dimension);selectedLabel.setFont(centerFont);panel3.add(selectedList, BorderLayout.CENTER);centerPanel.add(panel3, BorderLayout.WEST);}private void laySouth() {totalPriceLabel.setFont(centerFont);panel4.add(totalPriceLabel);payBt.setFont(centerFont);panel4.add(payBt);centerPanel.add(panel4, BorderLayout.SOUTH);payBt.addActionListener(userMainHandler);}private void showPreNext(int totalCount) {if(totalCount%pageSize==0) {}else {}}public void setPageNow(int pageNow) {this.pageNow = pageNow;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getPageNow() {return pageNow;}public void reloadTable() {/**调用数据库数据,创建一个FoodService和UserRequest实例*获取从数据库中查询到的data与totalCount*查询完后要更新Model的数据*/FoodService foodService=new FoodServiceImpl();UserRequest request=new UserRequest();request.setPageNow(pageNow);request.setPageSize(pageSize);request.setSearchKey(searchField.getText().trim());TableDTO tableDTO = foodService.retrieveFoods(request);Vector<Vector<Object>> data = tableDTO.getData();// 更新调用updataModel实现更新ModelMainViewTableModel.uptadaModel(data);// 设置渲染方式mianViewTable.renderRule();// 调用上一页与下一页按钮是否可见showPreNext(tableDTO.getTotalCount());} }{return pageNow;}public void reloadTable() {/**调用数据库数据,创建一个FoodService和UserRequest实例*获取从数据库中查询到的data与totalCount*查询完后要更新Model的数据*/FoodService foodService=new FoodServiceImpl();UserRequest request=new UserRequest();request.setPageNow(pageNow);request.setPageSize(pageSize);request.setSearchKey(searchField.getText().trim());TableDTO tableDTO = foodService.retrieveFoods(request);Vector<Vector<Object>> data = tableDTO.getData();// 更新调用updataModel实现更新ModelMainViewTableModel.uptadaModel(data);// 设置渲染方式mianViewTable.renderRule();// 调用上一页与下一页按钮是否可见showPreNext(tableDTO.getTotalCount());} }
如果觉得《『Java课设』JavaSwing+MySQL实现医院智慧点餐系统》对你有帮助,请点赞、收藏,并留下你的观点哦!