Java Swing设计简单商品信息管理系统(java swing+mysql+eclipse)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS PostgreSQL,高可用系列 2核4GB
简介: Java Swing设计简单商品信息管理系统(java swing+mysql+eclipse)

一、概述


为了管理好商店库存信息,提升店铺管理工作效率,结合实际工作需要,设计和开发本系统,主要用于商店商品信息维护出入库等。包含商品库存信息查看、商品信息修改,新增商品信息,删除信息等功能。



二、功能清单


1、查询如图


查询界面,请从数据库查询学生信息表数据并显示在控件上,通过底部功能菜单执行相应功能,添加、修改按钮点击后课弹出相应窗体执行相应操作,点击刷新按钮课刷新当前数据,删除按钮点击后可删除选中行数据并刷新


34.png


2、添加,如图


填写姓名和班级后,点击添加按钮后可添加数据


35.png


3、修改,如图

通过点击查询界面中“修改按钮”,可在修改界面修改当前选中行数据


36.png


三、数据库


注意:数据库名称为“班级_姓名”,如“1705_小白”。

表名称:tGoods

字段


37.png

评分规则(共100分)


38.png


实现代码:

数据库 链接: https://pan-yz.chaoxing.com/external/m/file/483246110958415872


Java文件 链接: https://pan-yz.chaoxing.com/external/m/file/483246085097291776


39.png


数据库:

