进出口商品管理系统_java课程设计

以下内容可且仅可供参考,如有错误欢迎指正。

部分内容借鉴自百度

侵删致歉

链接数据库部分建议看其他博主,以下代码建立在链接成功的基础上

UI界面是纯java版本,无web内容

 

目录

 一、开发环境

二、需求分析

 三、功能模块设计

四、数据库设计

五、代码

1.Enter.java(登录界面)

 2.Ewindow.java(验证登录)

3.ManagerRegister.java(注册界面)

4.Register.java(验证注册)

5.Good.java(商品类)

6.Manager.java(管理员类)

7.Show1.java(查询界面)

8.Addgood.java(添加界面)

9.Deletegood.java(删除界面)

10.Modifygood.java(修改界面)

11.Goodinsql.java(修改数据库商品信息)


 

 

 一、开发环境

工具:IntelliJ IDEA

JDK 1.8

数据库:MySQL 8.0.15

二、需求分析

  1. UI界面
  2. 商品管理: 可以对仓库的商品进行统一管理,实现增删改查等
  3. 管理员管理:对仓库管理员进行管理

 三、功能模块设计

1.登录注册

2.增删改

 

 

四、数据库设计

数据库:goods management

表manager

名称

类型

备注

id

Varchar(6)

管理员ID 唯一

passwd

Varchar(20)

管理员密码

表 good

名称

类型

备注

name

Varchar(20)

商品名称 唯一

kind

Varchar(20)

商品种类

Indate

Varchar(20)

入库时间

price

double

商品价格

quantity

int

商品数量

note

Varchar(50)

商品备注

五、代码

1.Enter.java(登录界面)

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class Enter extends JFrame{
    Enter() {
        init();
    }
    //登录界面初始化
    public void init() {
        JFrame frame = new JFrame("登录");
        frame.setLayout(null);

        JLabel nameStr = new JLabel("账号:");
        nameStr.setBounds(200, 100, 100, 25);
        frame.add(nameStr);

        JLabel passwordStr = new JLabel("密码:");
        passwordStr.setBounds(200, 150, 100, 25);
        frame.add(passwordStr);

        JTextField userID = new JTextField();
        userID.setBounds(250, 100, 150, 25);
        frame.add(userID);

        JPasswordField password = new JPasswordField();
        password.setBounds(250, 150, 150, 25);
        frame.add(password);

        JButton buttonlogin = new JButton("登录");
        buttonlogin.setBounds(225, 200, 70, 25);
        frame.add(buttonlogin);

        JButton buttonregister = new JButton("注册");
        buttonregister.setBounds(325, 200, 70, 25);
        frame.add(buttonregister);

        frame.setBounds(800, 300, 600, 360);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        //为登录按钮添加监听器
        buttonlogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String ID = userID.getText();
                String passwd = new String (password.getPassword());

                //创建一个Admin用户,把输入框中的用户名密码和提出来
                Manager admin = new Manager();
                admin.setID(ID);
                admin.setPasswd(passwd);

                //登录
                Ewindow ewindow = new Ewindow();
                ewindow.setEnter(admin);

                if(ewindow.JudgeManager()==0) {
                    //弹出账号或密码错误的窗口
                    JOptionPane.showMessageDialog(null, "账号或密码错误", "账号或密码错误", JOptionPane.WARNING_MESSAGE);
                    //清除密码框中的信息
                    password.setText("");
                    //清除账号框中的信息
                    userID.setText("");

                    //System.out.println("登陆失败");
                } else {
                    //弹出登录成功的窗口
                    JOptionPane.showMessageDialog(null, "登陆成功", "登陆成功", JOptionPane.NO_OPTION);
                    //点击确定后会跳转到主窗口
                    frame.setVisible(false);
                    Show1 show1 = new Show1();
                    try {
                        show1.showFrame(show1.getDbData());
                    } catch (ClassNotFoundException ex) {
                        ex.printStackTrace();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }

            }
        });

        //为注册按钮添加监听器
        buttonregister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //注册页面
                frame.setVisible(false);
                ManagerRegister managerRegister = new ManagerRegister();
            }
        }
        );
    }

    public static void main(String []args) {
        //主程序
        //登录窗口
        Enter enter = new Enter();
    }
}

 2.Ewindow.java(验证登录)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Ewindow {
        Manager manager;

        void setEnter(Manager manager) {
        this.manager=manager;
        //System.out.println(this.admin.getPassword()+"   " + this.admin.getID());
    }
    /*
     * JudgeEnter()方法
     * 判断Manager的ID和密码是否正确,如果正确,显示登录成功
     * 如果错误,弹出一个窗口,显示账号或密码错误
     */
    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/goods management?serverTimezone=UTC&characterEncoding=utf-8";
    private String user = "root";
    private String password = "password";

    public boolean login(Manager manager) throws SQLException, ClassNotFoundException {
    String sql="select * from manager where id=? and passwd=?";

    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, user, password);
    PreparedStatement ps = conn.prepareStatement(sql);

    ps.setString(1, manager.getID());
    ps.setString(2, manager.getPasswd());
    ResultSet rs = ps.executeQuery();
    int ans = 0;
    if(rs.next()) {
        ans = 1;
    }
    rs.close();
    ps.close();
    conn.close();
    if(ans == 1) {
        return true;
    }
    else {
        return false;
    }
    }
    int JudgeManager() {

        try {
            if(login(this.manager)) {
                System.out.println("登录成功");
                return 1;
            }else {
                return 0;
            }
        }catch(Exception e) {
            //e.printStackTrace();
            //System.out.println("!!!!!!!!!");
        }
        return 0;

    }
}

