vlambda博客
学习文章列表

Java实现超市管理系统(含数据库)

 
   
   
 

  
    
    
  
     
       
       
     
            
              
              
            
作者:AiY..

来源:https://blog.csdn.net/qq_44859533

序言: 学Java已经有几个月了,上一次总结是针对GUI界面写的简易计算器,实现加减乘除功能以及计算器标准型和科学型之间的转换,有兴趣可以看看 Java混合计算器以及界面切换 . 这次写的超市管理系统,实现的功能有账户的注册、登录,超市商品类别的添加、修改和删除以及商品的添加、修改和删除的功能。用户注册之后把注册信息导入数据库;用户登录时候查询用户表,方可登录进去;商品类别和商品的增加也如注册信息一样,把信息导入商品类别表和商品表。由于快期末考试了,超市管理系统的一些功能还没有完善,后续还会更新顾客登录超市系统后,只能拥有查询商品的权限,并且还可以实现购买商品的功能,哈~哈 ~…

1、首先是建立数据库表:

Java实现超市管理系统(含数据库)Java实现超市管理系统(含数据库)

Java实现超市管理系统(含数据库)
Java实现超市管理系统(含数据库)

2、实现主页面:

Java实现超市管理系统(含数据库)

3、实现用户注册:

Java实现超市管理系统(含数据库)核心代码:

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,"姓名不能为空");
            return;
        }
        if(StringUtil.isEmpty(age)){
            JOptionPane.showMessageDialog(null,"年龄不能为空");
            return;
        }
        if(StringUtil.isEmpty(QQ)){
            JOptionPane.showMessageDialog(null,"QQ不能为空");
            return;
        }
        if(StringUtil.isEmpty(userName)){
            JOptionPane.showMessageDialog(null,"注册账号不能为空");
            return;
        }
        if(StringUtil.isEmpty(password1)){
            JOptionPane.showMessageDialog(null,"注册密码不能为空");
            return;
        }
        if(StringUtil.isEmpty(password2)){
            JOptionPane.showMessageDialog(null,"确认密码不能为空");
            return;
        }
        if(!password1.equals(password2)){
            JOptionPane.showMessageDialog(null,"两个密码填写不一致");
            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,"用户注册成功!");

            }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);
    }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970

注册成功之后可以返回上一步,重新登录

4、实现用户登录:

Java实现超市管理系统(含数据库)核心代码:

 //重置
    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());
        if(StringUtil.isEmpty(userName)){
            JOptionPane.showMessageDialog(null,"用户名不能为空!");
            return;
        }
        if(StringUtil.isEmpty(password)){
            JOptionPane.showMessageDialog(null,"密码不能为空");
            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,"登录失败,用户名密码错误!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                dbUtil.closeCon(con);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
12345678910111213141516171819202122232425262728293031323334353637383940

接下来是商品类别和商品的实现,主要是在JFrame窗口中加入Inter窗口

Java实现超市管理系统(含数据库)

5、主菜单之关于:

Java实现超市管理系统(含数据库)核心代码:

Java实现超市管理系统(含数据库)6、主菜单之商品类别管理:

Java实现超市管理系统(含数据库)Java实现超市管理系统(含数据库)

核心代码:

 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("");
    }
123456789101112131415161718192021222324252627282930313233343536
//表格行点击事件
    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {
        int row=jTable1.getSelectedRow();//获取行数
        idTxt.setText((String)jTable1.getValueAt(row,0));
        goodsTypeNameTxt.setText((String)jTable1.getValueAt(row,1));
        goodsTypeDescTxt.setText((String)jTable1.getValueAt(row,2));

    }
    //鼠标单击后,删除
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        String id=idTxt.getText();
        if(StringUtil.isEmpty(id)){
            JOptionPane.showMessageDialog(null,"请选择要删除的记录");
            return;
        }
        int n=JOptionPane.showConfirmDialog(null,"Are you 确定delete this记录?");
        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,"删除成功");
                    this.resetValue();
                    this.fillTable(new GoodsType());
                }else{
                    JOptionPane.showMessageDialog(null,"删除失败");
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try{
                    dbUtil.closeCon(con);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    //鼠标单击后,修改
    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,"请选择要修改的记录");
            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,"修改成功");
                this.resetValue();
                this.fillTable(new GoodsType());
            }else{
                JOptionPane.showMessageDialog(null,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                dbUtil.closeCon(con);
            }catch (Exception e){
                e.printStackTrace();
            }
        }


    }
    //查询商品类型
    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);
    }
    //插入商品类型
    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();
            }
        }
    }
    //修改后重置
    private void resetValue(){
        this.idTxt.setText("");
        this.goodsTypeNameTxt.setText("");
        this.goodsTypeDescTxt.setText("");
    }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112

