数据库
首页 > 数据库> > 基于Swing和Mysql的简单的仓库管理系统

基于Swing和Mysql的简单的仓库管理系统

作者:互联网

本系统包含两个模块:仓库模块、记录模块

仓库模块功能:入库,出库,修改,删除,查询
记录模块功能:查询

接下来展示代码:

首先确定涉及到两个表,那就要做两个Bean类

下载链接

public class Goods {
    private int id;
    private String name;
    private int number;

    public Goods(){
        super();
    }

    public Goods(int id, String name, int number) {
        this.id = id;
        this.name = name;
        this.number = number;
    }
    public Goods(String name, int number) {

        this.name = name;
        this.number = number;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

import java.time.LocalDate;

public class Lists {

    private int id;
    private String name;
    private double score;
    private LocalDate birth;
    private String city;

    public String getWay() {
        return way;
    }

    public void setWay(String way) {
        this.way = way;
    }

    private String way;




    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public LocalDate getBirth() {
        return birth;
    }

    public void setBirth(LocalDate birth) {
        this.birth = birth;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }



    public Lists(String name, double score, LocalDate birth, String city,String way) {
        super();
        this.name = name;
        this.score = score;
        this.birth = birth;
        this.city = city;
        this.way = way;

    }

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", score=" + score + ", birth=" + birth + ", city=" + city
                +", way=" + way
                + "]";
    }

    public Lists() {
        super();
    }

}

Util类

既然涉及到数据库,那就离不开JDBC,所以首先要创建工具类,同时还有一个数据处理的工具类
import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtil {
    public static final ComboPooledDataSource ds=new ComboPooledDataSource();

    static
    {
        //加载连接数据库的信息
        try {
            InputStream is =new FileInputStream("jdbc.properties");
            Properties porp =new Properties();
            porp.load(is);
            is.close();
            //获取连接数据库信息
            String user =porp.getProperty("user");
            String pwd =porp.getProperty("pwd");
            String url=porp.getProperty("url");
            String driver =porp.getProperty("driver");
            //设置链接库的信息
            ds.setUser(user);
            ds.setDriverClass(driver);
            ds.setJdbcUrl(url);
            ds.setPassword(pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void release(Connection conn) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

public class DateUtil {

    //将日期转换为字符串
    public static LocalDate dateToLocalDate(Date date) {
        return LocalDate.ofEpochDay(date.getTime()/86400000).plusDays(1);
    }

    public static Object[][] listToArray(List<Lists> stus){
        Object[][] data=new Object[stus.size()][];
        int size=stus.size();
        for(int i=0;i<size;i++) {
            Lists s = stus.get(i);
            Object[] info =new Object[] {s.getId(),s.getName(),s.getScore(),s.getBirth(),s.getCity(),s.getWay()};
            data[i]=info;
        }
        return data;
    }
    public static Object[][] listToArray2(List<Goods> stus){
        Object[][] data=new Object[stus.size()][];
        int size=stus.size();
        for(int i=0;i<size;i++) {
            Goods s = stus.get(i);
            Object[] info =new Object[] {s.getId(),s.getName(),s.getNumber()};
            data[i]=info;
        }
        return data;
    }
}

接下来是Dao层

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ListDao {
    public void add(Lists s) throws SQLException {

        Connection conn = JdbcUtil.getConnection();
        String sql="insert into stus values(null,?,?,?,?,?)";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, s.getName());
        ps.setDouble(2, s.getScore());
        ps.setObject(3, s.getBirth());
        ps.setString(4, s.getCity());
        ps.setString(5, s.getWay());

        ps.executeUpdate();

        JdbcUtil.release(conn);
    }

    public void add2(Goods s) throws SQLException {

        Connection conn = JdbcUtil.getConnection();
        String sql="insert into good values(null,?,?)";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, s.getName());
        ps.setInt(2, s.getNumber());
        System.out.println(s.getName()+"..."+s.getNumber());
        ps.executeUpdate();
        JdbcUtil.release(conn);
    }



    public void detele2(int id) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql="delete from good where id=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setInt(1, id);
        ps.executeUpdate();
        JdbcUtil.release(conn);
    }


    public void update2(Goods s) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql="update good set name=?,score=? where id=?";
        PreparedStatement ps=conn.prepareStatement(sql);
        ps.setString(1, s.getName());
        ps.setInt(2, s.getNumber());
//        ps.setObject(3, s.getBirth());
//        ps.setString(4, s.getCity());
        ps.setInt(3, s.getId());
        ps.executeUpdate();
        JdbcUtil.release(conn);
    }

    public void update3(Goods s) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql="UPDATE good SET score = score-? WHERE NAME=?";
        PreparedStatement ps=conn.prepareStatement(sql);

        ps.setDouble(1, s.getNumber());
        ps.setString(2, s.getName());
        ps.executeUpdate();
        JdbcUtil.release(conn);
    }