3.ManagerRegister.java(注册界面)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

public class ManagerRegister extends JFrame{
    ManagerRegister () {
        init();
    }
    void init() {
        JFrame frame = new JFrame("注册管理员账号");
        frame.setLayout(null);

        JLabel IDStr = new JLabel("账号:");
        IDStr.setBounds(200, 70, 100, 25);
        frame.add(IDStr);

        JLabel passwdStr = new JLabel("密码:");
        passwdStr.setBounds(200, 120, 100, 25);
        frame.add(passwdStr);

        JLabel confrimStr = new JLabel("确认密码:");
        confrimStr.setBounds(200, 170, 100, 25);
        frame.add(confrimStr);

        JTextField userID = new JTextField();
        userID.setBounds(270, 70, 150, 25);
        frame.add(userID);

        JPasswordField password = new JPasswordField();
        password.setBounds(270, 120, 150, 25);
        frame.add(password);

        JPasswordField confrimPassword = new JPasswordField();
        confrimPassword.setBounds(270, 170, 150, 25);
        frame.add(confrimPassword);

        JButton buttonregister = new JButton("注册");
        buttonregister.setBounds(275, 220, 70, 25);
        frame.add(buttonregister);

        frame.setBounds(800, 300, 600, 360);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        //为注册按钮增加监听器
        buttonregister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String ID = userID.getText();
                String passwd = new String (password.getPassword());
                String confrimpasswd = new String (confrimPassword.getPassword());

                //创建Register类
                Register register = new Register();
                register.setID(ID);
                register.setPasswd(passwd);
                register.setconfirmpasswd(confrimpasswd);

                //如果注册成功,返回登录界面
                try {
                    if(register.JudgeRegister()) {

                        frame.setVisible(false);
                        Enter enter = new Enter();
                    }
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    //e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }

        });
    }
}

4.Register.java(验证注册)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.swing.JOptionPane;

public class Register {
    String ID;
    String passwd;
    String confirmpasswd;

    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/goods management?serverTimezone=UTC&characterEncoding=utf-8";
    private String user = "root";
    private String sqlpassword = "password";

    void setID(String ID) {
        this.ID = ID;
    }
    void setPasswd(String passwd) {
        this.passwd = passwd;
    }
    void setconfirmpasswd(String confirmpasswd) {
        this.confirmpasswd = confirmpasswd;
    }