7、主菜单之商品管理:

Java实现超市管理系统(含数据库)Java实现超市管理系统(含数据库)

核心代码:

//查询
    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 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("请选择");
                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();
            }

        }
    }
    //表
    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();
            }
        }
    }
    //鼠标点击表格事件
    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));

    }
    //鼠标单击后,修改
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
        String id=this.idTxt.getText();
        if(StringUtil.isEmpty(id)){
            JOptionPane.showMessageDialog(null,"请选择要修改的记录");
            return;
        }
        String goodsName=this.goodsNameTxt.getText();
        String price=this.priceTxt.getText();
        String goodsDesc=this.goodsDescTxt.getText();
        if(StringUtil.isEmpty(goodsName)){
            JOptionPane.showMessageDialog(null,"商品名称不能为空");
            return;
        }
        if(StringUtil.isEmpty(price)){
            JOptionPane.showMessageDialog(null,"商品价钱不能为空");
            return;
        }
        if(StringUtil.isEmpty(goodsDesc)){
            JOptionPane.showMessageDialog(null,"商品描述不能为空");
            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,"修改成功");
                this.resetValue();
                this.fillTable(new Goods());
            }else{
                JOptionPane.showMessageDialog(null,"修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{
                dbUtil.closeCon(con);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
    //鼠标单击后删除
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
        String id=idTxt.getText();
        if(StringUtil.isEmpty(id)){
            JOptionPane.showMessageDialog(null,"请选择要删除的记录");
            return;
        }
        int n=JOptionPane.showConfirmDialog(null,"Are you 确定delete this记录?");
        if(n==0){
            Connection con=null;
            try{
                con=dbUtil.getCon();
                int deleteNum=goodsDao.delete(con,id);
                if(deleteNum==1){
                    JOptionPane.showMessageDialog(null,"删除成功");
                    this.resetValue();
                    this.fillTable(new Goods());
                }else{
                    JOptionPane.showMessageDialog(null,"删除失败");
                }

            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try{
                    dbUtil.closeCon(con);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    //修改后重置
    private void resetValue(){
        this.idTxt.setText("");
        this.goodsNameTxt.setText("");
        this.goodsDescTxt.setText("");
    }
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
private void jButton2ActionPerformed(ActionEvent evt) {
        this.resetValue();
    }
    //商品添加
    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,"商品类别名称不能为空");
            return;
        }
        if(StringUtil.isEmpty(price)){
            JOptionPane.showMessageDialog(null,"商品价格不能为空");
            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,"商品添加成功");
                resetValue();
            }else{
                JOptionPane.showMessageDialog(null,"商品添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            JOptionPane.showMessageDialog(null,"商品添加失败");
        }finally {
            try{
                dbUtil.closeCon(con);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    /**
     * 重置表单
     */

    private void resetValue(){
        this.goodsNameTxt.setText("");
        this.priceTxt.setText("");
        this.goodsDecTxt.setText("");
        if(this.goodsTypeJcb.getItemCount()>0){
            this.goodsTypeJcb.setSelectedIndex(0);

        }
    }
    //初始化商品类别下拉框
    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 {

        }
    }
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273

8、退出:




17张「思维导图」由浅入深的总结了「 统计学」领域的基础知识,是我至今为止所有与统计有关的学习笔记。 获取方式:

2. 回复关键词:统计思维导图