我实际上是在尝试执行delete from mytable where CreationDate < now() - interval '5 month'
我想将参数5
传递给?
public static final StringBuilder SQL_MY_Query= new StringBuilder(
"delete from mytable where CreationDate < now() - interval '? month'");
ps = con.prepareStatement(SQL_MY_Query.toString());
ps.setInt(1, 5);
我收到错误Sql Exception org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
我们如何解决此问题?
答案 0 :(得分:1)
问号必须替换令牌;不是字符串文字。
试试这个:
public static final StringBuilder SQL_MY_Query= new StringBuilder(
"delete from mytable where CreationDate < now() - interval ? || ' month'");
或(基于:Using a variable period in an interval in Postgres):
public static final StringBuilder SQL_MY_Query= new StringBuilder(
"delete from mytable where CreationDate < now() - (? || ' month')::interval");
答案 1 :(得分:0)
以下其中一项可能有所帮助。看看。
选项1: 从mytable中删除CreationDate&lt; now() - 间隔? “
ps.setString(1,numMonth +“month”);
选项2: 从mytable中删除CreationDate&lt; now() - interval(?||'month')“
ps.setString(1,numMonth);