    //判断注册的账号是否符合规则
    boolean JudgeRegister() throws SQLException, ClassNotFoundException {

        if(this.ID.equals("")) {
            JOptionPane.showMessageDialog(null, "账号不能为空!", "账号为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if(this.passwd.equals("")) {
            JOptionPane.showMessageDialog(null, "密码不能为空!", "密码为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if(!this.passwd.equals(this.confirmpasswd)) {
            JOptionPane.showMessageDialog(null, "两次输入的密码不一致!", "密码不一致", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        //符合规则,弹出注册成功的窗口,并将账号添加数据库
        JOptionPane.showMessageDialog(null, "注册成功");
        addManager();
        return true;
    }

    //向数据库添加Admin账户
    void addManager() throws ClassNotFoundException, SQLException {
        String sql="insert into manager (id, passwd) values (?,?)";
        Class.forName(driver);
        try {
            Connection conn = DriverManager.getConnection(url, user, sqlpassword);
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, this.ID);
            ps.setString(2, this.passwd);
            ps.executeUpdate();
            ps.close();
            conn.close();

        }catch(SQLException ex) {
            System.out.println("添加用户失败!");
        }
    }
}

5.Good.java(商品类)

import java.util.Date;

//商品类
public class Good {
    private String name;
    private String kind;
    private String indate;
    private double price;
    private int quantity;
    private String note;
    void setName(String name) {
        this.name=name;
    }
    void setKind(String kind) {
        this.kind=kind;
    }
    void setIndate(String indate) {
        this.indate=indate;
    }
    void setPrice(double price) {
        this.price=price;
    }
    void setQuantity(int quantity) {
        this.quantity=quantity;
    }
    void setNote(String note) {
        this.note=note;
    }
    String getName() {
        return this.name;
    }
    String getKind() {
        return this.kind;
    }
    String getIndate() {
        return this.indate;
    }
    double getPrice() {
        return this.price;
    }
    int getQuantity() {
        return this.quantity;
    }
    String getNote() {
        return this.note;
    }
}

6.Manager.java(管理员类)


//管理员类
public class Manager {
    private String id;
    private String passwd;
    void setID(String id) {
        this.id=id;
    }
    void setPasswd(String passwd) {
        this.passwd=passwd;
    }

    String getID() {
        return this.id;
    }
    String getPasswd() {
        return this.passwd;
    }
}

7.Show1.java(查询界面)

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.LinkedList;
import java.util.List;

public class Show1 {
    //获取数据库中的数据并以list返回
    public List<Good> getDbData() throws ClassNotFoundException, SQLException {
        //1,注册驱动信息
        Class.forName("com.mysql.cj.jdbc.Driver");
        //2,获取连接对象
        String url ="jdbc:mysql://localhost:3306/goods management?serverTimezone=UTC&characterEncoding=utf-8";
        Connection conn= DriverManager.getConnection(url,"root","password");
        String sql="select * from good";
        //3,连接对象conn的方法prepareStatement获取SQL语句的预编译对象
        PreparedStatement parameter =conn.prepareStatement(sql);
        //4,执行sql
        ResultSet result =parameter.executeQuery();
        //返回的数据List
        LinkedList<Good> list =new LinkedList<>();
        while (result.next()){

            Good stu_info =new Good();
            stu_info.setName(result.getString("name"));
            stu_info.setKind(result.getString("kind"));
            stu_info.setIndate(result.getString("indate"));
            stu_info.setPrice(Double.parseDouble(result.getString("price")));
            stu_info.setQuantity(Integer.parseInt(result.getString("quantity")));
            stu_info.setNote(result.getString("note"));
            list.add(stu_info);
        }
        result.close();
        parameter.close();
        conn.close();

        return list;

    }
    //创建窗口,以列表展示从数据库中获取的数据
        public void showFrame(List<Good> list){

        //1,设定窗口
        JFrame frame =new JFrame("商品信息展示");
        frame.setLocation(700,400);
        frame.setSize(500, 300);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

        //2,添加table
        JTable table =null;
        String [] index={"商品名称","商品种类","入库时间","商品价格","商品数量","商品备注"};
        Object [][] data=new Object[list.size()][index.length];
        //3,向data中添加数据
        for (int i = 0; i < list.size(); i++) {
            Good good =list.get(i);
            data[i][0]=good.getName();
            data[i][1]=good.getKind();
            data[i][2]=good.getIndate();
            data[i][3]=good.getPrice();
            data[i][4]=good.getQuantity();
            data[i][5]=good.getNote();
        }
        //4,创建一个默认的表格模型
        DefaultTableModel defaultModel = new DefaultTableModel(data, index);
        table=new JTable(defaultModel);
        table.setPreferredScrollableViewportSize(new Dimension(100, 80));//JTable的高度和宽度按照设定
        table.setFillsViewportHeight(true);

        //5,给表格设置滚动条
        JScrollPane jScrollPane = new JScrollPane();
        jScrollPane.setViewportView(table);

        Font font = new Font("宋体", Font.BOLD, 13);

        //添加button
        JButton button =new JButton("查询商品信息");
        button.setBounds(20,10,50,30);

        JButton button1 =new JButton("出库商品");
        button1.setBounds(30,10,50,30);

        JButton button2 =new JButton("入库商品");
        button2.setBounds(40,10,50,30);

        JButton button3 =new JButton("修改商品信息");
        button3.setBounds(50,10,50,30);

        //通过panel组合button,label
        JPanel panel =new JPanel();
        panel.setSize(200,100);
        panel.add(button);
        panel.add(button1);
        panel.add(button2);
        panel.add(button3);

        //6,添加表格、滚动条到容器中
        frame.add(panel, BorderLayout.NORTH);
        frame.setVisible(true);


        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.add(jScrollPane,BorderLayout.CENTER);
                frame.setVisible(true);
            }
        });
        //为添加商品按钮添加监听器
        button2.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 //添加页面
                 frame.setVisible(false);
                 Addgood addgood = new  Addgood();
             }
         }
        );
        //为删除商品按钮添加监听器
        button1.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               //删除页面
               frame.setVisible(false);
               Deletegood deletegood = new Deletegood();
           }
       }
        );
        //为更新商品按钮添加监听器
        button3.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
              //删除页面
              frame.setVisible(false);
              Modifygood modifygood = new Modifygood();
          }
      }
        );
    }
}

