网络编程
位置:首页>> 网络编程>> 数据库>> 基于Mysql+JavaSwing的超市商品管理系统设计与实现

基于Mysql+JavaSwing的超市商品管理系统设计与实现

作者:java李杨勇  发布时间:2024-01-29 00:31:46 

标签:Mysql,JavaSwing
目录
  • 1、功能介绍

  • 2、关键代码

    • 2.1 主页功能

    • 2.2 添加商品信息

    • 2.3 数据库设计

      • 商品表

基于Mysql+JavaSwing的超市商品管理系统设计与实现

前言:

     随着小超市规模的发展不断扩大, 商品数量急剧增加, 有关商品的各种信息量也成倍增长。 超市时时刻刻都需要对商品各种信息进行统计分析。 而大型的超市管理系统功能过于强大而造成操作繁琐降低了小超市的工作效率。 超市管理系统是市场上最流行的超市上常用的系统之一, 由于刚学Java知识、所有功能设计的比较简单、只有商品信息的增删改查。实现对商品信息全面、 动态、及时的管理。本文系统的分析了软件开发的背景以过程;首先介绍了软件的开发环境, 其次介绍了本软件的详细设计过程: 数据库的设计、各个模块的设计和实现,以及具体界面的设计和功能。超市库存管理系统是基于 Java eclipse 作为开发工具 , Mysql 作为后台数据库支持。超市库存管理系统开发主要是界面程序的开发、数据库的建立、数据库的维护。应用程序功能完善,界面人机交互要好,而且操作简单。同时 JAVASwing语言简单,在较短的时间内能够开发出使用性强、 功能完善, 易于操作的程序, 也能实现与数据库的连接。

主要模块:

商品列表数据展示、商品信息添加、商品信息修改、商品信息删除、按照商品名称查询商品信息

1、功能介绍

功能截图:

查询商品列表信息:

基于Mysql+JavaSwing的超市商品管理系统设计与实现

添加商品信息:

基于Mysql+JavaSwing的超市商品管理系统设计与实现

修改商品信息:

基于Mysql+JavaSwing的超市商品管理系统设计与实现

删除商品信息:

删除之后需要刷新一下列表数据

基于Mysql+JavaSwing的超市商品管理系统设计与实现

编号查询商品信息:

基于Mysql+JavaSwing的超市商品管理系统设计与实现

2、关键代码

2.1 主页功能


public class GoodsManage extends JFrame {
private JTextField textField;
Select select = new Select();
Updata updata = new Updata();
Object[] header= {"商品编号","商品名称","数量","单价"};
String sql = "SELECT goodsID,goodsname,num,price FROM goods";
Object[][] data= select.getGoods(sql);
DefaultTableModel df = new DefaultTableModel(data, header);
int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;

public GoodsManage() {
 super("商品管理系统");
 this.setBounds(0, 0, 700, 450);
 this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
 this.setResizable(false);//让窗口大小不可改变
 getContentPane().setLayout(null);

JTable jTable = new JTable(df);
 JScrollPane jsp=new JScrollPane(jTable,v,h);
 jsp.setBounds(10, 10, 515, 320);
 getContentPane().add(jsp);

JButton button_1 = new JButton("显示所有商品");
 button_1.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
   String sql = "SELECT goodsID,goodsname,num,price FROM goods";
   Object[][] data = Select.getGoods(sql);
   df.setDataVector(data, header);
  }
 });

button_1.setBounds(535, 80, 127, 30);
 getContentPane().add(button_1);

JButton button_2 = new JButton("修改商品");
 button_2.setBounds(535, 140, 127, 30);
 getContentPane().add(button_2);
 button_2.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
   if (jTable.getSelectedColumn()<0) {
    JOptionPane.showMessageDialog(null, "请选择要修改的数据!");
   } else {
    int goodsID = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
    String name = jTable.getValueAt(jTable.getSelectedRow(), 1).toString();
    int num = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 2).toString());
    String price = jTable.getValueAt(jTable.getSelectedRow(), 3).toString();
    Goods goods = new Goods(goodsID,name,num,price);
    GoodsXG goodsXG = new GoodsXG(goods);
    goodsXG.setVisible(true);
   }

}
 });

