{"id":1113,"date":"2021-08-16T13:03:41","date_gmt":"2021-08-16T05:03:41","guid":{"rendered":"https:\/\/blog.odliken.top\/?p=1113"},"modified":"2025-03-18T16:46:39","modified_gmt":"2025-03-18T08:46:39","slug":"%e4%bb%93%e5%ba%93%e7%ae%a1%e7%90%86%e7%b3%bb%e7%bb%9f%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/blog.odjbinail.cn\/?p=1113","title":{"rendered":"\u4ed3\u5e93\u7ba1\u7406\u7cfb\u7edf\u603b\u7ed3"},"content":{"rendered":"<h2>8 \u6708 8 \u65e5\u665a 19.30 \u5de6\u53f3<\/h2>\n<p>1\u3001\u5546\u54c1\u754c\u9762<br \/>\n<img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210809102733076.png\"  alt=\"\" \/><\/p>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210809102733076.png\" alt=\"\" \/><\/p><\/noscript>\n<ul>\n<li>\n<p>\u5c06\u65b0\u589e\u3001\u66f4\u65b0\u3001\u5220\u9664\u6dfb\u52a0\u5230\u9762\u677f\uff0c\u987a\u5e8f\u653e\u5230 table \u8868\u4e0a\u9762<\/p>\n<\/li>\n<li>\n<p>\u6309\u987a\u5e8f\u6392\u5217\u5728 MyTableByGoods()\u4e0a\u9762\u3001\u67e5\u8be2-&gt;\u589e\u5220\u6539-&gt;\u8868\u683c<\/p>\n<\/li>\n<li>\n<p>\u6309\u94ae\u5de6\u5bf9\u9f50\uff1a<\/p>\n<pre class=\"prettyprint linenums\" ><code class=\"language-java\">this.setLayout(new FlowLayout(FlowLayout.LEFT));\nquery = new JPanel(new FlowLayout(FlowLayout.LEFT));\noption = new JPanel(new FlowLayout(FlowLayout.LEFT));<\/code><\/pre>\n<h2>8 \u6708 9 \u65e5\u5348 10 \u70b9 20 \u5de6\u53f3<\/h2>\n<\/li>\n<\/ul>\n<p>1\u3001\u5546\u54c1\u51fa\u5e93\u5165\u5e93\u4fe1\u606f\u754c\u9762<\/p>\n<p><img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210809133941431.png\"  alt=\"\" \/><br \/>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210809133941431.png\" alt=\"\" \/><br \/><\/noscript>\n2\u3001\u5546\u54c1\u66f4\u65b0\u529f\u80fd<\/p>\n<ul>\n<li>update.setEnabled(false); \/\/\u4f7f\u6309\u626d\u5931\u53bb\u5355\u51fb\u6548\u679c<\/li>\n<li>\u5728 GoodsDao \u7c7b\u4e2d\n<pre class=\"prettyprint linenums\" ><code class=\"language-java\">\/\/\u66f4\u65b0\npublic boolean update(Goods goods)\n{\n    boolean flag = false;\n    con = getCon();\n    try {\n        pst = con.prepareStatement(&quot;update goods set  category=?,price=?,sum=?,brand=? where name=?&quot;);\n        pst.setString(1, goods.getCategory());\n        pst.setDouble(2, goods.getPrice());\n        pst.setInt(3,goods.getSum());\n        pst.setString(4,goods.getBrand());\n\/\/where xx=\uff1f\u8981\u653e\u5728\u6700\u540e \uff0c\u95ee\u53f7\u5728\u7b2c\u51e0\u4e2a\u4f4d\u7f6e\u548c pst \u8bed\u53e5\u7684 12345 \u8981\u5bf9\u5e94\n        pst.setString(5, goods.getName());\n        pst.executeUpdate();\n        flag = true;\n    } catch (SQLException e) {\n        e.printStackTrace();\n    }\n    return flag;\n}<\/code><\/pre>\n<p>3\u3001\u767b\u5f55\u754c\u9762<\/p>\n<\/li>\n<\/ul>\n<p>4\u3001JComboBox \u5982\u4f55\u52a8\u6001\u7ed1\u5b9a\u6570\u636e\u5e93\uff1f\uff1f\u3001\uff1f<\/p>\n<pre class=\"prettyprint linenums\" ><code class=\"language-java\">Mytable \u7c7b\u4e2d\uff1a\n\/\/\u4e0b\u62c9\u6846\u540d\u79f0\ncon = getCon();\ntry {\n    pst = con.prepareStatement(&quot;select * from goods &quot;);\n    rs = pst.executeQuery();\n    Vector v = new Vector();\n    while (rs.next()) {\n        String list = rs.getString(&quot;name&quot;);\n        v.add(list);\n    }\n    query = new JPanel();\n    combName = new JComboBox&lt;String&gt;();\n    name = new JTextField(15);\n    add(query, BorderLayout.PAGE_START);\n    \/\/\u8bbe\u7f6e\u8fb9\u6846\n    query.setBorder(BorderFactory.createEtchedBorder());\n    combName.setModel(new DefaultComboBoxModel&lt;String&gt;(v));\n    \/\/\u5236\u4f5c\u76d1\u542c\u5668\n    ActionListener m = new ActionListener() {\n        @Override\n        public void actionPerformed(ActionEvent e) {\n            \/\/ TODO Auto-generated method stub\n            String item = (String) combName.getSelectedItem(); \/\/\u83b7\u53d6\u9879\u76ee\u540d\uff08\u9009\u62e9\u7684\u7701\u4efd\uff09\n            if (&quot;\u8bf7\u9009\u62e9\u5546\u54c1\uff1a&quot;.equals(item))\n                name.setText(&quot;&quot;);\n            else\n                name.setText(item);\n        }\n    };\n    \/\/\u6dfb\u52a0\u76d1\u542c\u5668\n    combName.addActionListener(m);\n    query.add(combName);\n    query.add(name);\n} catch (SQLException e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n<p><img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210809210848566.png\"  alt=\"\" \/><br \/>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210809210848566.png\" alt=\"\" \/><br \/><\/noscript>\n5\u3001\u5982\u4f55\u5728\u6570\u636e\u8868\u4e0a<strong>\u641c\u7d22\u8fc7\u6ee4\u6570\u636e<\/strong>\uff1f\uff1f<\/p>\n<pre class=\"prettyprint linenums\" ><code class=\"language-java\">public MyTableByGoods() throws SQLException, ClassNotFoundException {\n\/\/\u4e0b\u62c9\u6846\u7c7b\u578b\n        Vector v = new Vector();\n        v.add(&quot;\u8bf7\u9009\u62e9\u5546\u54c1\u7c7b\u578b&quot;);\n        v.add(&quot;\u624b\u673a&quot;);\n        v.add(&quot;\u7535\u8111&quot;);\n        v.add(&quot;\u76f8\u673a&quot;);\n        query = new JPanel();\n        inputCategory = new JLabel(&quot;\u7c7b\u578b&quot;);\n        query.add(inputCategory);\n        comboCategory = new JComboBox&lt;String&gt;();\n        add(query, BorderLayout.PAGE_START);\n        \/\/\u8bbe\u7f6e\u8fb9\u6846\n        query.setBorder(BorderFactory.createEtchedBorder());\n        comboCategory.setModel(new DefaultComboBoxModel&lt;String&gt;(v));\n        \/\/\u5236\u4f5c\u76d1\u542c\u5668\n        ActionListener m = new ActionListener() {\n            @Override\n            public void actionPerformed(ActionEvent e) {\n                String item = (String) comboCategory.getSelectedItem();\n                if (&quot;\u8bf7\u9009\u62e9\u5546\u54c1\u7c7b\u578b&quot;.equals(item)) {\n                    categoryQuery = &quot;&quot;;\n                } else {\n                    categoryQuery = item;\n                }\n            }\n        };\n        \/\/\u6dfb\u52a0\u76d1\u542c\u5668\n        comboCategory.addActionListener(m);\n        query.add(comboCategory);\n        query = new JPanel();\n        search = new JButton(&quot;\u641c\u7d22&quot;);\n        query.add(search);\n        search.addActionListener(this);\n}\n@Override\npublic void actionPerformed(ActionEvent e) {\n    JButton b = (JButton) e.getSource();\n    if (b == search) {\n            GoodsDao goodsDao = new GoodsDao();\n            goodsDao.searchData(dtm, name.getText(), categoryQuery);\n        }\n}\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\nGoodsDao \u7c7b\u4e2d\uff1a\npublic class GoodsDao {\n    public void searchData(DefaultTableModel dtm, String name, String category) {\n        String[][] comm = getGoodsList(name, category);\n        int index = dtm.getRowCount();\n        while (index != 1) {\n            try {\n                dtm.removeRow(0);\n                index = dtm.getRowCount();\n            } catch (RuntimeException e) {\n                e.printStackTrace();\n            }\n        }\n        for (int i = 0; i &lt; comm.length; i++) {\n            dtm.addRow(comm[i]);\n        }\n        dtm.removeRow(0);\n        dtm.fireTableDataChanged();\n    }\n\/\/\u83b7\u53d6\u6570\u636e\u8868\u7684\u6570\u636e\u3001\u641c\u7d22\u529f\u80fd\n    public String[][] getGoodsList(String name, String category) {\n        con = getCon();\n        try {\n            if (name.trim().equals(&quot;&quot;) &amp;&amp; category.trim().equals(&quot;&quot;)) {\n                pst = con.prepareStatement(&quot;select * from goods&quot;);\n            } else if (!name.trim().equals(&quot;&quot;) &amp;&amp; category.trim().equals(&quot;&quot;)) {\n                pst = con.prepareStatement(&quot;select * from goods where name like ?&quot;);\n                pst.setString(1, &quot;%&quot; + name.trim() + &quot;%&quot;);\n            } else if (name.trim().equals(&quot;&quot;) &amp;&amp; !category.trim().equals(&quot;&quot;)) {\n                pst = con.prepareStatement(&quot;select * from goods where category = ?&quot;);\n                pst.setString(1, category);\n            } else {\n                pst = con.prepareStatement(&quot;select * from goods where name like ? and category = ?&quot;);\n                pst.setString(1, &quot;%&quot; + name.trim() + &quot;%&quot;);\n                pst.setString(2, category);\n            }\n            rs = pst.executeQuery();\n            rs.last();\n            int row = rs.getRow();\/\/\u83b7\u53d6\u8bb0\u5f55\u7684\u603b\u6761\u6570\n            rs.beforeFirst();\/\/\u6e38\u6807\u56de\u5230\u7b2c\u4e00\u4e2a\u8bb0\u5f55\u524d\u7684\u4f4d\u7f6e\n            String comm[][] = new String[row][5];\/\/row \u884c\u30015 \u5217\n            rs.next();\/\/\u6e38\u6807\u56de\u5230\u7b2c\u4e00\u4e2a\u8bb0\u5f55\u7684\u4f4d\u7f6e\n            for (int i = 0; i &lt; row; i++) {\n                comm[i][0] = rs.getString(&quot;name&quot;);\n                comm[i][1] = rs.getString(&quot;category&quot;);\n                comm[i][2] = rs.getString(&quot;price&quot;);\n                comm[i][3] = rs.getString(&quot;sum&quot;);\n                comm[i][4] = rs.getString(&quot;brand&quot;);\n                rs.next();\n            }\n            rs.close();\n            return comm;\n        } catch (SQLException e) {\n            e.printStackTrace();\n            try {\n                pst.close();\n                con.close();\n            } catch (SQLException throwables) {\n                throwables.printStackTrace();\n            }\n        }\n        return null;\n    }<\/code><\/pre>\n<p><img data-original=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210810113546512.png\"  alt=\"\" \/><br \/>\n<noscript><img decoding=\"async\" src=\"https:\/\/blog.odjbinail.cn\/wp-content\/uploads\/2021\/08\/image-20210810113546512.png\" alt=\"\" \/><br \/><\/noscript>\n6\u3001\u6dfb\u52a0\u4e00\u6761\u51fa\u5165\u5e93\u4fe1\u606f\u3001\u548c\u5546\u54c1 sum \u5982\u4f55\u540c\u6b65<\/p>\n<pre class=\"prettyprint linenums\" ><code class=\"language-java\">\/\/\u6dfb\u52a0\u51fa\u5165\u5e93\u4fe1\u606f\npublic boolean addIn (OutIn outIn, Component parentComponent)\n{\n    boolean flag = false;\n    con = GetCon.getCon();\n    try {\n        PreparedStatement selectSum = con.prepareStatement(&quot;select sum from goods where id = ?&quot;);\n        selectSum.setString(1, outIn.getGoods_id());\n        rs = selectSum.executeQuery();\n        rs.next();\/\/\u6e38\u6807\u56de\u5230\u7b2c\u4e00\u4e2a\u8bb0\u5f55\u7684\u4f4d\u7f6e\n        int sum = rs.getInt(1);\n        selectSum.close();\n        if (sum &lt; outIn.getSum()&amp;&amp;outIn.getType().equals(&quot;\u51fa\u5e93&quot;)){\n            JOptionPane.showMessageDialog(parentComponent, &quot;\u8be5\u5546\u54c1\u51fa\u5e93\u6570\u91cf\u5927\u4e8e\u5e93\u5b58\u6570\u91cf\uff0c\u8bf7\u91cd\u65b0\u8f93\u5165\u5546\u54c1\u51fa\u5e93\u6570\u91cf\uff01\uff01\uff01&quot;);\n            return false;\n        }\n        pst = con.prepareStatement(&quot;insert into out_in values(null,?,?,?,?)&quot;);\n        pst.setString(1, outIn.getGoods_id());\n        pst.setString(2, outIn.getType());\n        pst.setString(3, outIn.getMerchant());\n        pst.setInt(4, outIn.getSum());\n        pst.execute();\n        PreparedStatement pstUpdate = con.prepareStatement(&quot;update goods set sum = ? where id = ?&quot;);\n        if (outIn.getType().equals(&quot;\u5165\u5e93&quot;)){\n            pstUpdate.setInt(1,sum+outIn.getSum());\n        }else {\n            pstUpdate.setInt(1,sum-outIn.getSum());\n        }\n        pstUpdate.setInt(2,Integer.parseInt(outIn.getGoods_id()));\n        pstUpdate.execute();\n        pstUpdate.close();\n        flag = true;\n        rs.close();\n        pst.close();\n        con.close();\n    } catch (SQLException e) {\n        e.printStackTrace();\n    }\n    return flag;\n}<\/code><\/pre>\n<p>7\u3001\u7a97\u53e3\u4e0d\u53ef\u8c03\u8282\u5927\u5c0f<\/p>\n<p>\u200b    this.setResizable(false);<\/p>\n<p>8\u3001\u672a\u8f93\u5165\u65f6\u5f39\u51fa\u5bf9\u8bdd\u6846\uff0c\u63d0\u793a\u8bf7\u8f93\u5165<\/p>\n<pre class=\"prettyprint linenums\" ><code class=\"language-java\">public Goods getGood() {\n        Goods goods = new Goods();\n        if (goodsName.getText().equals(&quot;&quot;)){\n            JOptionPane.showMessageDialog(this, &quot;\u8bf7\u8f93\u5165\u5546\u54c1\u540d\u79f0&quot;);\n            return null;\n        }else if(comboCategory.getSelectedItem().equals(&quot;\u8bf7\u9009\u62e9\u5546\u54c1\u7c7b\u578b&quot;)){\n            JOptionPane.showMessageDialog(this, &quot;\u8bf7\u9009\u62e9\u5546\u54c1\u7c7b\u578b&quot;);\n            return null;\n        }else if (goodsPrice.getText().trim().equals(&quot;&quot;)){\n            JOptionPane.showMessageDialog(this, &quot;\u8bf7\u8f93\u5165\u5546\u54c1\u4ef7\u683c&quot;);\n            return null;\n        }else if (goodsBrand.getText().trim().equals(&quot;&quot;)){\n            JOptionPane.showMessageDialog(this, &quot;\u8bf7\u8f93\u5165\u5546\u54c1\u54c1\u724c&quot;);\n            return null;\n        }\n        goods.setName(goodsName.getText());\n        goods.setCategory((String) comboCategory.getSelectedItem());\n        goods.setPrice(Double.parseDouble(goodsPrice.getText()));\n        goods.setBrand(goodsBrand.getText());\n        return goods;\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>8 \u6708 8 \u65e5\u665a 19.30 \u5de6\u53f3 1\u3001\u5546\u54c1\u754c\u9762 \u5c06\u65b0\u589e\u3001\u66f4\u65b0\u3001\u5220\u9664\u6dfb\u52a0\u5230\u9762\u677f\uff0c\u987a\u5e8f\u653e\u5230 table \u8868\u4e0a\u9762  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1113","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/1113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1113"}],"version-history":[{"count":1,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/1113\/revisions"}],"predecessor-version":[{"id":4978,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=\/wp\/v2\/posts\/1113\/revisions\/4978"}],"wp:attachment":[{"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.odjbinail.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}