8.Addgood.java(添加界面)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Addgood extends JFrame {
    Addgood () {
        init();
    }
    void init(){
        JFrame frame = new JFrame("添加商品");
        frame.setLayout(null);

        JLabel nameStr = new JLabel("商品名称:");
        nameStr.setBounds(200, 70, 100, 25);
        frame.add(nameStr);

        JLabel kindStr = new JLabel("商品种类:");
        kindStr.setBounds(200, 120, 100, 25);
        frame.add(kindStr);

        JLabel dateStr = new JLabel("入库时间:");
        dateStr.setBounds(200, 170, 100, 25);
        frame.add(dateStr);

        JLabel priceStr = new JLabel("商品价格:");
        priceStr.setBounds(200, 220, 100, 25);
        frame.add(priceStr);

        JLabel quantityStr = new JLabel("商品数量:");
        quantityStr.setBounds(200, 270, 100, 25);
        frame.add(quantityStr);

        JLabel noteStr = new JLabel("商品备注:");
        noteStr.setBounds(200, 320, 100, 25);
        frame.add(noteStr);

        JTextField goodname = new JTextField();
        goodname.setBounds(270, 70, 150, 25);
        frame.add(goodname);

        JTextField goodkind = new JTextField();
        goodkind.setBounds(270, 120, 150, 25);
        frame.add(goodkind);

        JTextField goodindate = new JTextField();
        goodindate.setBounds(270, 170, 150, 25);
        frame.add(goodindate);

        JTextField goodprice = new JTextField();
        goodprice.setBounds(270, 220, 150, 25);
        frame.add(goodprice);

        JTextField goodquantity = new JTextField();
        goodquantity.setBounds(270, 270, 150, 25);
        frame.add(goodquantity);

        JTextField goodnote = new JTextField();
        goodnote.setBounds(270, 320, 150, 25);
        frame.add(goodnote);

        JButton buttonregister = new JButton("添加");
        buttonregister.setBounds(270, 370, 70, 25);
        frame.add(buttonregister);

        JButton buttonreturn = new JButton("返回");
        buttonreturn.setBounds(370, 370, 70, 25);
        frame.add(buttonreturn);

        frame.setBounds(800, 200, 600, 560);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        //为添加按钮增加监听器
        buttonregister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = goodname.getText();
                String kind = goodkind.getText();
                String indate = goodindate.getText();
                String price =goodprice.getText();
                String quantity =goodquantity.getText();
                String note = goodnote.getText();

                //创建Goodinsql类
                Goodinsql goodinsql = new Goodinsql();
                goodinsql.setName(name);
                goodinsql.setKind(kind);
                goodinsql.setIndate(indate);
                goodinsql.setPrice(price);
                goodinsql.setQuantity(quantity);
                goodinsql.setNote(note);

                //如果添加成功,返回商品查询界面
                try {
                    if(goodinsql.Judgein()) {
                        frame.setVisible(false);
                        Show1 show1 = new Show1();
                        try {
                            show1.showFrame(show1.getDbData());
                        } catch (ClassNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    //e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }

        });
        //返回
        buttonreturn.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               frame.setVisible(false);
               Show1 show1 = new Show1();
               try {
                   show1.showFrame(show1.getDbData());
               } catch (ClassNotFoundException ex) {
                   ex.printStackTrace();
               } catch (SQLException ex) {
                   ex.printStackTrace();
               }
           }
       }
        );
    }
}

