如何在jdbc中为PostgreSQL设置'5天'(日期时间间隔)?

4 java postgresql jdbc

考虑以下示例

select * from foo where time +'10 day' >current_timestamp
Run Code Online (Sandbox Code Playgroud)

我想让这个查询参数化为Java,我不知道如何设置10 day?!(字符串不起作用)

Cra*_*ger 14

您可以传递一个String参数并将其强制转换,例如

select * from foo where (time + CAST(? AS interval)) > current_timestamp
Run Code Online (Sandbox Code Playgroud)

或者传递一个int参数乘以一个固定的间隔,如果你总是处理不太复杂的间隔天,那就更好了.例如

select * from foo where (time + ? * INTERVAL '1' DAY) > current_timestamp
Run Code Online (Sandbox Code Playgroud)

setInt参数.


Gre*_*reg 4

select * from foo where time +'10 day'::interval >current_timestamp;
Run Code Online (Sandbox Code Playgroud)