    public void update4(Goods s) throws SQLException {
        Connection conn = JdbcUtil.getConnection();
        String sql="UPDATE good SET score = score+? WHERE NAME=?";
        PreparedStatement ps=conn.prepareStatement(sql);

        ps.setDouble(1, s.getNumber());
        ps.setString(2, s.getName());
        ps.executeUpdate();
        JdbcUtil.release(conn);
    }

    public List<Lists> queryAll() throws SQLException{
        List<Lists> stus=new ArrayList<>();
        Connection conn = JdbcUtil.getConnection();

        String sql="select * from stus";
        PreparedStatement ps=conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while(rs.next()) {
            Lists s=new Lists();
            s.setId(rs.getInt(1));
            s.setName(rs.getString(2));
            s.setScore(rs.getDouble(3));
            s.setBirth(DateUtil.dateToLocalDate(rs.getDate(4)));
            s.setCity(rs.getString(5));
            s.setWay(rs.getString(6));

            stus.add(s);
        }
        JdbcUtil.release(conn);
        return stus;
    }

    public List<Goods> queryAll2() throws SQLException{
        List<Goods> stus=new ArrayList<>();
        Connection conn = JdbcUtil.getConnection();
        String sql="select * from good";
        PreparedStatement ps=conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while(rs.next()) {
            Goods g = new Goods();
            g.setId(rs.getInt(1));
            g.setName(rs.getString(2));
            g.setNumber(rs.getInt(3));
            stus.add(g);
        }
        JdbcUtil.release(conn);
        return stus;
    }
}

Service层

import java.sql.SQLException;
import java.util.List;

