如何修复此错误:参数索引超出范围(1>参数数,即0)


int Prid,newQty;
public void Update()
{
String path="jdbc:mysql://localhost:3307/supermarket";
String user="root";
String pass="123456";
try {
con=DriverManager.getConnection(path, user, pass);
ps = con.prepareStatement("update product set ProdQty="+newQty+""+"where ProdID="+Prid);           
ps.setInt(1, Prid);               
ps.executeUpdate();                    
SelectSeller();
} catch (SQLException e) {
e.printStackTrace();
}
}

我创建int以在计算时更新数据库表,但错误

该代码用于计算

private void ProdTBMouseClicked(java.awt.event.MouseEvent evt) {                                    
DefaultTableModel model = (DefaultTableModel)ProdTB.getModel();
int Myindex = ProdTB.getSelectedRow();
Prid = Integer.valueOf(model.getValueAt(Myindex, 0).toString());
AvailQty = Integer.valueOf(model.getValueAt(Myindex, 2).toString());        
try {
newQty = AvailQty - Integer.valueOf(ProdQty.getText());   
} catch (Exception e) {

}        
ProdName.setText(model.getValueAt(Myindex, 1).toString());
Uprice = Double.valueOf(model.getValueAt(Myindex, 3).toString());      
}                                   

当newQty=AvailQty-Integer.valueOf(ProdQty.getText(((时,我需要更改产品表中的变量ProdQty;

private void AddtoBillActionPerformed(java.awt.event.ActionEvent evt) {                                          
if(ProdQty.getText().isEmpty()|| ProdName.getText().isEmpty())
{
JOptionPane.showMessageDialog(this, "Missing Information..");
}
else if(AvailQty <= Integer.valueOf(ProdQty.getText()))
{
JOptionPane.showMessageDialog(this, "Not Enough In Stock..");
}
else{
i++;
ProdTot = Uprice * Double.valueOf(ProdQty.getText());
GrdTotal = GrdTotal+ProdTot;
if(i==1)
{
Bill.setText(Bill.getText()+"=============== SHOPPING MORE ==================n"+" NUM     PRODUCT     PRICE     QAUNTITY     SUBTOTALn"+"    "+i+"         "+ProdName.getText()+"      "+Uprice+"              "+ProdQty.getText()+"               "+ProdTot+"n");
}
else
{
Bill.setText(Bill.getText()+"    "+i+"         "+ProdName.getText()+"      "+Uprice+"              "+ProdQty.getText()+"               "+ProdTot+"n");
}
Grdtotal.setText("Total "+GrdTotal);
Update();
}

}                    

我是java新手,请帮帮我,谢谢。

您指定Prid是一个绑定变量,但也将该值嵌入到sql语句文本中
您需要为绑定变量指定占位符,而不是在语句中嵌入值:
"update product set ProdQty=" + newQty + " where ProdID = ?"

对于CCD_ 2也建议这样做。它提高了类似语句的执行性能,并降低了SQL注入安全漏洞的风险

最新更新