netbeans 我想在mysql中使用jdatechooser在java中插入和更新日期

66bbxpm5  于 2022-11-10  发布在  Mysql
关注(0)|答案(1)|浏览(113)

我做了一个产品的形式,我必须添加产品的制造商和有效期在mysql数据库表。当保存按钮按下它插入数据在mysql数据库,但当我试图更新它显示我的错误

if(cb1.getSelectedItem().equals("Select Any Type"))
        {
            JOptionPane.showMessageDialog(null,"Please Select User type");
        }

        try
        {
              Class.forName("com.mysql.jdbc.Driver");
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/retailer","root","");
            java.util.Date date=new java.util.Date();
            int n=cb1.getSelectedIndex();
              String sql="Insert into stockable(CategoryID, ProductName, Quantity, SaleUnitPrice, CurrentPurchasePrice, ExpiryDate, MfturDate, StockThesoldQty, Description) values (?,?,?,?,?,?,?,?,?) ";
            PreparedStatement pst=conn.prepareStatement(sql);
             pst.setString(1,String.valueOf(n));
            pst.setString(2, productname.getText());
              pst.setString(3, quantity.getText());
                 pst.setString(4, saleprice.getText());
                 pst.setString(5, purchaseprice.getText());
                 pst.setString(6, (expirydate.getDateEditor().getUiComponent()));
                 pst.setString(7, (mandate.getDateEditor().getUiComponent()));
                pst.setString(8, thesoldqty.getText());
                pst.setString(9,desc.getText());

                int rs= pst.executeUpdate();
            if(rs>0)
            {
                JOptionPane.showMessageDialog(null,"data inserted");
                productname.setText("");
                saleprice.setText("");
                purchaseprice.setText("");
                  thesoldqty.setText("");
                  String sql2="Select * from stockable";
            Statement st1=conn.createStatement();
            ResultSet rs2=st1.executeQuery(sql2);

            while(rs2.next())
            {
                String id=String.valueOf(rs2.getInt("Productid"));
                String catid=String.valueOf(rs2.getInt("categoryid"));
                String usertypename=rs2.getString("ProductName");
                String qty=String.valueOf(rs2.getInt("Quantity"));
                String saleprice=String.valueOf(rs2.getInt("SaleUnitPrice"));
                String purprice=String.valueOf(rs2.getInt("CurrentPurchasePrice"));
                String expirydate=String.valueOf(rs2.getDate("ExpiryDate"));
                String manudate=String.valueOf(rs2.getDate("MfturDate"));
                String StockThesoldQty=String.valueOf(rs2.getInt("StockThesoldQty"));
                String description=rs2.getString("description");
                String tbdata[]={id,catid,usertypename,qty,saleprice,purprice,expirydate,manudate,StockThesoldQty,description};
                DefaultTableModel tblmodel=(DefaultTableModel)jTable2.getModel();
                tblmodel.addRow(tbdata);
            }

            }

        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, ex);
        }

现在,当我从表中提取行时,插入操作就完成了。

int i=jTable2.getSelectedRow();
        TableModel model=jTable2.getModel();
        n.setText(model.getValueAt(i,0).toString());
        n.setVisible(false);
        productname.setText(model.getValueAt(i, 2).toString());
       quantity.setText(model.getValueAt(i, 3).toString());
       saleprice.setText(model.getValueAt(i, 4).toString());
       purchaseprice.setText(model.getValueAt(i, 5).toString());
       thesoldqty.setText(model.getValueAt(i, 8).toString());
      desc.setText(model.getValueAt(i, 9).toString());
      String name=model.getValueAt(i,1).toString();
      cb1.setSelectedItem(name);
         try {
                 java.util.Date date= new SimpleDateFormat("yyyy-MM-dd").parse((String)model.getValueAt(i,6));
                  java.util.Date date2= new SimpleDateFormat("yyyy-MM-dd").parse((String)model.getValueAt(i,7));
             expirydate.setDate(date);
             mandate.setDate(date2);
         } catch (ParseException ex) {
             Logger.getLogger(product.class.getName()).log(Level.SEVERE, null, ex);
         }

现在当我更新表单的日期时

try{

             Class.forName("com.mysql.jdbc.Driver");
            Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/retailer","root","");

             String query = "UPDATE stockable SET ProductName='"+productname.getText()+"',Quantity='"+quantity.getText()+"',SaleUnitPrice='"+saleprice.getText()+"',CurrentPurchasePrice='"+purchaseprice.getText()+"',ExpiryDate='"+expirydate.getDateEditor().getUiComponent()+"',MfturDate='"+mandate.getDateEditor().getUiComponent()+"',StockThesoldQty='"+thesoldqty.getText()+"',Description='"+desc.getText()+"' WHERE Productid ='"+n.getText()+"'";
                    Statement ps = conn.createStatement();
                   ps.execute(query);

                    JOptionPane.showMessageDialog(null,"Data Update");

        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null,ex);
        }

它显示错误,数据截断不正确的日期格式错误显示:“数据截断不正确的日期值“星期三9月08日00:00:00 PKT 2021列”零售商"“库存”“到期日期”在第1行”

cclgggtu

cclgggtu1#

getUiComponent不返回日期本身,而是返回包含日期的UI组件。
尝试替换

pst.setString(6, (expirydate.getDateEditor().getUiComponent()));
           pst.setString(7, (mandate.getDateEditor().getUiComponent()));

与:

pst.setString(6, (((JTextField)expiryDate.getDateEditor().getUiComponent()).getText()));
           pst.setString(7, (((JTextField)mandate.getDateEditor().getUiComponent()).getText()));

相关问题