public class ListService {
    public List<Lists> queryAll(){
        try {
            return new ListDao().queryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    public List<Goods> queryAll2(){
        try {
            return new ListDao().queryAll2();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public int update2(Goods s) {
        ListDao sDao =new ListDao();
        try {
            sDao.update2(s);
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int update3(Goods s) {
        ListDao sDao =new ListDao();
        try {
            sDao.update3(s);
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int update4(Goods s) {
        ListDao sDao =new ListDao();
        try {
            sDao.update4(s);
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }



    public int delete2(int id) {
        ListDao sDao =new ListDao();
        try {
            sDao.detele2(id);
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int insert(Lists s) {
        ListDao sDao =new ListDao();
        try {
            sDao.add(s);
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int insert2(Goods s) {
        ListDao sDao =new ListDao();
        try {
            sDao.add2(s);
            return 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }
}

最后是显示界面及功能,包括主界面,以及增删改查的界面

import javax.swing.*;
import java.awt.*;

import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class MainFrame extends JFrame{

    private JPanel contentPane;
    private JTable table;
    JComboBox jcb1;
    private String[] columnCount= {"序号","名称","数量","日期","去向","出入库"};
    private String[] columnCount2= {"序号","设备名称","数量"};
    private List<Lists> list;
    private List<Goods> list2;
    public static Goods stu;
    public static MainFrame frame;

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    frame = new MainFrame();
                    //窗口居中
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public MainFrame() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 764, 469);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);



        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(29, 58, 692, 332);
        contentPane.add(scrollPane);

        table = new JTable();
        scrollPane.setViewportView(table);


        String []ct= {"仓库","记录"};
        jcb1=new JComboBox(ct);
        jcb1.setBounds(10,20,93,23);
        contentPane.add(jcb1);


        JButton button = new JButton("查询");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (jcb1.getSelectedItem().equals("记录")){
                    quaryAll();
                }

                else {
                    quaryAll2();
                }
            }
        });
        button.setBounds(110, 22, 93, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("添加");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new FromFjame().setVisible(true);
            }
        });
        button_1.setBounds(213, 22, 93, 23);
        contentPane.add(button_1);
        //全屏
//		setExtendedState(JFrame.MAXIMIZED_BOTH);

        JButton button_2 = new JButton("修改");
        button_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                update2();
                quaryAll2();
            }
        });
        button_2.setBounds(316, 22, 93, 23);
        contentPane.add(button_2);

        JButton button_3 = new JButton("删除");
        button_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 remove2();
                 quaryAll2();
            }
        });
        button_3.setBounds(419, 22, 93, 23);
        contentPane.add(button_3);

        JButton button_4 = new JButton("出库");
        button_4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new AddFjame().setVisible(true);
            }
        });
        button_4.setBounds(522, 22, 93, 23);
        contentPane.add(button_4);

        JButton button_5 = new JButton("入库");
        button_5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new OutFjame().setVisible(true);
            }
        });
        button_5.setBounds(625, 22, 93, 23);
        contentPane.add(button_5);

        jcb1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (jcb1.getSelectedItem().equals("记录")){
                    button_1.setVisible(false);
                    button_2.setVisible(false);
                    button_3.setVisible(false);
                    button_4.setVisible(false);
                    button_5.setVisible(false);
                }else {
                    button_1.setVisible(true);
                    button_2.setVisible(true);
                    button_3.setVisible(true);
                    button_4.setVisible(true);
                    button_5.setVisible(true);
                }
            }
        });
    }

    //记录查询
    public void quaryAll() {
        ListService ss=new ListService();
        list = ss.queryAll();
        if(list==null) {
            JOptionPane.showMessageDialog(null, "服务器繁忙");
            return;
        }
        Object[][] data = DateUtil.listToArray(list);
        table.setModel(new DefaultTableModel(data, columnCount));
    }

    //仓库查询
    public void quaryAll2() {
        ListService ss=new ListService();
        list2 = ss.queryAll2();
        if(list2==null) {
            JOptionPane.showMessageDialog(null, "服务器繁忙");
            return;
        }
        Object[][] data = DateUtil.listToArray2(list2);
        table.setModel(new DefaultTableModel(data, columnCount2));
    }



    private void remove2() {
        int i = table.getSelectedRow();
        Goods s = list2.get(i);
        int code = new ListService().delete2(s.getId());
        if(code==0) {
            JOptionPane.showMessageDialog(null, "删除成功");
            return;
        }else {
            JOptionPane.showMessageDialog(null,"删除失败");
        }
        quaryAll2();
    }

    //修改

    private void update2() {
        int i = table.getSelectedRow();
        stu = list2.get(i);
        new FromFjame().setVisible(true);
    }

}

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.time.LocalDate;

