gpt4 book ai didi

java - 将参数传递给 java 准备语句

转载 作者:行者123 更新时间:2023-12-01 10:19:18 25 4
gpt4 key购买 nike

我实际上正在尝试执行 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.我们该如何解决这个问题?

最佳答案

问号必须替换标记;不在字符串文字中。

试试这个:

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");

关于java - 将参数传递给 java 准备语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com