Java PostgreSQL 中的查询错误没有返回任何结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21276059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No results returned by the Query error in PostgreSQL
提问by Ragesh Kr
I am trying to insert a data into a table. After executing the query i am getting an exception stating
我正在尝试将数据插入表中。执行查询后,我收到一个异常说明
org.postgresql.util.PSQLException: No results were returned by the query.
org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:284)
The data is getting inserted successfully, but i have no idea why i am getting this exception ??
数据已成功插入,但我不知道为什么会出现此异常??
采纳答案by Paul Draper
Use
用
executeUpdate
instead of
代替
executeQuery
if no data will be returned (i.e. a non-SELECT
operation).
如果没有数据将返回(即非SELECT
操作)。
回答by Popeye
If you want last generated id, you can use this code after using executeUpdate() method
如果你想要最后生成的 id,你可以在使用 executeUpdate() 方法后使用此代码
int update = statement.executeUpdate()
ResultSet rs = statement.getGeneratedKeys();
if (rs != null && rs.next()) {
key = rs.getLong(1);
}
回答by Dharmender Rawat
Please use @Modifyingannotion over the @Queryannotion.
请在@Query注释上使用@Modifying注释。
@Modifying @Query(value = "UPDATE Users set coins_balance = coins_balance + :coinsToAddOrRemove where user_id = :user_id", nativeQuery = true) int updateCoinsBalance(@Param("user_id") Long userId, @Param("coinsToAddOrRemove") Integer coinsToAddOrRemove);
The same is true for any DML query (i.e. DELETE, UPDATE or INSERT)
任何 DML 查询(即 DELETE、UPDATE 或 INSERT)也是如此
回答by Shahid Hussain Abbasi
Using @Modifying and @Transaction fixed me
使用 @Modifying 和 @Transaction 修复了我