JButton button_3 = new JButton("删除商品");
 button_3.setBounds(535, 200, 127, 30);
 getContentPane().add(button_3);
 button_3.addActionListener(new ActionListener() {
  @Override
  public void actionPerformed(ActionEvent e) {
   if (jTable.getSelectedColumn()<0) {
    JOptionPane.showMessageDialog(null, "请选中要删除的数据!");
   } else {
    int goodsID = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
    String sql="delete from goods where goodsid="+goodsID;
    int result = updata.addData(sql);
    if (result>0) {
     JOptionPane.showMessageDialog(null, "删除成功!");
     JOptionPane.showMessageDialog(null, "记得刷新一下哦!");
    } else {
     JOptionPane.showMessageDialog(null, "删除失败!");
    }
   }
  }
 });

JButton button_4 = new JButton("添加商品");
 button_4.setBounds(535, 258, 127, 30);
 getContentPane().add(button_4);
 button_4.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
   GoodsADD goodsAdd = new GoodsADD();
   goodsAdd.setVisible(true);
  }
 });

JLabel label = new JLabel("商品编号:");
 label.setBounds(40, 354, 112, 32);
 getContentPane().add(label);

textField = new JTextField();
 textField.setBounds(154, 358, 127, 26);
 getContentPane().add(textField);
 textField.setColumns(10);

JButton button = new JButton("按编号查询");
 button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
   String sql = "SELECT goodsID,goodsname,num,price FROM goods WHERE goodsid LIKE '%"+textField.getText()+"%'";
   Object[][] data = Select.getGoods(sql);
   df.setDataVector(data, header);
  }
 });
 button.setBounds(305, 355, 112, 30);
 getContentPane().add(button);

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {
   super.windowClosing(e);
   //加入动作
   GoodsManagement m = new GoodsManagement();
   m.setVisible(true);
   }
 });
}

public static void main(String[] args) {
 GoodsManage t = new GoodsManage();
 t.setVisible(true);
}
}

2.2 添加商品信息


public class GoodsADD extends JFrame {
private JTextField id,name,num,price;
private JButton button;
private JButton button_1;

public GoodsADD() {
 super("商品管理系统");
 this.setBounds(0, 0, 400, 450);
 this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
 this.setResizable(false);//让窗口大小不可改变
 getContentPane().setLayout(null);

JLabel label = new JLabel("商品编号:");
 label.setBounds(85, 89, 87, 22);
 getContentPane().add(label);

id = new JTextField();
 id.setBounds(147, 90, 142, 21);
 getContentPane().add(id);
 id.setColumns(10);

JLabel label_1 = new JLabel("商品名称");
 label_1.setBounds(85, 139, 87, 22);
 getContentPane().add(label_1);

name = new JTextField();
 name.setColumns(10);
 name.setBounds(147, 140, 142, 21);
 getContentPane().add(name);

JLabel label_2 = new JLabel("数量:");
 label_2.setBounds(85, 193, 87, 22);
 getContentPane().add(label_2);

num = new JTextField();
 num.setColumns(10);
 num.setBounds(147, 194, 142, 21);
 getContentPane().add(num);

JLabel label_3 = new JLabel("单价:");
 label_3.setBounds(85, 241, 87, 22);
 getContentPane().add(label_3);

price = new JTextField();
 price.setColumns(10);
 price.setBounds(147, 242, 142, 21);
 getContentPane().add(price);

button = new JButton("确定");
 button.setBounds(78, 317, 93, 23);
 getContentPane().add(button);
 button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
   String addId = id.getText();
   String addName = name.getText();
   String addNum = num.getText();
   String addPrice = num.getText();
   if (addName.equals("")||addName.equals("")||addNum.equals("")||addPrice.equals("")) {
    JOptionPane.showMessageDialog(null, "请完整输入要添加的数据");
   } else {
    String sql="INSERT INTO goods VALUES("+addId+",'"+addName+"','"+addNum+"','"+addPrice+"')";
    int result = Updata.addData(sql);
    if (result>0) {
     JOptionPane.showMessageDialog(null, "添加成功!");
                 JOptionPane.showMessageDialog(null, "记得刷新一下哦!");
     dispose();
//      GoodsManage i = new GoodsManage();
//      i.setVisible(true);
    } else {
     JOptionPane.showMessageDialog(null, "添加失败!");
    }
   }

}
 });

button_1 = new JButton("取消");
 button_1.setBounds(208, 317, 93, 23);
 getContentPane().add(button_1);
 button_1.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent arg0) {
   dispose();
  }
 });

}
}

2.3 数据库设计

商品表


CREATE TABLE `NewTable` (
`goodsID`  int(11) NOT NULL ,
`goodsName`  varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,
`num`  int(11) NOT NULL ,
`price`  decimal(10,4) NOT NULL ,
PRIMARY KEY (`goodsID`)
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=COMPACT
;

来源:https://juejin.cn/post/7012021268597702664

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com