检查是否从Java JDBC更新了postgres数据库中的记录 [英] Check whether a record is updated in postgres database from java JDBC

查看:74
本文介绍了检查是否从Java JDBC更新了postgres数据库中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当调用更新查询时,如何从java知道记录是否已更新. 我试过了来自executeUpdate()的返回类型int,但是无论何时执行查询,它都将显示1.也就是说,如果记录被更改,则它将返回值> 1. 如果有机会在Postgresql中触发以调用java方法() 这将对此有所帮助. 请帮忙.

How can I know from java whether a record is updated when update query is called. I had tried the return type int from executeUpdate() but it will show 1 whenever the query is executed. That is if the record is changed or not it will return value > 1. If there is an opportunity for trigger in postgresql for calling a java method() would be helpful for this. Please help.

推荐答案

如果您需要知道更新后行的数据是否被更改,则需要将参数传递给WHERE子句,例如UPDATE foo SET bar = $1, baz = $2 WHERE bar <> $1 AND baz <> $2 AND id = $3这样的语句.示例假设您希望基于id更新一行的两列.

If you need to know whether a row's data was changed as a result of the update, you would need to pass the parameters to the WHERE clause, making a statement such as UPDATE foo SET bar = $1, baz = $2 WHERE bar <> $1 AND baz <> $2 AND id = $3. Example is assuming you wish to update two columns of a single row based on id.

此后,WHERE子句匹配的唯一方法是是否有实际的更新要执行,您可以使用返回值检查是否发生了更新.

After that the only way the WHERE clause will match is if there is an actual update to do, and you can use the return value to check if an update occurred or not.

另一种可能性是重构代码,这样您就不必费心了,但是您需要为此进一步解释用例.

Another possibility is to refactor your code so that you don't need to bother about it, but you will need to explain your use case further for that.

这篇关于检查是否从Java JDBC更新了postgres数据库中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