java.sql.SQLException:在 index::4 处缺少 IN 或 OUT 参数,preparestatement 发生错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3674008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.sql.SQLException: Missing IN or OUT parameter at index::4 error occurs with preparestatement
提问by vanita vaghasiya
strInsertQuery="INSERT INTO SYNPACKAGEFREEUSAGEMAPPING (PACKAGEID,SERVICEID,PRIORITY,MAPPINGID,ATTRIBUTEVALUE,FREEUSAGE,UOM,PARAMSTR1,UNITCREDITPOLICYDETAILID) VALUES (?,?,?,?,?,?,?,?,?)";
newPstmt=newCon.prepareStatement(strInsertQuery);
newPstmt.setString(1,strProductId);
newPstmt.setString(2,strUPGServiceId);
newPstmt.setInt(3,0);
if(FieldMappingrs.next())
{
newPstmt.setLong(4,FieldMappingrs.getLong(1));
}
newPstmt.setString(5,ucpDetailData.getCreditattributevalue());
for(Iterator itrColUnitCreditDetail=ucpData.iterator();itrColUnitCreditDetail.hasNext();)
{
unitCreditData = (UnitCreditDetailData)itrColUnitCreditDetail.next();
if(ucpDetailData.getUnitcreditpolicydetailid().equals(unitCreditData.getUnitcreditpolicydetailid()))
{
debugLog(PackageConstant.PACKAGE_MODULE_NAME,"ServicePackageSessionBean:createFreeUsageServiceDetails() unitCreditData "+ unitCreditData);
newPstmt.setDouble(6,unitCreditData.getEndvalue());
}
debugLog(PackageConstant.PACKAGE_MODULE_NAME,"Insert record successfull"+ ucpDetailData);
}
newPstmt.setString(7,ucpDetailData.getUom());
newPstmt.setString(8,ucpDetailData.getCreditattribute());
newPstmt.setString(9,ucpDetailData.getUnitcreditpolicydetailid());
newPstmt.executeUpdate();
回答by Jon Skeet
Well, look at this:
嗯,看看这个:
if(FieldMappingrs.next())
{
newPstmt.setLong(4,FieldMappingrs.getLong(1));
}
So you're conditionallysetting parameter 4.
所以你有条件地设置参数 4。
What do you want to insert if FieldMappingsrs.next()
returns false? (I suspect this is what is happening.) Try setting it to an appropriate value in an else
block.
如果FieldMappingsrs.next()
返回假,你想插入什么?(我怀疑这就是正在发生的事情。)尝试在else
块中将其设置为适当的值。
回答by Garis M Suero
Your problem is just at this block
你的问题就在这个街区
if(FieldMappingrs.next()) {
newPstmt.setLong(4,FieldMappingrs.getLong(1));
}
As you see if .next()
returns false
you won't be passing the parameter at index
4.
如您所见,如果.next()
返回,false
您将不会在index
4处传递参数。
I suggest to, if you can pass to that table 0 instead of null use this
我建议,如果你可以传递到那个表 0 而不是 null 使用这个
if(FieldMappingrs.next()) {
newPstmt.setLong(4,FieldMappingrs.getLong(1));
} else {
newPstmt.setLong(4,0);
}
Or if it is required value, just throw an exception
.
或者如果它是必需的值,只需抛出一个exception
.