Realization of supermarket management system (including database) with Java

Java advanced architect 2021-01-22 12:24:08
realization supermarket management including database



preface : The supermarket management system I wrote this time , The functions include account registration 、 Sign in , The addition of supermarket commodity categories 、 Modification and deletion and addition of goods 、 Modify and delete functions . After the user registers, the registration information is imported into the database ; When a user logs in, he queries the user table , To log in ; The increase of commodity category and commodity is just like the registration information , Import information into commodity category table and commodity table . Some functions of supermarket management system are not perfect , After customers log in to the supermarket system , You can only have the right to query goods , And it can also realize the function of purchasing goods .

1、 The first is to build a database table :

2、 Realize the main page :

3、 Implement user registration :

Core code :

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();
if(StringUtil.isEmpty(name)){
JOptionPane.showMessageDialog(null," Name cannot be empty ");
return;
}
if(StringUtil.isEmpty(age)){
JOptionPane.showMessageDialog(null," Age cannot be empty ");
return;
}
if(StringUtil.isEmpty(QQ)){
JOptionPane.showMessageDialog(null,"QQ Can't be empty ");
return;
}
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null," The registered account number cannot be empty ");
return;
}
if(StringUtil.isEmpty(password1)){
JOptionPane.showMessageDialog(null," The registration password cannot be empty ");
return;
}
if(StringUtil.isEmpty(password2)){
JOptionPane.showMessageDialog(null," The confirmation password cannot be empty ");
return;
}
if(!password1.equals(password2)){
JOptionPane.showMessageDialog(null," The two passwords don't match ");
return;
}
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," User registration successful !");
}else{
JOptionPane.showMessageDialog(null," Registration failed !!");
}
}catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null," Registration failed !!");
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
// The previous step
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
dispose();
new HomePage().setVisible(true);
}

After successful registration, you can return to the previous step , Log back in

4、 Implement user login :

Core code :

 // Reset
private void resetValueActionPerformed(ActionEvent evt) {
this.userNameTxt.setText("");
this.passwordTxt.setText("");
}
// Login event handling
private void loginActionPerformed(ActionEvent evt) {
String userName=this.userNameTxt.getText();
String password=new String(this.passwordTxt.getPassword());
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(null," The username cannot be empty !");
return;
}
if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(null," The password cannot be empty ");
return;
}
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," Login failed , Wrong username and password !");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}

Next is the commodity category and commodity realization , Mainly in the JFrame Add... To the window Inter window

5、 The main menu is about :

Core code :

6、 Commodity category management in the main menu :

