Supermarket Management System Tutorial Using Java Swing and MySQL
This article walks through building a Java Swing supermarket management desktop application, covering database table creation, user registration and login, product category and product CRUD operations, and the main menu UI, with complete source code snippets for each feature.
The tutorial describes a complete supermarket management system implemented as a Java Swing desktop application that interacts with a MySQL database. It begins with the design of the required database tables for users, product categories, and products.
1. Database Tables – The author shows screenshots of the SQL schema and explains that user registration data, product category data, and product data are stored in separate tables.
2. Main Interface – A screenshot of the main window is provided, illustrating the navigation menu that links to registration, login, product category management, and product management screens.
3. User Registration
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Register().setVisible(true);
}
});
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String name = this.jTextField1.getText();
String age = this.jTextField3.getText();
String QQ = this.jTextField4.getText();
String userName = this.jTextField5.getText();
String password1 = this.jPasswordField1.getText();
String password2 = this.jPasswordField2.getText();
// validation checks omitted for brevity
User user = new User(userName, password1);
Connection con = null;
try {
con = dbUtil.getCon();
int n = userDao.add(con, user);
if (n == 1) {
JOptionPane.showMessageDialog(null, "用户注册成功!");
} else {
JOptionPane.showMessageDialog(null, "注册失败!!");
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "注册失败!!");
} finally {
try { dbUtil.closeCon(con); } catch (Exception e) { e.printStackTrace(); }
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
new HomePage().setVisible(true);
}After successful registration, the user can return to the login screen.
4. User Login
private void resetValueActionPerformed(ActionEvent evt) {
this.userNameTxt.setText("");
this.passwordTxt.setText("");
}
private void loginActionPerformed(ActionEvent evt) {
String userName = this.userNameTxt.getText();
String password = new String(this.passwordTxt.getPassword());
// validation omitted
User usr = new User(userName, password);
Connection con = null;
try {
con = dbUtil.getCon();
User currentUser = userDao.login(con, usr);
if (currentUser != null) {
dispose();
new MainFrm().setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "登录失败,用户名密码错误!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { dbUtil.closeCon(con); } catch (Exception e) { e.printStackTrace(); }
}
}5. Product Category Management
private void jButton1ActionPerformed(ActionEvent evt) {
String goodsTypeName = this.goodsTypeNameTxt.getText();
String goodsTypeDesc = this.goodsTypeDescTxt.getText();
if (StringUtil.isEmpty(goodsTypeName)) {
JOptionPane.showMessageDialog(null, "商品类别不能为空");
return;
}
GoodsType goodsType = new GoodsType(goodsTypeName, goodsTypeDesc);
Connection con = null;
try {
con = dbUtil.getCon();
int n = goodsTypeDao.add(con, goodsType);
if (n == 1) {
JOptionPane.showMessageDialog(null, "商品类别添加成功!");
jButton2ActionPerformed(evt);
} else {
JOptionPane.showMessageDialog(null, "添加失败!!");
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "添加失败!!");
} finally {
try { dbUtil.closeCon(con); } catch (Exception e) { e.printStackTrace(); }
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.goodsTypeNameTxt.setText("");
this.goodsTypeDescTxt.setText("");
}Additional code handles table row selection, deletion, and modification of product categories.
6. Product Management
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String t_goodsName = this.s_goodsNameTxt.getText();
Goods goods = new Goods();
goods.setGoodsName(t_goodsName);
this.fillTable(goods);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String goodsName = this.goodsNameTxt.getText();
String price = this.priceTxt.getText();
String goodsDesc = this.goodsDescTxt.getText();
if (StringUtil.isEmpty(goodsName) || StringUtil.isEmpty(price) || StringUtil.isEmpty(goodsDesc)) {
JOptionPane.showMessageDialog(null, "字段不能为空");
return;
}
Goods goods = new Goods(goodsName, Float.parseFloat(price), goodsDesc);
Connection con = null;
try {
con = dbUtil.getCon();
int addNum = goodsDao.add(con, goods);
if (addNum == 1) {
JOptionPane.showMessageDialog(null, "商品添加成功");
resetValue();
} else {
JOptionPane.showMessageDialog(null, "商品添加失败");
}
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "商品添加失败");
} finally {
try { dbUtil.closeCon(con); } catch (Exception e) { e.printStackTrace(); }
}
}Code for updating, deleting, and resetting product entries is also included, following the same pattern of validation, DAO calls, and resource cleanup.
7. Exit – The final menu option simply closes the application, as shown in the accompanying screenshot.
At the end, the author invites readers to like and share the post if they found it helpful.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.