9.Deletegood.java(删除界面)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Deletegood {
    Deletegood(){init();}
    void init(){
        JFrame frame = new JFrame("删除商品");
        frame.setLayout(null);

        JLabel nameStr = new JLabel("请输入要删除的商品名称:");
        nameStr.setBounds(170, 70, 200, 25);
        frame.add(nameStr);

        JTextField goodname = new JTextField();
        goodname.setBounds(170, 120, 150, 25);
        frame.add(goodname);

        JLabel quantityStr = new JLabel("请输入要删除的商品数量:");
        quantityStr.setBounds(170, 170, 200, 25);
        frame.add(quantityStr);

        JTextField goodquantity = new JTextField();
        goodquantity.setBounds(170, 220, 150, 25);
        frame.add(goodquantity);

        JButton buttonregister = new JButton("删除");
        buttonregister.setBounds(170, 270, 70, 25);
        frame.add(buttonregister);

        JButton buttonreturn = new JButton("返回");
        buttonreturn.setBounds(270, 270, 70, 25);
        frame.add(buttonreturn);

        frame.setBounds(800, 200, 500, 460);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        //为删除按钮增加监听器
        buttonregister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = goodname.getText();
                String quantity = goodquantity.getText();
                //创建Goodinsql类
                Goodinsql goodinsql = new Goodinsql();
                goodinsql.setName(name);
                goodinsql.setQuantity(quantity);
                //如果删除成功,返回商品查询界面
                try {
                    if(goodinsql.Judgedelete()) {
                        frame.setVisible(false);
                        Show1 show1 = new Show1();
                        try {
                            show1.showFrame(show1.getDbData());
                        } catch (ClassNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    //e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }

        });
        //返回
        buttonreturn.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               frame.setVisible(false);
               Show1 show1 = new Show1();
               try {
                   show1.showFrame(show1.getDbData());
               } catch (ClassNotFoundException ex) {
                   ex.printStackTrace();
               } catch (SQLException ex) {
                   ex.printStackTrace();
               }
           }
       }
       );
    }
}

