(更新)JAVA:在表列中添加数据量

(更新)JAVA:在表列中添加数据量,java,Java,我试图更新我的数量列,但它给了我错误 这是我的密码: private void BtnPurchsPurchaseRunningOutActionPerformed(java.awt.event.ActionEvent evt) { try{ String upd = "update for_purchaseitems set Quant

我试图更新我的数量列,但它给了我错误

这是我的密码:

private void BtnPurchsPurchaseRunningOutActionPerformed(java.awt.event.ActionEvent evt) {                                                            
        try{
            String upd = "update for_purchaseitems set Quantity=Quantity+? where Description=?;";
            pst=conn.prepareStatement(upd);
            pst.executeUpdate(upd);
            if(rs.next()){
            pst.setString(1, txt_PurchsQty.getText());
            pst.setString(2, txt_PurchsDesc.getText());
            JOptionPane.showMessageDialog(null, "success!");
        }
        }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
        }
        Updatefor_PurchaseItem();
        BtnPurchsPurchaseRunningOut.setVisible(false);
        BtnPurchsPurchase.setVisible(true);
    }
错误消息显示:


您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解使用接近“”的正确语法?其中Description=?'在第1行

设置参数之前,您有您的
执行更新
;因此,该语句最终使用的是文本
,而不是您的参数。您想要的顺序是:

  • 创建语句
  • 设置其参数
  • 执行它
  • 阅读结果
因此:

还要注意
pst.setString(2,txt_PurchsDesc.getText())
之后的code>


如果
quantity
是一个数字列,如
+
和名称所示,也不要使用
setString
;使用
setInt
setLong

pst=conn.prepareStatement(upd);
pst.setInt(1, Integer.parseInt(txt_PurchsQty.getText())); // <===
pst.setString(2, txt_PurchsDesc.getText());
pst.executeUpdate(upd);
if(rs.next()){

它说:您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解使用接近“”的正确语法?其中第1行的Description=?'要改进您的问题,请使用问题下的“编辑”链接,而不是评论。谢谢!我已经这样做了,但仍然有相同的错误。。。TT@DjayPediA当前位置这肯定是问题之一。我刚刚注意到您正在将
setString
与可能是数字列的内容一起使用。请看更新。嗯。。。我应该换吗?我已经将数量的数据类型分配到nvarchar。。。我想如果前端将是限制这种数据类型的一个,这将是很容易的。。。啊!我命中注定……在那里!!我得到了它!!!非常感谢你!!!哇!!!!谢谢Crowder先生@T.J.Crowder
pst=conn.prepareStatement(upd);
pst.setInt(1, Integer.parseInt(txt_PurchsQty.getText())); // <===
pst.setString(2, txt_PurchsDesc.getText());
pst.executeUpdate(upd);
if(rs.next()){
String upd = "update for_purchaseitems set Quantity=Quantity+? where Description=?;";
// Here --------------------------------------------------------------------------^