xyz*_*xyz 1 java sql prepared-statement
我invalid column index准备接受准备好的陈述.
这是我的代码
// 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
}
}
Run Code Online (Sandbox Code Playgroud)
当我得到异常i变3,虽然查询包含3个占位符.
编辑(提示):我认为问题'在于造成破坏.我们怎么能逃脱呢?
我不知道这是否是问题的原因,但我不认为参数的工作方式与你认为引用时的方式完全相同.您仍在每个参数后面的代码中添加引号,并作为参数的开头.我怀疑你只想要:
rsTableNames.getString(1)+ " where lower(c_name) like ?";
Run Code Online (Sandbox Code Playgroud)
在每个地方,然后:
String param = query.toLowerCase() + "%";
Run Code Online (Sandbox Code Playgroud)
由于引用解析,这可能会解决问题 - 我认为你的中间参数被认为是一个大文字的一部分.