10.Modifygood.java(修改界面)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Modifygood extends JFrame {
    Modifygood () {
        init();
    }
    void init(){
        JFrame frame = new JFrame("修改商品");
        frame.setLayout(null);

        JLabel nameStr = new JLabel("商品名称:");
        nameStr.setBounds(200, 70, 100, 25);
        frame.add(nameStr);

        JLabel kindStr = new JLabel("商品种类:");
        kindStr.setBounds(200, 120, 100, 25);
        frame.add(kindStr);

        JLabel dateStr = new JLabel("入库时间:");
        dateStr.setBounds(200, 170, 100, 25);
        frame.add(dateStr);

        JLabel priceStr = new JLabel("商品价格:");
        priceStr.setBounds(200, 220, 100, 25);
        frame.add(priceStr);

        JLabel quantityStr = new JLabel("商品数量:");
        quantityStr.setBounds(200, 270, 100, 25);
        frame.add(quantityStr);

        JLabel noteStr = new JLabel("商品备注:");
        noteStr.setBounds(200, 320, 100, 25);
        frame.add(noteStr);

        JTextField goodname = new JTextField();
        goodname.setBounds(270, 70, 150, 25);
        frame.add(goodname);

        JTextField goodkind = new JTextField();
        goodkind.setBounds(270, 120, 150, 25);
        frame.add(goodkind);

        JTextField goodindate = new JTextField();
        goodindate.setBounds(270, 170, 150, 25);
        frame.add(goodindate);

        JTextField goodprice = new JTextField();
        goodprice.setBounds(270, 220, 150, 25);
        frame.add(goodprice);

        JTextField goodquantity = new JTextField();
        goodquantity.setBounds(270, 270, 150, 25);
        frame.add(goodquantity);

        JTextField goodnote = new JTextField();
        goodnote.setBounds(270, 320, 150, 25);
        frame.add(goodnote);

        JButton buttonregister = new JButton("修改");
        buttonregister.setBounds(270, 370, 70, 25);
        frame.add(buttonregister);

        JButton buttonreturn = new JButton("返回");
        buttonreturn.setBounds(370, 370, 70, 25);
        frame.add(buttonreturn);

        frame.setBounds(800, 200, 600, 560);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        //为添加按钮增加监听器
        buttonregister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = goodname.getText();
                String kind = goodkind.getText();
                String indate = goodindate.getText();
                String price =goodprice.getText();
                String quantity =goodquantity.getText();
                String note = goodnote.getText();

                //创建Goodinsql类
                Goodinsql goodinsql = new Goodinsql();
                goodinsql.setName(name);
                goodinsql.setKind(kind);
                goodinsql.setIndate(indate);
                goodinsql.setPrice(price);
                goodinsql.setQuantity(quantity);
                goodinsql.setNote(note);


                try {
                    if(goodinsql.Judgemodify()) {
                        frame.setVisible(false);
                        Show1 show1 = new Show1();
                        try {
                            show1.showFrame(show1.getDbData());
                        } catch (ClassNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (SQLException ex) {
                            ex.printStackTrace();
                        }
                    }
                } catch (SQLException e1) {
                    // TODO Auto-generated catch block
                    //e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

            }

        });
        //返回
        buttonreturn.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {
               frame.setVisible(false);
               Show1 show1 = new Show1();
               try {
                   show1.showFrame(show1.getDbData());
               } catch (ClassNotFoundException ex) {
                   ex.printStackTrace();
               } catch (SQLException ex) {
                   ex.printStackTrace();
               }
           }
       }
        );
    }
}

11.Goodinsql.java(修改数据库商品信息)

import javax.swing.*;
import java.sql.*;
import java.util.Date;
import java.util.Objects;

//向数据库添加商品、修改商品、删除商品
public class Goodinsql {

    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://localhost:3306/goods management?serverTimezone=UTC&characterEncoding=utf-8";
    private String user = "root";
    private String password = "password";

    String name;
    String kind;
    String indate;
    double price;
    int quantity;
    String note;

    void setName(String name){this.name = name;}
    void setKind(String kind){this.kind = kind;}
    void setIndate(String indate){this.indate = indate;}
    void setPrice(String price){
        if(!Objects.equals(price, "")) {this.price = Double.parseDouble(price);}
        else {this.price = -1;}
    }
    void setQuantity(String quantity){
        if(!Objects.equals(quantity, "")) {this.quantity = Integer.parseInt(quantity);}
        else {this.quantity = -1;}
    }
    void setNote(String note){this.note = note;}

