例外"参数索引超出范围(1>参数个数,为0)。"

时间:2016-05-28 17:01:26

标签: mysql netbeans

query="INSERT INTO paint_inventory(ID,Name,type,gallons,quarters) VALUES(?,?,?,?,?)";
con.prepareStatement(query);
pstm.setInt(1,Integer.parseInt(itemID_newitem_field.getText()));
pstm.setString(2,name_newitem_field.getText());
pstm.setString(3,type_newitem_field.getText());
pstm.setInt(4,Integer.parseInt(gallons_quantity_newitem_field.getText()));
pstm.setInt(5,Integer.parseInt(quarters_quantity_newitem_field.getText()));
pstm.execute();
JOptionPane.showMessageDialog(rootPane,"item has been registered");

1 个答案:

答案 0 :(得分:0)

你的代码中定义了

pstm吗?希望您需要将prepareStatement分配给pstm,然后您可以设置参数值。

同时确保pstm不会在同一方法或其他方法中用于其他prepareStatement。每个prepareStatement都应该有单独的变量名来设置参数值。

query="INSERT INTO paint_inventory(ID,Name,type,gallons,quarters) VALUES(?,?,?,?,?)";
pstm = con.prepareStatement(query); -- assign to pstm then set values
...
...