我做了一个产品形式,我必须在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列'零售商''可库存''过期日期'在第一行&;
getuiccomponent不返回日期本身,而是返回包含该日期的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()));