    //判断添加商品是否符合规则
    boolean Judgein() throws SQLException, ClassNotFoundException {
        if(this.name.equals("")) {
            JOptionPane.showMessageDialog(null, "商品名称不能为空!", "商品名称为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if(this.kind.equals("")) {
            JOptionPane.showMessageDialog(null, "商品种类不能为空!", "商品种类为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if(this.indate.equals("")) {
            JOptionPane.showMessageDialog(null, "商品入库时间不能为空!", "商品入库时间为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if(this.price==-1) {
            JOptionPane.showMessageDialog(null, "商品价格不能为空!", "商品价格为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        if(this.quantity==-1) {
            JOptionPane.showMessageDialog(null, "商品数量不能为空!", "商品数量为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        //符合规则,弹出添加成功的窗口,并将账号添加数据库
        JOptionPane.showMessageDialog(null, "添加成功");
        addgood();
        return true;
    }
    boolean Judgemodify() throws SQLException, ClassNotFoundException {
        if(this.name.equals("")) {
            JOptionPane.showMessageDialog(null, "商品名称不能为空!", "商品名称为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        String sql="select * from good where name=?";

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1, this.name);
        ResultSet rs = ps.executeQuery();
        int ans = 0;
        if(rs.next()) {
            ans = 1;
        }
        rs.close();
        ps.close();
        conn.close();
        if(ans == 1) {
            JOptionPane.showMessageDialog(null, "商品修改成功");
            modifygood();
            return true;
        }
        else {
            JOptionPane.showMessageDialog(null, "查无此物!");
            return false;
        }
    }
    //判断删除条件
    boolean Judgedelete() throws SQLException, ClassNotFoundException{
        if(this.name.equals("")) {
            JOptionPane.showMessageDialog(null, "商品名称不能为空!", "商品名称为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        if(this.quantity == -1) {
            JOptionPane.showMessageDialog(null, "商品数量不能为空!", "商品数量为空", JOptionPane.ERROR_MESSAGE);
            return false;
        }

        String sql="select * from good where name=?";

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);
        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1, this.name);
        ResultSet rs = ps.executeQuery();
        int ans = 0;
        int q = 0;
        if(rs.next()) {//如果此商品存在 出库数量大于库存则出口失败 等于则删除商品 小于则出库对应数量
            ans = 1;
            q = Integer.parseInt(rs.getString("quantity"));
        }
        rs.close();
        ps.close();
        conn.close();
        if(ans == 1) {
            if(q<this.quantity)
            {
                JOptionPane.showMessageDialog(null, "库存不足!");
                return false;
            }
            else if (q == this.quantity)
            {
                JOptionPane.showMessageDialog(null, "商品删除成功");
                deletegood();
                return true;
            }
            else{
                JOptionPane.showMessageDialog(null, "出库成功");
                deletegood1();
                return true;
            }
        }
        else {
            JOptionPane.showMessageDialog(null, "查无此物!");
            return false;
        }
    }

    void addgood() throws ClassNotFoundException, SQLException {
        String sql1="select * from good where name=?";

        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);
        PreparedStatement ps = conn.prepareStatement(sql1);

        ps.setString(1, this.name);
        ResultSet rs = ps.executeQuery();
        int ans = 0;
        if(rs.next()) {
            ans = 1;
        }
        rs.close();
        ps.close();
        conn.close();
        if(ans == 1) {//如果存在同名商品则更新商品信息切叠加商品数量
            String sql2="update good set kind=?,indate=?,price=?,note=? where name=?";
            String sql3="update good set quantity=quantity+? where name=?";
            Class.forName(driver);
            try {
                Connection conn1 = DriverManager.getConnection(url, user, password);
                PreparedStatement ps1 = conn1.prepareStatement(sql2);
                ps1.setString(1, this.kind);
                ps1.setString(2, this.indate);
                ps1.setString(3, String.valueOf(this.price));
                ps1.setString(4, this.note);
                ps1.setString(5, String.valueOf(this.name));
                PreparedStatement ps2 = conn1.prepareStatement(sql3);
                ps2.setString(1, String.valueOf(this.quantity));
                ps2.setString(2, String.valueOf(this.name));
                ps1.executeUpdate();
                ps1.close();
                ps2.executeUpdate();
                ps2.close();
                conn1.close();

            }catch(SQLException ex) {
                System.out.println("添加商品失败!");
                System.out.println(ex);
            }
        }
        else {
            String sql="insert into good (name,kind,indate,price,quantity,note) values (?,?,?,?,?,?)";
            Class.forName(driver);
            try {
                Connection conn1 = DriverManager.getConnection(url, user, password);
                PreparedStatement ps1 = conn1.prepareStatement(sql);
                ps1.setString(1, this.name);
                ps1.setString(2, this.kind);
                ps1.setString(3, this.indate);
                ps1.setString(4, String.valueOf(this.price));
                ps1.setString(5, String.valueOf(this.quantity));
                ps1.setString(6, this.note);
                ps1.executeUpdate();
                ps1.close();
                conn1.close();

            }catch(SQLException ex) {
                System.out.println("添加商品失败!");
            }
        }
    }
    void modifygood() throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        Connection conn = DriverManager.getConnection(url, user, password);
        String s1 = "";
        if (!Objects.equals(this.kind, ""))
        {
            s1 = "update good set kind=? where name=?";
            Class.forName(driver);
            try {
                Connection conn1 = DriverManager.getConnection(url, user, password);
                PreparedStatement ps1 = conn1.prepareStatement(s1);
                ps1.setString(1, this.kind);
                ps1.setString(2, String.valueOf(this.name));
                ps1.executeUpdate();
                ps1.close();
                conn1.close();

            }catch(SQLException ex) {
                System.out.println("更新信息失败!");
            }
        }
        if (!Objects.equals(this.indate, ""))
        {
            s1 = "update good set indate=? where name=?";
            Class.forName(driver);
            try {
                Connection conn1 = DriverManager.getConnection(url, user, password);
                PreparedStatement ps1 = conn1.prepareStatement(s1);
                ps1.setString(1, this.indate);
                ps1.setString(2, String.valueOf(this.name));
                ps1.executeUpdate();
                ps1.close();
                conn1.close();

            }catch(SQLException ex) {
                System.out.println("更新信息失败!");
            }
        }
        if(this.price != -1)
        {
            s1 = "update good set price=? where name=?";
            Class.forName(driver);
            try {
                Connection conn1 = DriverManager.getConnection(url, user, password);
                PreparedStatement ps1 = conn1.prepareStatement(s1);
                ps1.setString(1, String.valueOf(this.price));
                ps1.setString(2, String.valueOf(this.name));
                ps1.executeUpdate();
                ps1.close();
                conn1.close();

            }catch(SQLException ex) {
                System.out.println("更新信息失败!");
            }
        }
        if (this.quantity != -1)
        {
            s1 = "update good set quantity=? where name=?";
            Class.forName(driver);
            try {
                Connection conn1 = DriverManager.getConnection(url, user, password);
                PreparedStatement ps1 = conn1.prepareStatement(s1);
                ps1.setString(1, String.valueOf(this.quantity));
                ps1.setString(2, String.valueOf(this.name));
                ps1.executeUpdate();
                ps1.close();
                conn1.close();

            }catch(SQLException ex) {
                System.out.println("更新信息失败!");
                System.out.println(ex);
            }
        }
        if (!Objects.equals(this.note, ""))
        {
            s1 = "update good set note=? where name=?";
            Class.forName(driver);
            try {
                Connection conn1 = DriverManager.getConnection(url, user, password);
                PreparedStatement ps1 = conn1.prepareStatement(s1);
                ps1.setString(1, this.note);
                ps1.setString(2, String.valueOf(this.name));
                ps1.executeUpdate();
                ps1.close();
                conn1.close();

            }catch(SQLException ex) {
                System.out.println("更新信息失败!");
            }
        }

    }
    void deletegood() throws ClassNotFoundException, SQLException {
        String sql="delete from good where name=?";
        Class.forName(driver);
        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, this.name);
            ps.executeUpdate();
            ps.close();
            conn.close();

        }catch(SQLException ex) {
            System.out.println("删除商品失败!");
        }
    }
    void deletegood1() throws ClassNotFoundException, SQLException {
        String sql="update good set quantity=quantity-? where name=?";
        Class.forName(driver);
        try {
            Connection conn = DriverManager.getConnection(url, user, password);
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, String.valueOf(this.quantity));
            ps.setString(2, String.valueOf(this.name));
            ps.executeUpdate();
            ps.close();
            conn.close();

        }catch(SQLException ex) {
            System.out.println("删除商品失败!");
            System.out.println(ex);
        }
    }
}
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值