-- ----------------------------
-- Table structure for tgoods
-- ----------------------------
DROP TABLE IF EXISTS `tgoods`;
CREATE TABLE `tgoods` (
  `goodsID` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `typeName` varchar(20) DEFAULT NULL,
  `stock` int(11) DEFAULT NULL,
  PRIMARY KEY (`goodsID`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of tgoods
-- ----------------------------
INSERT INTO `tgoods` VALUES ('9', '统一冰红茶', '饮料', '24');
INSERT INTO `tgoods` VALUES ('10', '娃哈哈营养快线', '饮料', '23');


com.test.db >>> DbConnection


package com.test.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
public class DbConnection {
  //驱动类的类名
  private static final String DRIVERNAME="com.mysql.jdbc.Driver";
  //连接数据的URL路径
  private static final String URL="jdbc:mysql://localhost:3306/1902_杨明金";
  //数据库登录账号
  private static final String USER="root";
  //数据库登录密码
  private static final String PASSWORD="root123";
  //加载驱动
  static{
    try {
      Class.forName(DRIVERNAME);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
  }
    //获取数据库连接
  public static Connection getConnection() {
            try {
    return DriverManager.getConnection(URL,USER,PASSWORD);
             } catch (SQLException e) { 
    e.printStackTrace();
             }
      return null;
  }
  //查询
  public static ResultSet query(String sql) {
    System.out.println(sql);
    //获取连接
    Connection connection=getConnection();
    PreparedStatement psd;
    try {
      psd = connection.prepareStatement(sql);
      return psd.executeQuery();
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return null;
  }
  //增、删、改、查
    public static int updataInfo(String sql) {
      System.out.println(sql);
      //获取连接
      Connection connection=getConnection();
      try {
        PreparedStatement psd=connection.prepareStatement(sql);
        return psd.executeUpdate();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      return 0;
    }
  //关闭连接
  public  static  void colse(ResultSet rs,Statement stmt,Connection  conn) throws Exception{
            try { if (rs != null){ rs.close(); }
                           if (stmt != null) { stmt.cancel(); }
     if (conn != null) { conn.close(); }
     } catch (Exception e) {
       e.printStackTrace(); throw new Exception();
     }
  }
} 


com.test.entity >>> Goods


package com.test.entity;
public class Goods {
  private int goodsID;//商品ID
  private String name;//商品名称
  private String typeName;//商品类别
  private int stock;//库存
  public Goods(int goodsID, String name, String typeName, int stock) {
    super();
    this.goodsID = goodsID;
    this.name = name;
    this.typeName = typeName;
    this.stock = stock;
  }
  public Goods() {
    super();
  }
  public int getGoodsID() {
    return goodsID;
  }
  public void setGoodsID(int goodsID) {
    this.goodsID = goodsID;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getTypeName() {
    return typeName;
  }
  public void setTypeName(String typeName) {
    this.typeName = typeName;
  }
  public int getStock() {
    return stock;
  }
  public void setStock(int stock) {
    this.stock = stock;
  }
}


com.test.controller >>> Updata


package com.test.controller;
import com.test.db.DbConnection;
public class Updata {
  //添加数据
  public static int addData(String sql) {
    return DbConnection.updataInfo(sql);
  }
}


com.test.controller >>> Select


package com.test.controller;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.test.db.DbConnection;
import com.test.entity.Goods;
public class Select {
  public static Object[][] getGoods() {
    String sql = "SELECT * FROM tgoods";
    ResultSet resultSet = DbConnection.query(sql);
    ArrayList<Goods> list=new ArrayList<Goods>();
    try {
      while (resultSet.next()) {
        Goods goods=new Goods();
        goods.setGoodsID(resultSet.getInt(1));
        goods.setName(resultSet.getString(2));
        goods.setTypeName(resultSet.getString(3));
        goods.setStock(resultSet.getInt(4));
        list.add(goods);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    Object[][] objects=new Object[list.size()][4];
    for(int i=0;i<list.size();i++) {
      objects[i][0]=list.get(i).getGoodsID();
      objects[i][1]=list.get(i).getName();
      objects[i][2]=list.get(i).getTypeName();
      objects[i][3]=list.get(i).getStock();
    }
    return objects;
  }
}


com.test.View >>> IndexGUI


package com.test.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import com.test.controller.Select;
import com.test.controller.Updata;
import com.test.entity.Goods;
public class IndexGUI extends JFrame {
  Object[] columnNames = {"商品编号","名称","类别名称","库存"};
  Object[][] data = Select.getGoods();
  DefaultTableModel df = new DefaultTableModel(data, columnNames);
  public IndexGUI() {
    super("商品信息管理");
    this.setBounds(0, 0, 780, 500);
    this.setLocationRelativeTo(null);//让窗口在屏幕中间显示
    this.setResizable(false);//让窗口大小不可改变
    this.setLayout(null);
    JTable jTable=new JTable(df);
    JScrollPane jp = new JScrollPane(jTable);
    jp.setBounds(0, 10, 780, 350);
    this.add(jp);
    JButton tj = new JButton("添加");
    tj.setBounds(50, 400, 100, 30);
    tj.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        IncreaseGUL i = new IncreaseGUL();
        i.setVisible(true);
      }
    });
    JButton sc = new JButton("删除");
    sc.setBounds(180, 400, 100, 30);
    sc.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 tgoods where goodsid="+goodsID;
          Updata updata = new Updata();
          int result = updata.addData(sql);
          if (result>0) {
            JOptionPane.showMessageDialog(null, "删除成功!");
            JOptionPane.showMessageDialog(null, "记得刷新一下哦!");
          } else {
            JOptionPane.showMessageDialog(null, "删除失败!");
          }
        }
      }
    });
    JButton xg = new JButton("修改");
    xg.setBounds(310, 400, 100, 30);
    xg.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();
          String typeName = jTable.getValueAt(jTable.getSelectedRow(), 2).toString();
          int stock = Integer.parseInt(jTable.getValueAt(jTable.getSelectedRow(), 3).toString());
          Goods goods = new Goods(goodsID,name,typeName,stock);
          ModifyGUI modifyGUI = new ModifyGUI(goods);
          modifyGUI.setVisible(true);
        }
      }
    });
    JButton sx = new JButton("刷新");
    sx.setBounds(440, 400, 100, 30);
    sx.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        Object[][] data = Select.getGoods();
        df.setDataVector(data, columnNames);
      }
    });
    this.add(tj);
    this.add(sc);
    this.add(xg);
    this.add(sx);
  }
}



com.test.View >>> IncreaseGUL

