【问题标题】:getting result for a query in pgAdmin console but not through my java code在 pgAdmin 控制台中获取查询结果,但不是通过我的 java 代码
【发布时间】:2021-12-04 13:39:02
【问题描述】:

我正在查询:

SELECT * 
FROM test_test 
where user_id_one = 123456789 
  and user_id_two = 123456788 
  and action_taken = 'CHECK' 
  and games_type = 'LUDDO' 
  and modified_on >= '2021-10-07 05:53:48'::timestamp 
  and modified_on < '2021-10-07 05:53:49'::timestamp

我在 pgAdmin 控制台中得到了结果,但是当我使用 Java 代码尝试相同的事情时,它不起作用:

ps = conn.prepareStatement("SELECT * FROM test_test where user_id_one = ? and user_id_two = ? and action_taken = ? and games_type = ? and modified_on >= ?::timestamp and modified_on < ?::timestamp");
ps.setInt(1,testTest.getUserIdOne());
ps.setInt(2,testTest.getUserIdTwo());
ps.setString(3,testTest.getActionTaken());
ps.setString(4,testTest.getGamesType());
ps.setString(5,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(collusionDashboard.getModifiedOn().toDate()));
ps.setString(6,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(collusionDashboard.getModifiedOn().plusSeconds(1).toDate()));
resultSet = ps.executeQuery();
log.info("{}",ps);
if (resultSet.next()) == getting false here {
           
        }

当我记录数据时,我得到了与上面提到的相同的查询。

【问题讨论】:

  • 您不应将时间戳值作为字符串传递。使用setObject() 传递LocalDateTime 的实例

标签: java postgresql jdbc


【解决方案1】:

您正在尝试记录 PrepareStatement 对象,请尝试记录 ResultSet 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 2018-07-29
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多