Not many valid reasons, why would ever isRecordExists2 be better method. It is almost always good to extract reusable strings into constants and split code into smaller pieces. I definitely vote for isRecordExists. If you decide to stick with isRecordExists2, why even create sqlQuery variable? You might as well just pass the string itself into queryForObject.
Only possible downsides of using isRecordExists I can think of (but very small and insignificant imho):
- Static field is always there and taking some piece of memory. Local string would not unless while that method is being called.
- That string is not as close to method and therefore code might be harder for read (but we have IDEs to help with that). I would choose better name than
QUERYto help with that too.
If there are specific coding standards in Spring boot against it, please link them :-)