package com.test.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.test.controller.Updata;
public class IncreaseGUL extends JFrame implements ActionListener {
  JTextField name = new JTextField();
  JTextField type = new JTextField();
  JTextField num = new JTextField();
  public IncreaseGUL() {
    super.setTitle("添加商品");
    this.setBounds(0, 0, 780, 250);
    this.setLocationRelativeTo(null);
    this.setResizable(false);
    this.setLayout(null);
    JLabel nameT = new JLabel("名称");
    nameT.setBounds(50, 30, 40, 25);
    name.setBounds(100, 30, 145, 30);
    JLabel typeT = new JLabel("类别");
    typeT.setBounds(280, 30, 40, 25);
    type.setBounds(335, 30, 145, 30);
    JLabel numT = new JLabel("数量");
    numT.setBounds(515, 30, 40, 25);
    num.setBounds(575, 30, 145, 30);
    JButton tj = new JButton("添加");
    tj.setBounds(100, 115, 100, 30);
    tj.addActionListener(this);
    this.add(nameT);
    this.add(name);
    this.add(typeT);
    this.add(type);
    this.add(numT);
    this.add(num);
    this.add(tj);
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    String sql = null;
    String addName = name.getText();
    String addType = type.getText();
    String addNum = num.getText();
    if (addName.equals("")||addType.equals("")||addNum.equals("")) {
      JOptionPane.showMessageDialog(null, "请完整输入要添加的数据");
    } else {
      sql="INSERT INTO tgoods VALUES("+"null,'"+addName+"','"+addType+"','"+addNum+"')";
      int result = Updata.addData(sql);
      if (result>0) {
        JOptionPane.showMessageDialog(null, "添加成功!");
                JOptionPane.showMessageDialog(null, "记得刷新一下哦!");
        dispose();
        IndexGUI i = new IndexGUI();
      } else {
        JOptionPane.showMessageDialog(null, "添加失败!");
      }
    }
  }
}



com.test.View >>> ModifyGUI

package com.test.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.test.controller.Updata;
import com.test.entity.Goods;
public class ModifyGUI extends JFrame implements ActionListener {
  JTextField name = new JTextField();
  JTextField type = new JTextField();
  JTextField num = new JTextField();
  int id;
  public ModifyGUI(Goods goods) {
    super.setTitle("修改商品");
    this.setBounds(0, 0, 780, 250);
    this.setLocationRelativeTo(null);
    this.setLayout(null);
    JLabel nameT = new JLabel("名称");
    nameT.setBounds(50, 30, 40, 25);
    name.setBounds(100, 30, 145, 30);
    JLabel typeT = new JLabel("类别");
    typeT.setBounds(280, 30, 40, 25);
    type.setBounds(335, 30, 145, 30);
    JLabel numT = new JLabel("库存");
    numT.setBounds(515, 30, 40, 25);
    num.setBounds(575, 30, 145, 30);
    JButton xg = new JButton("修改");
    xg.setBounds(100, 115, 100, 30);
    xg.addActionListener(this);
    this.add(nameT);
    this.add(name);
    this.add(typeT);
    this.add(type);
    this.add(numT);
    this.add(num);
    this.add(xg);
    name.setText(goods.getName());
    type.setText(goods.getTypeName());
    num.setText(Integer.toString(goods.getStock()));
    id = goods.getGoodsID();
  }
  @Override
  public void actionPerformed(ActionEvent e) {
    String sql = null;
    String addName = name.getText();
    String addType = type.getText();
    int addNum = Integer.parseInt(num.getText());
    if (addName.equals("")||addType.equals("")||addNum==0) {
      JOptionPane.showMessageDialog(null, "请完整输入要修改的数据");
    }else {
      Updata up=new Updata();
      sql="UPDATE tgoods SET "+"name='"+addName+"',typeName='"+addType+"',stock='"+addNum+"'where goodsid="+id;
      int result = Updata.addData(sql);
      Updata.addData(sql);
      if (result>0) {
        JOptionPane.showMessageDialog(null, "修改成功!");
                JOptionPane.showMessageDialog(null, "记得刷新一下哦!");
        dispose();
        IndexGUI i = new IndexGUI();
        i.setVisible(true);
      } else {
        JOptionPane.showMessageDialog(null, "修改失败!");
      }
    }
  }
}



com.test.Test >>> Test


