Java实战之JDBC + swing + MySQL:超市管理系统
文章目录超市管理系统前言系统设计要求系统用户管理模块商品管理模块商品分类管理系统商品结算模块订单管理模块进货管理模块设计思路JDBC部分GUI部分前后端对接部分界面展示部分参考代码商品信息表DAO模式实现commodify类commodifyDAO接口commodifyDAOimpl类主界面绘制及监听器全部代码下载超市管理系统前言完成java程序设计基础与java高级程序设计这两门课程,决定做一个
超市管理系统
前言
完成java程序设计基础与java高级程序设计这两门课程,决定做一个本地的超市管理系统进行练习,本系统基于JDBC与swing框架,JDBC负责存储系统的数据,swing负责绘制操作界面
系统设计要求
系统用户管理模块
此模块可对系统操作用户信息进行增、删、改、查操作,统一管理登陆系统的操作用户信息。在系统初始化时提供用户名为admin,密码为admin的系统默认用户,在添加用户信息时需要填写登陆账号(登陆账号唯一、不可重复)、设置登陆密码、真实姓名及用户电话,并且选择用户角色(管理员、收银员、库管员)。
商品管理模块
管理员或库管员有权限操作商品管理模块,维护商品信息,对商品信息进行增、删、改、查操作。商品信息包括但不限于商品名称、选择商品分类、展示图集、商品详情、商品价格、库存量、上架下架状态等。已产生订单的商品信息不可彻底删除,需伪删除处理。
商品分类管理系统
管理员或库管员有权限操作商品分类管理模块,对分类信息进行增、删、改、查等操作,分类下有商品信息的分类信息不可删除。分类信息包括但不限于分类名称、展示图片、分类状态等。
商品结算模块
管理员或收银员有权限进行商品结算处理,在系统中选择用户所购商品、填写各商品购买数量等信息,创建商品销售订单,订单单号唯一,订单中需记录订单操作员、操作时间等信息。
订单管理模块
管理员或收银员有权限进行订单查看,可按订单创建时间、订单单号等信息进行订单信息查询。
进货管理模块
管理员或库管员有权限处理进货操作,选择所进商品信息,进货数量,创建进货批次订单(正常批次订单号为进货日期时间),提交进货订单后,所选的进货商品库存量应自动更新。
设计思路
JDBC部分
利用JDBC的DAO模式完成商品信息、分类信息、订单信息、用户信息和进货管理五个数据库的设计与操作
GUI部分
利用swing框架的组件,完成操作界面的绘制,为按钮与文本框添加事件监听器,监听用户的输入与操作
前后端对接
在事件监听器中调用DAO的实现方法来实现对数据表的增删改查操作
部分界面展示
部分参考代码
商品信息表DAO模式实现
commodify类
public class commodify {
private int id;
private String name;
private String type;
private String detail;
private double price;
private int num;
private String statue;
public commodify() {
}
public commodify(int id, String name, String type, String detail, double price, int num, String statue) {
super();
this.id = id;
this.name = name;
this.type = type;
this.detail = detail;
this.price = price;
this.num = num;
this.statue = statue;
}
public int getId() {
return id;
}
public void setId(int 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 String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getStatue() {
return statue;
}
public void setStatue(String statue) {
this.statue = statue;
}
@Override
public String toString() {
return "商品编号[" + id + "] 商品名称[" + name + "] 商品类型[" + type + "] 商品详情[" + detail + "] 商品价格[" + price + "] 商品库存[" + num + "] 上架状态[" + statue + "]";
}
}
commodifyDAO接口
import java.util.Vector;
public interface commodifyDAO {
public boolean AddCommodify(commodify commodify);
public boolean UpdateCommodify(commodify commodify);
public boolean DeleteCommodify(int id);
public Vector<commodify> selectById(int id);
public Vector<commodify> selectByName(String name);
public Vector<commodify> selectAll();
public boolean FakedeleteCommodify(int id);
public int selectNum(int id);
public boolean updateNum(int num, int id);
public Vector<commodify> selectByNameandID(int id,String name);
public Vector<commodify> selectBySort(String sort);
}
commodifyDAOimpl类
package supermarket.commodify;
import supermarket.*;
import java.sql.*;
import java.util.*;
public class commodifyDAOImpl implements commodifyDAO{
@Override
public boolean AddCommodify(commodify commodify) {
Connection conn = null;
PreparedStatement pst = null;
int num = 0;
try {
conn = ConnectionFactory.getConnection();
String sql = "insert into commodify values(?,?,?,?,?,?,?,1)";
pst = conn.prepareStatement(sql);
pst.setInt(1,commodify.getId());
pst.setString(2,commodify.getName());
pst.setString(3,commodify.getType());
pst.setString(4,commodify.getDetail());
pst.setDouble(5,commodify.getPrice());
pst.setInt(6,commodify.getNum());
pst.setString(7,commodify.getStatue());
num = pst.executeUpdate();
if(num == 1)
{
return true;
}
else
{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
CloseResource.close(pst,conn);
}
}
@Override
public boolean UpdateCommodify(commodify commodify) {
Connection conn = null;
PreparedStatement pst = null;
int num = 0;
try {
conn = ConnectionFactory.getConnection();
String sql = "update commodify set name = ?,type = ?, detail = ?,price = ?,num = ?,statue = ? where id = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setInt(7,commodify.getId());
pst.setString(1,commodify.getName());
pst.setString(2,commodify.getStatue());
pst.setString(3,commodify.getDetail());
pst.setDouble(4,commodify.getPrice());
pst.setInt(5,commodify.getNum());
pst.setString(6,commodify.getStatue());
num = pst.executeUpdate();
if(num == 1)
{
return true;
}
else
{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
CloseResource.close(pst,conn);
}
}
@Override
public boolean DeleteCommodify(int id) {
Connection conn = null;
PreparedStatement pst = null;
int num = 0;
try {
conn = ConnectionFactory.getConnection();
String sql = "delete from commodify where id = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
num = pst.executeUpdate();
if(num == 1)
{
return true;
}
else
{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
CloseResource.close(pst,conn);
}
}
@Override
public Vector<commodify> selectById(int id) {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
commodify commodify = null;
Vector<commodify> vector = new Vector<commodify>();
try {
conn = ConnectionFactory.getConnection();
String sql = "select * from commodify where id = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
rs = pst.executeQuery();
while(rs.next())
{
commodify = new commodify();
commodify.setId(rs.getInt(1));
commodify.setName(rs.getString(2));
commodify.setType(rs.getString(3));
commodify.setDetail(rs.getString(4));
commodify.setPrice(rs.getDouble(5));
commodify.setNum(rs.getInt(6));
commodify.setStatue(rs.getString(7));
vector.add(commodify);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseResource.close(rs,pst,conn);
}
return vector;
}
@Override
public Vector<commodify> selectByName(String name) {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
commodify commodify = null;
Vector<commodify> vector = new Vector<commodify>();
try {
conn = ConnectionFactory.getConnection();
String sql = "select * from commodify where name = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setString(1,name);
rs = pst.executeQuery();
while(rs.next())
{
commodify = new commodify();
commodify.setId(rs.getInt(1));
commodify.setName(rs.getString(2));
commodify.setType(rs.getString(3));
commodify.setDetail(rs.getString(4));
commodify.setPrice(rs.getDouble(5));
commodify.setNum(rs.getInt(6));
commodify.setStatue(rs.getString(7));
vector.add(commodify);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseResource.close(rs,pst,conn);
}
return vector;
}
@Override
public Vector<commodify> selectAll() {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
commodify commodify = null;
Vector<commodify> vector = new Vector<commodify>();
try {
conn = ConnectionFactory.getConnection();
String sql = "select * from commodify and visible = 1";
st = conn.createStatement();
rs = st.executeQuery(sql);
while(rs.next())
{
commodify = new commodify();
commodify.setId(rs.getInt(1));
commodify.setName(rs.getString(2));
commodify.setType(rs.getString(3));
commodify.setDetail(rs.getString(4));
commodify.setPrice(rs.getDouble(5));
commodify.setNum(rs.getInt(6));
commodify.setStatue(rs.getString(7));
vector.add(commodify);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseResource.close(rs,st,conn);
}
return vector;
}
@Override
public boolean FakedeleteCommodify(int id) {
Connection conn = null;
PreparedStatement pst = null;
int num = 0;
try {
conn = ConnectionFactory.getConnection();
String sql = "update commodify set visible = 0 where id = ?";
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
num = pst.executeUpdate();
if(num == 1)
{
return true;
}
else
{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
CloseResource.close(pst,conn);
}
}
@Override
public int selectNum(int id) {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
commodify commodify = null;
int store = 0;
try {
conn = ConnectionFactory.getConnection();
String sql = "select num from commodify where id = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
rs = pst.executeQuery();
while(rs.next())
{
store = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseResource.close(rs,pst,conn);
}
return store;
}
@Override
public boolean updateNum(int num, int id) {
Connection conn = null;
PreparedStatement pst = null;
int n = 0;
try {
conn = ConnectionFactory.getConnection();
String sql = "update commodify set num = ? where id = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setInt(1,num);
pst.setInt(2,id);
n = pst.executeUpdate();
if(n == 1)
{
return true;
}
else
{
return false;
}
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
CloseResource.close(pst,conn);
}
}
@Override
public Vector<commodify> selectByNameandID(int id, String name) {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
commodify commodify = null;
Vector<commodify> vector = new Vector<commodify>();
try {
conn = ConnectionFactory.getConnection();
String sql = "select * from commodify where id = ? and name = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setInt(1,id);
pst.setString(2,name);
rs = pst.executeQuery();
while(rs.next())
{
commodify = new commodify();
commodify.setId(rs.getInt(1));
commodify.setName(rs.getString(2));
commodify.setType(rs.getString(3));
commodify.setDetail(rs.getString(4));
commodify.setPrice(rs.getDouble(5));
commodify.setNum(rs.getInt(6));
commodify.setStatue(rs.getString(7));
vector.add(commodify);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseResource.close(rs,pst,conn);
}
return vector;
}
@Override
public Vector<commodify> selectBySort(String sort) {
Connection conn = null;
PreparedStatement pst = null;
ResultSet rs = null;
commodify commodify = null;
Vector<commodify> vector = new Vector<commodify>();
try {
conn = ConnectionFactory.getConnection();
String sql = "select * from commodify where type = ? and visible = 1";
pst = conn.prepareStatement(sql);
pst.setString(1,sort);
rs = pst.executeQuery();
while(rs.next())
{
commodify = new commodify();
commodify.setId(rs.getInt(1));
commodify.setName(rs.getString(2));
commodify.setType(rs.getString(3));
commodify.setDetail(rs.getString(4));
commodify.setPrice(rs.getDouble(5));
commodify.setNum(rs.getInt(6));
commodify.setStatue(rs.getString(7));
vector.add(commodify);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseResource.close(rs,pst,conn);
}
return vector;
}
}
主界面绘制及监听器
import supermarket.user.UserDAOimpl;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class mainframe {
JFrame mainframe = new JFrame("超市管理系统");
JTextField user = new JTextField();
JPasswordField pwd = new JPasswordField();
JLabel title_I = new JLabel("超市管理系统");
JButton register = new JButton("注册");
JButton logon = new JButton("登录");
JLabel user_I = new JLabel("用户名");
JLabel pwd_I = new JLabel("密 码");
public void Main()
{
pwd.setEchoChar('*');
user_I.setBounds(51,100,50,30);
user.setBounds(111,100,240,30);
pwd.setBounds(111,150,240,30);
pwd_I.setBounds(51,150,50,30);
register.setBounds(111,210,75,30);
register.addActionListener(new registerButton());
logon.addActionListener(new logonButton(user, pwd));
logon.setBounds(267,210,75,30);
title_I.setBounds(150,50,120,30);
title_I.setFont(new Font("微软雅黑",Font.BOLD,20));
mainframe.setBounds(744,374,429,359);
mainframe.setResizable(false);
mainframe.setLayout(null);
mainframe.setVisible(true);
mainframe.add(title_I);
mainframe.add(user);
mainframe.add(pwd);
mainframe.add(pwd_I);
mainframe.add(user_I);
mainframe.add(register);
mainframe.add(logon);
mainframe.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private class registerButton implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e) {
mainframe.dispose();
new registerframe().Register();
}
}
private class logonButton implements ActionListener
{
private JTextField userid = new JTextField();
private JTextField userpwd = new JTextField();
public logonButton(JTextField userid, JTextField userpwd) {
this.userid = userid;
this.userpwd = userpwd;
}
@Override
public void actionPerformed(ActionEvent e) {
JDialog comp = new JDialog();
JButton sure = new JButton("确定");
JLabel tip = new JLabel("用户未注册");
sure.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
comp.dispose();
}
});
sure.setBounds(60,80,80,40);
tip.setFont(new Font("微软雅黑",Font.PLAIN,17));
tip.setBounds(50,20,100,40);
comp.setLayout(null);
comp.setResizable(false);
comp.setBounds(859, 474, 200, 160);
comp.add(sure);
comp.add(tip);
String id = userid.getText();
String pwd = userpwd.getText();
UserDAOimpl ud = new UserDAOimpl();
String pwd_1 = ud.logon(id);
if(pwd_1==null)
{
tip.setText("用户未注册");
comp.setVisible(true);
}
else
{
if(pwd.equals(pwd_1))
{
String role = ud.prove(id);
switch (role)
{
case "管理员":
new adminframe().admin();
break;
case "库管员":
new storeframe().store();
break;
case "收银员":
new accounterframe().accounter();
break;
}
mainframe.dispose();
}
else
{
tip.setText("密码错误");
comp.setVisible(true);
}
}
}
}
}
全部代码下载
百度网盘链接:https://pan.baidu.com/s/1Kl9nO-SkfS9nsk709IHS0g
提取码:gdp6
百度网盘的sql文件有点问题,需要在表orders最前面添加一列int类型的cid
Git仓库的是修复完成的版本
克隆地址:https://gitcode.net/qq_45886144/supermarket.git
更多推荐
所有评论(0)