Core code :

 private void jButton1ActionPerformed(ActionEvent evt) {
String goodsTypeName=this.goodsTypeNameTxt.getText();
String goodsTypeDesc=this.goodsTypeDescTxt.getText();
if(StringUtil.isEmpty(goodsTypeName)){
JOptionPane.showMessageDialog(null," Product category cannot be empty ");
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," Product category added successfully !");
jButton2ActionPerformed(evt);
}else{
JOptionPane.showMessageDialog(null," Add failure !!");
}
}catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null," Add failure !!");
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
// Reset
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
this.goodsTypeNameTxt.setText("");
this.goodsTypeDescTxt.setText("");
}
// Table row click events
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int row=jTable1.getSelectedRow();// Get the number of lines
idTxt.setText((String)jTable1.getValueAt(row,0));
goodsTypeNameTxt.setText((String)jTable1.getValueAt(row,1));
goodsTypeDescTxt.setText((String)jTable1.getValueAt(row,2));
}
// After mouse click , Delete
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null," Please select the record to delete ");
return;
}
int n=JOptionPane.showConfirmDialog(null,"Are you determine delete this Record ?");
if(n==0){
Connection con=null;
try{
con=dbUtil.getCon();
int deleteNum=goodsTypeDao.delect(con,id);
//System.out.println(deleteNum);
if(deleteNum==1){
JOptionPane.showMessageDialog(null," Delete successful ");
this.resetValue();
this.fillTable(new GoodsType());
}else{
JOptionPane.showMessageDialog(null," Delete failed ");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
// After mouse click , modify
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String id=idTxt.getText();
String goodsTypeName=goodsTypeNameTxt.getText();
String goodsTypeDesc=goodsTypeDescTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null," Please select the record to modify ");
return;
}
GoodsType goodsType=new GoodsType(Integer.parseInt(id),goodsTypeName,goodsTypeDesc);
Connection con=null;
try{
con=dbUtil.getCon();
int modifyNum=goodsTypeDao.update(con,goodsType);
if(modifyNum==1){
JOptionPane.showMessageDialog(null," Modification successful ");
this.resetValue();
this.fillTable(new GoodsType());
}else{
JOptionPane.showMessageDialog(null," Modification failed ");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
// Query product type
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String t_goodsTypeName=this.jTextField2.getText();
GoodsType goodsType=new GoodsType();
goodsType.setGoodsTypeName(t_goodsTypeName);
this.fillTable(goodsType);
}
// Insert item type
private void fillTable(GoodsType goodsType){
DefaultTableModel dtm=(DefaultTableModel)jTable1.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,goodsType);
while(rs.next()){
Vector v=new Vector();
v.add(rs.getString("id"));
v.add(rs.getString("goodsTypeName"));
v.add(rs.getString("goodsTypeDesc"));
dtm.addRow(v);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
// Reset after modification
private void resetValue(){
this.idTxt.setText("");
this.goodsTypeNameTxt.setText("");
this.goodsTypeDescTxt.setText("");
}

7、 Commodity management of the main menu :

Core code :

// Inquire about
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);
}
// A drop-down box
private void fillGoodsType(String type){
Connection con=null;
GoodsType goodsType=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,new GoodsType());
if("search".equals(type)){
goodsType=new GoodsType();
goodsType.setGoodsTypeName(" Please select ");
goodsType.setId(-1);
this.s_goodsJcb.addItem(goodsType);
}
while(rs.next()){
goodsType=new GoodsType();
goodsType.setGoodsTypeName(rs.getString("goodsTypeName"));
goodsType.setId(rs.getInt("id"));
if("search".equals(type)){
this.s_goodsJcb.addItem(goodsType);
}else if("modify".equals(type)){
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
// surface
private void fillTable(Goods goods){
DefaultTableModel dtm=(DefaultTableModel) jTable1.getModel();
dtm.setRowCount(0);
Connection con=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsDao.list(con,goods);
while(rs.next()){
Vector v=new Vector();
v.add(rs.getInt("id"));
v.add(rs.getString("goodsName"));
v.add(rs.getFloat("price"));
v.add(rs.getString("goodsDesc"));
dtm.addRow(v);
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
// Click on the table event
private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
int row=jTable1.getSelectedRow();
this.idTxt.setText((Integer)jTable1.getValueAt(row,0)+"");
this.goodsNameTxt.setText((String)jTable1.getValueAt(row,1));
this.priceTxt.setText((Float)jTable1.getValueAt(row,2)+"");
this.goodsDescTxt.setText((String)jTable1.getValueAt(row,3));
}
// After mouse click , modify
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
String id=this.idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null," Please select the record to modify ");
return;
}
String goodsName=this.goodsNameTxt.getText();
String price=this.priceTxt.getText();
String goodsDesc=this.goodsDescTxt.getText();
if(StringUtil.isEmpty(goodsName)){
JOptionPane.showMessageDialog(null," Commodity name cannot be empty ");
return;
}
if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null," Commodity price cannot be empty ");
return;
}
if(StringUtil.isEmpty(goodsDesc)){
JOptionPane.showMessageDialog(null," Product description cannot be empty ");
return;
}
Goods goods=new Goods(Integer.parseInt(id),goodsName, Float.parseFloat(price),goodsDesc);
Connection con=null;
try{
con=dbUtil.getCon();
int modifyNum=goodsDao.update(con,goods);
if(modifyNum==1){
JOptionPane.showMessageDialog(null," Modification successful ");
this.resetValue();
this.fillTable(new Goods());
}else{
JOptionPane.showMessageDialog(null," Modification failed ");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
// Delete after mouse click
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
String id=idTxt.getText();
if(StringUtil.isEmpty(id)){
JOptionPane.showMessageDialog(null," Please select the record to delete ");
return;
}
int n=JOptionPane.showConfirmDialog(null,"Are you determine delete this Record ?");
if(n==0){
Connection con=null;
try{
con=dbUtil.getCon();
int deleteNum=goodsDao.delete(con,id);
if(deleteNum==1){
JOptionPane.showMessageDialog(null," Delete successful ");
this.resetValue();
this.fillTable(new Goods());
}else{
JOptionPane.showMessageDialog(null," Delete failed ");
}
}catch (Exception e){
e.printStackTrace();
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
// Reset after modification
private void resetValue(){
this.idTxt.setText("");
this.goodsNameTxt.setText("");
this.goodsDescTxt.setText("");
}
private void jButton2ActionPerformed(ActionEvent evt) {
this.resetValue();
}
// Add
private void jButton1ActionPerformed(ActionEvent evt){
String goodsName=this.goodsNameTxt.getText();
String price=this.priceTxt.getText();
String goodsDesc=this.goodsDecTxt.getText();
if(StringUtil.isEmpty(goodsName)){
JOptionPane.showMessageDialog(null," Product category name cannot be empty ");
return;
}
if(StringUtil.isEmpty(price)){
JOptionPane.showMessageDialog(null," Commodity price cannot be empty ");
return;
}
GoodsType goodsType=(GoodsType)goodsTypeJcb.getSelectedItem();
int goodsTypeId=goodsType.getId();
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," Product added successfully ");
resetValue();
}else{
JOptionPane.showMessageDialog(null," Product addition failed ");
}
}catch (Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(null," Product addition failed ");
}finally {
try{
dbUtil.closeCon(con);
}catch (Exception e){
e.printStackTrace();
}
}
}
/**
* Reset form
*/
private void resetValue(){
this.goodsNameTxt.setText("");
this.priceTxt.setText("");
this.goodsDecTxt.setText("");
if(this.goodsTypeJcb.getItemCount()>0){
this.goodsTypeJcb.setSelectedIndex(0);
}
}
// Initialize the product category drop-down box
private void fillGoodsType(){
Connection con=null;
GoodsType goodsType=null;
try{
con=dbUtil.getCon();
ResultSet rs=goodsTypeDao.list(con,new GoodsType());
while(rs.next()){
goodsType=new GoodsType();
goodsType.setId(rs.getInt("id"));
goodsType.setGoodsTypeName(rs.getString("goodsTypeName"));
this.goodsTypeJcb.addItem(goodsType);
}
}catch (Exception e){
e.printStackTrace();
}finally {
}
}

8、 sign out :


This article is from WeChat official account. - java Advanced architect (java_jiagoushi)

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the yunjia_community@tencent.com Delete .

Original publication time : 2021-01-18

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Java advanced architect]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210122115415440l.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云