开发者

how to create a general java program to update only the fields that is changed in mysql

i have a table that contains address of users AddressTable(country,state,city,pincode,开发者_StackOverflowphonenumber) which contains value (USA,NY,Bronx,512345,1123456)

When a user edits a new value for 1 field or all fields, how to create a java program which can update only the values that have been edited.

i dont want to read the previous values using select and reinsert them back to places where the new values have not been edited.

how to do this with a single update query like given below

void editAddress(String country,String state,String city,String pincode,String phonenumber)
{
 PreparedStatement ps = con.prepareStatement("UPDATE AddressTable set country=?,set state=?,set city=?,set pincode=? ,set phonenumber=? WHERE user=(?)") ;
 ps.setString(1, country);
 ps.setString(2, state);
 ps.setString(3, city);
 ps.setString(4, pincode);
 ps.setString(5, phonenumber);
 ps.setString(6, user);             
 ps.executeUpdate();
}


You could copy the values to a simple POJO class, and then when saving edited values compare them with original ones.

Or you could use and ORM (object relational mapper) like Hibernate which should do it automatically.


You could make a bean that hold the current and previous value for each field, and then make a cross cutting logic (like an AspectJ advice) that saves/updates a modified value only when it's different from the old one


Like matesuz said , just set the field as not null on the client side. And your good to go!


UPDATE AddressTable set country=IFNULL(?,country),state=IFNULL(?,state),city=IFNULL(?,city),pincode=IFNULL(?,pincode) ,phonenumber=IFNULL(?,phonenumber) WHERE user=(?)

The value of the field wont change if the value to be replaced is NULL with this query

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