package com.test.Test;
import com.test.view.IndexGUI;
public class Test {
  public static void main(String[] args) {
    IndexGUI i = new IndexGUI();
    i.setVisible(true);
  }
}
相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
关系型数据库 MySQL Java
【MySQL+java+jpa】MySQL数据返回项目的感悟
【MySQL+java+jpa】MySQL数据返回项目的感悟
134 1
|
关系型数据库 MySQL Java
【IDEA】java后台操作mysql数据库驱动常见错误解决方案
【IDEA】java后台操作mysql数据库驱动常见错误解决方案
347 0
|
6月前
|
负载均衡 算法 关系型数据库
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
本文聚焦 MySQL 集群架构中的负载均衡算法,阐述其重要性。详细介绍轮询、加权轮询、最少连接、加权最少连接、随机、源地址哈希等常用算法,分析各自优缺点及适用场景。并提供 Java 语言代码实现示例,助力直观理解。文章结构清晰,语言通俗易懂,对理解和应用负载均衡算法具有实用价值和参考价值。
大数据大厂之MySQL数据库课程设计:揭秘MySQL集群架构负载均衡核心算法:从理论到Java代码实战,让你的数据库性能飙升!
|
2月前
|
SQL Java 关系型数据库
Java连接MySQL数据库环境设置指南
请注意,在实际部署时应该避免将敏感信息(如用户名和密码)硬编码在源码文件里面;应该使用配置文件或者环境变量等更为安全可靠地方式管理这些信息。此外,在处理大量数据时考虑使用PreparedStatement而不是Statement可以提高性能并防止SQL注入攻击;同时也要注意正确处理异常情况,并且确保所有打开过得资源都被正确关闭释放掉以防止内存泄漏等问题发生。
114 13
|
9月前
|
存储 Java 关系型数据库
java调用mysql存储过程
在 Java 中调用 MySQL 存储过程主要借助 JDBC(Java Database Connectivity)。其核心原理是通过 JDBC 与 MySQL 建立连接,调用存储过程并处理结果。具体步骤包括:加载 JDBC 驱动、建立数据库连接、创建 CallableStatement 对象、设置存储过程参数并执行调用。此过程实现了 Java 程序与 MySQL 数据库的高效交互。
|
4月前
|
人工智能 Java 关系型数据库
Java的时间处理与Mysql的时间查询
本文总结了Java中时间与日历的常用操作,包括时间的转换、格式化、日期加减及比较,并介绍了MySQL中按天、周、月、季度和年进行时间范围查询的方法,适用于日常开发中的时间处理需求。
|
9月前
|
人工智能 JavaScript 关系型数据库
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
330 14
【02】Java+若依+vue.js技术栈实现钱包积分管理系统项目-商业级电玩城积分系统商业项目实战-ui设计图figmaUI设计准备-figma汉化插件-mysql数据库设计-优雅草卓伊凡商业项目实战
|
10月前
|
自然语言处理 Java 关系型数据库
Java mysql根据很长的富文本如何自动获取简介
通过使用Jsoup解析富文本并提取纯文本,然后根据需要生成简介,可以有效地处理和展示长文本内容。该方法简单高效,适用于各种应用场景。希望本文对您在Java中处理富文本并生成简介的需求提供实用的指导和帮助。
178 9
|
11月前
|
NoSQL Java 关系型数据库
Liunx部署java项目Tomcat、Redis、Mysql教程
本文详细介绍了如何在 Linux 服务器上安装和配置 Tomcat、MySQL 和 Redis,并部署 Java 项目。通过这些步骤,您可以搭建一个高效稳定的 Java 应用运行环境。希望本文能为您在实际操作中提供有价值的参考。
700 26
|
11月前
|
存储 IDE Java
漂亮不是梦!Java Swing美化攻略
Java Swing 是一个为 Java 设计的 GUI 工具包,提供文本框、按钮等组件。尽管其外观可定制,通过 Look and Feel(LAF)机制改变应用风格,如 Darcula 和 FlatLaf,但现已淡出主流视野,主要应用于 IDE 领域,如 IntelliJ IDEA 和 Eclipse。相比其他 GUI 框架,Swing 的发展前景有限。
405 1

推荐镜像

更多