【问题标题】:No results returned by the Query error in PostgreSQLPostgreSQL 中的 Query 错误没有返回结果
【发布时间】:2014-02-12 02:31:49
【问题描述】:

我正在尝试将数据插入表中。执行查询后,我得到一个异常说明

org.postgresql.util.PSQLException: No results were returned by the query.
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:284)

数据已成功插入,但我不知道为什么会出现此异常??

【问题讨论】:

    标签: java database jsp postgresql sql-insert


    【解决方案1】:

    使用

    executeUpdate
    

    而不是

    executeQuery
    

    如果不返回任何数据(即非SELECT 操作)。

    【讨论】:

    • 美女!完美地工作..但是它像executeUpdate一样工作但executeQuery不起作用?在这两种情况下,数据都被正确插入。
    • @RageshKr,它被正确插入,但 Java 数据库连接器随后期待数据,但没有数据。所以错误发生在插入之后。
    【解决方案2】:

    请在 @Query 注释上使用 @Modifying 注释。

    @修改 @Query(value = "UPDATE 用户设置 coin_balance = coin_balance + :coinsToAddOrRemove where user_id = :user_id", nativeQuery = true) int updateCoinsBalance(@Param("user_id") Long userId, @Param("coinsToAddOrRemove") Integer coinToAddOrRemove);

    对于任何 DML 查询(即 DELETE、UPDATE 或 INSERT)也是如此

    【讨论】:

    • 谢谢,我想要一个 JPQL 查询的解决方案!
    【解决方案3】:

    使用@Modifying 和@Transaction 修复了我

    【讨论】:

    • 它的@Transactional
    • @Transactional @Modifying(clearAutomatically = true) @Query(value = Update/Delete/InstertQuery , nativeQuery = true)
    【解决方案4】:

    如果你想要最后生成的id,你可以在使用executeUpdate()方法后使用这段代码

     int update = statement.executeUpdate()
     ResultSet rs = statement.getGeneratedKeys();
     if (rs != null && rs.next()) {
      key = rs.getLong(1);
     }
    

    【讨论】:

      【解决方案5】:

      让我想到这个问题的问题有点不同——我在使用基于接口的 Spring JPA 存储库删除行时遇到了这个错误。原因是我的方法签名应该返回一些结果:

      @Modifying
      @Query(value = "DELETE FROM table t WHERE t.some_id IN (:someIds)", nativeQuery = true)
      List<Long> deleteBySomeIdIn(@Param("someIds") Collection<Long> someIds);
      

      将返回类型更改为void 解决了该问题:

      @Modifying
      @Query(value = "DELETE FROM table t WHERE t.some_id IN (:someIds)", nativeQuery = true)
      void deleteBySomeIdIn(@Param("someIds") Collection<Long> someIds);
      

      【讨论】:

        【解决方案6】:

        我已经使用addBatchexecuteBatch 解决了这个问题,如下所示:

        statement.addBatch("DELETE FROM public.session_event WHERE id = " + entry.getKey());
        statement.executeBatch();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-08-16
          • 1970-01-01
          • 2019-10-13
          • 2015-07-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多