public class AddFjame extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_3;
    private JComboBox comboBox;
    private JComboBox comboBox_1;
    private JComboBox comboBox_2;



    public AddFjame() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 314, 436);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("出库");
        label.setFont(new Font("宋体", Font.PLAIN, 17));
        label.setBounds(118, 20, 78, 39);
        contentPane.add(label);

        JLabel label_1 = new JLabel("设备名称");
        label_1.setBounds(23, 71, 40, 15);
        contentPane.add(label_1);

        textField = new JTextField();
        textField.setBounds(87, 68, 155, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_2 = new JLabel("数量");
        label_2.setBounds(23, 128, 40, 15);
        contentPane.add(label_2);

        textField_1 = new JTextField();
        textField_1.setBounds(87, 125, 155, 21);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNewLabel = new JLabel("时间");
        lblNewLabel.setBounds(23, 191, 32, 15);
        contentPane.add(lblNewLabel);

        JLabel label_3 = new JLabel("去向");
        label_3.setBounds(23, 251, 32, 15);
        contentPane.add(label_3);

        textField_3 = new JTextField();
        textField_3.setBounds(87, 248, 155, 21);
        contentPane.add(textField_3);
        textField_3.setColumns(10);

        comboBox = new JComboBox();
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                addDay();
            }
        });
        comboBox.setBounds(87, 188, 54, 21);
        contentPane.add(comboBox);

        comboBox_1 = new JComboBox();
        comboBox_1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                addDay();
            }
        });
        comboBox_1.setBounds(151, 188, 47, 21);
        contentPane.add(comboBox_1);

        comboBox_2 = new JComboBox();
        comboBox_2.setBounds(202, 188, 40, 21);
        contentPane.add(comboBox_2);

        //选择框添加内容
        addBirth();

        JButton button = new JButton("出库");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    add();
                    update();

            }
        });
        button.setBounds(37, 325, 93, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("返回");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //每次返回清空信息
                MainFrame.stu=null;
                //退出
                dispose();
            }
        });
        button_1.setBounds(169, 325, 93, 23);
        contentPane.add(button_1);


    }


    //选择框填充内容
    private void addBirth() {
        int year=LocalDate.now().getYear();
        for(int i=year;i>=2000;i--) {
            comboBox.addItem(i);
        }
        for(int i=1;i<=12;i++) {
            comboBox_1.addItem(i);
        }
    }

    private void addDay() {
        int year=2020;
        int month=1;
        int day=0;
	    Object oYear = comboBox.getSelectedItem();
        Object oMonth =comboBox_1.getSelectedItem();
        if(oYear==null||oMonth==null) {
            return;
        }else {
            year=(int) oYear;
            month=(int) oMonth;
        }
        boolean flag=(year%4==0&&year%100!=0)||year%400==0;
        switch(month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day=31;
                break;
            case 2:
                day=flag?28:29;
                break;
            default:
                day=30;
                break;
        }
        comboBox_2.removeAllItems();
        for(int i=1;i<=day;i++) {
            comboBox_2.addItem(i);
        }
    }

    //增加
    private void add() {
        String way = "出库";
        String name=textField.getText();
        String strSouce=textField_1.getText();
        String city=textField_3.getText();
        int year=(int)comboBox.getSelectedItem();
        int month=(int) comboBox_1.getSelectedItem();
        int day=(int) comboBox_2.getSelectedItem();
        Lists s=new Lists(name,Double.parseDouble(strSouce),LocalDate.of(year, month, day),city,way);
        int insert = new ListService().insert(s);
        if(insert==0) {
            JOptionPane.showMessageDialog(null, "添加成功");
        }else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }

    //修改
    private void update() {
        String name=textField.getText();
        String strSouce=textField_1.getText();
        Goods stu = new Goods(name,Integer.parseInt(strSouce));

        int i = new ListService().update3(stu);

        if(i==0) {
            JOptionPane.showMessageDialog(null, "修改成功");
            return;
        }else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }


}

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FromFjame extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;


    public FromFjame() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 314, 236);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("添加设备");
        label.setFont(new Font("宋体", Font.PLAIN, 17));
        label.setBounds(118, 20, 78, 39);
        contentPane.add(label);

        JLabel label_1 = new JLabel("设备名称");
        label_1.setBounds(23, 71, 40, 15);
        contentPane.add(label_1);

        textField = new JTextField();
        textField.setBounds(87, 68, 155, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_2 = new JLabel("数量");
        label_2.setBounds(23, 128, 40, 15);
        contentPane.add(label_2);

        textField_1 = new JTextField();
        textField_1.setBounds(87, 125, 155, 21);
        contentPane.add(textField_1);
        textField_1.setColumns(10);



        JButton button = new JButton("添加");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(MainFrame.stu==null) {
                    add();
                }else {
                    update();
                }
            }
        });
        button.setBounds(37, 165, 93, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("返回");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //每次返回清空信息
                MainFrame.stu=null;
                //退出
                dispose();
            }
        });
        button_1.setBounds(169, 165, 93, 23);
        contentPane.add(button_1);

        //当点击的行数的信息不为空时,进行下面操作
        if(MainFrame.stu!=null) {
            textField.setText(MainFrame.stu.getName());
            textField_1.setText(MainFrame.stu.getNumber()+"");
            button.setText("修改");
        }
    }



    //增加
    private void add() {
        String way = "出库";
        String name=textField.getText();
        String strSouce=textField_1.getText();
        Goods s=new Goods(name,Integer.parseInt(strSouce));
        int insert = new ListService().insert2(s);
        if(insert==0) {
            JOptionPane.showMessageDialog(null, "添加成功");
            textField.setText("");
            textField_1.setText("");
            return;
        }else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }

    //修改
    private void update() {
        String name=textField.getText();
        String strSouce=textField_1.getText();
        MainFrame.stu.setName(name);
        MainFrame.stu.setNumber(Integer.parseInt(strSouce));
        int i = new ListService().update2(MainFrame.stu);
        if(i==0) {
            JOptionPane.showMessageDialog(null, "修改成功");
            MainFrame.stu=null;
            textField.setText("");
            textField_1.setText("");
            MainFrame.frame.quaryAll2();
            dispose();
            return;
        }else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }


}

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.time.LocalDate;

