How to do the operation w_balance = w_balance + amount
in the query String? One way is to perform the operation outside the Query String. But is there a way to do that directly? Here, amount
is the new amount I would want to add or subtract from the w_balance
.
My database is postgresql.
String SQL = "update walletapp.users_acc set w_balance = w_balance + amount where kyc_id = :sender_id";
I used - String SQL = "update walletapp.users_acc set w_balance = w_balance + :amount where kyc_id = :sender_id";
which solved my problem.
Here is a simple example of an assignation of a value an the usage of it :
int i = 0;
System.out.println(i = 5);
String s = "The Value is " + (i = 4);
System.out.println(s);
System.out.println(i);
This will output
5
The Value is 4
4
So you see that you can set the variable an recover it at the same time, this is just some operator priority
如@a_horse_with_no_name所示, set w_balance = w_balance + :amount
对我set w_balance = w_balance + :amount
。
Assing it to a variable or do it directly. A good way to do it is using String.format:
String SQL = String.format("update walletapp.users_acc set w_balance = %d where kyc_id = :sender_id", w_balance + amount);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.