我收到以下准备好的语句的无效列索引
。
这是我的代码
// Excluding some unnecessary code
counter = 1;
if (rsTableNames.next())
{
// Creating Query for prepared statement
String getCode = "select * from ( select c_name from "
+ rsTableNames.getString(1)+ " where lower(c_name) like ?%'";
while (rsTableNames.next())
{
getCode += " union select c_name from " +
rsTableNames.getString(1)+ " where lower(c_name) like ?%'";
counter++;
}
getCode += " ) where rownum <= " + maxRecords;
// Now The getCode contains 3 place holders ie ?
pst = con.prepareStatement(getCode);
String param = "'" + query.toLowerCase();
for(int i=1;i<=counter;i++)
{
pst.setString(i,param); // when i=3 exception is thrown
}
}
虽然查询包含 3 个占位符,但当 i
变为 3
时,我得到了异常。
编辑(提示): 我认为问题出在造成破坏的'
上。我们如何才能摆脱它?
最佳答案
我不知道这是否是问题的原因,但我认为在引用时参数的工作方式并不像您认为的那样。您仍然在代码中添加引号在每个参数之后并作为参数的开头。我怀疑你只是想:
rsTableNames.getString(1)+ " where lower(c_name) like ?";
在每个地方,然后:
String param = query.toLowerCase() + "%";
由于引号解析,这可能会解决问题 - 我认为您的中间参数被认为是大文字的一部分。
关于java - Prepare 语句出现无效的列索引错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7021087/