public class OutFjame extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_3;
    private JComboBox comboBox;
    private JComboBox comboBox_1;
    private JComboBox comboBox_2;



    public OutFjame() {
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 314, 436);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel label = new JLabel("入库");
        label.setFont(new Font("宋体", Font.PLAIN, 17));
        label.setBounds(118, 20, 78, 39);
        contentPane.add(label);

        JLabel label_1 = new JLabel("设备名称");
        label_1.setBounds(23, 71, 40, 15);
        contentPane.add(label_1);

        textField = new JTextField();
        textField.setBounds(87, 68, 155, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel label_2 = new JLabel("数量");
        label_2.setBounds(23, 128, 40, 15);
        contentPane.add(label_2);

        textField_1 = new JTextField();
        textField_1.setBounds(87, 125, 155, 21);
        contentPane.add(textField_1);
        textField_1.setColumns(10);

        JLabel lblNewLabel = new JLabel("时间");
        lblNewLabel.setBounds(23, 191, 32, 15);
        contentPane.add(lblNewLabel);

        JLabel label_3 = new JLabel("去向");
        label_3.setBounds(23, 251, 32, 15);
        contentPane.add(label_3);

        textField_3 = new JTextField();
        textField_3.setBounds(87, 248, 155, 21);
        contentPane.add(textField_3);
        textField_3.setColumns(10);

        comboBox = new JComboBox();
        comboBox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                addDay();
            }
        });
        comboBox.setBounds(87, 188, 54, 21);
        contentPane.add(comboBox);

        comboBox_1 = new JComboBox();
        comboBox_1.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent e) {
                addDay();
            }
        });
        comboBox_1.setBounds(151, 188, 47, 21);
        contentPane.add(comboBox_1);

        comboBox_2 = new JComboBox();
        comboBox_2.setBounds(202, 188, 40, 21);
        contentPane.add(comboBox_2);

        //选择框添加内容
        addBirth();

        JButton button = new JButton("入库");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                    add();
                    update();

            }
        });
        button.setBounds(37, 325, 93, 23);
        contentPane.add(button);

        JButton button_1 = new JButton("返回");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //每次返回清空信息
                MainFrame.stu=null;
                //退出
                dispose();
            }
        });
        button_1.setBounds(169, 325, 93, 23);
        contentPane.add(button_1);


    }


    //选择框填充内容
    private void addBirth() {
        int year=LocalDate.now().getYear();
        for(int i=year;i>=2000;i--) {
            comboBox.addItem(i);
        }
        for(int i=1;i<=12;i++) {
            comboBox_1.addItem(i);
        }
    }

    private void addDay() {
        int year=2020;
        int month=1;
        int day=0;
	    Object oYear = comboBox.getSelectedItem();
        Object oMonth =comboBox_1.getSelectedItem();
        if(oYear==null||oMonth==null) {
            return;
        }else {
            year=(int) oYear;
            month=(int) oMonth;
        }
        boolean flag=(year%4==0&&year%100!=0)||year%400==0;
        switch(month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day=31;
                break;
            case 2:
                day=flag?28:29;
                break;
            default:
                day=30;
                break;
        }
        comboBox_2.removeAllItems();
        for(int i=1;i<=day;i++) {
            comboBox_2.addItem(i);
        }
    }

    //增加
    private void add() {
        String way = "入库";
        String name=textField.getText();
        String strSouce=textField_1.getText();
        String city=textField_3.getText();
        int year=(int)comboBox.getSelectedItem();
        int month=(int) comboBox_1.getSelectedItem();
        int day=(int) comboBox_2.getSelectedItem();
        Lists s=new Lists(name,Double.parseDouble(strSouce),LocalDate.of(year, month, day),city,way);
        int insert = new ListService().insert(s);
        if(insert==0) {
            JOptionPane.showMessageDialog(null, "添加成功");

        }else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }

    //修改
    private void update() {
        String name=textField.getText();
        String strSouce=textField_1.getText();
        Goods stu = new Goods(name,Integer.parseInt(strSouce));

        int i = new ListService().update4(stu);

        if(i==0) {
            JOptionPane.showMessageDialog(null, "修改成功");
            return;
        }else {
            JOptionPane.showMessageDialog(null, "修改失败");
        }
    }


}

Mr.东方老赢 发布了7 篇原创文章 · 获赞 1 · 访问量 151 私信 关注

标签:管理系统,button,Swing,add,contentPane,Mysql,new,textField,public
来源: https://blog.csdn.net/qq_40181435/article/details/104577025