What I have is below table.
+----+--------+--------+--------+--------+
| id | val1 | val2 | val3 | val4 |
+====+========+========+========+========+
| 1 | data1 | data2 | data3 | data4 |
+----+--------+--------+--------+--------+
Now I fire a query with below details, say
UPDATE tableName
SET val1='newVal1', val2='data2', val3='data3', val4='data4'
WHERE id=1
Note: This is just same query. Actually I am implementing edit from four text field. So what I would be having using JSF and Java bean would be as below.
PreparedStatement psmt = conn.prepareStatement("
UPDATE tableName
SET val1=?, val2=?, val3=?, val4=?
WHERE id=1
");
psmt.setString(1, myValue1);
psmt.setString(2, myValue2);
psmt.setString(3, myValue3);
psmt.setString(4, myValue4);
psmt.execute();
What I want is using above query, which columns are updated. In above case, val1
column is updated.
Basically I want this for log purpose where I would come to know who did the edit and what edit is done. Any idea/ suggestion how to get this done?
Note: I don't want to create any other table as stated in answer by meewoK. Just using query, I would like to know the column names which are updated.
I will explain what I want with example.
Let's say userA gets logged in into the system. Now he has access of user lists. Now let's say he did edit for User B. What he changed is User B, email id and full name. Now in log what I want is two entries.
=================================================================
whoDid whatDid forWhom whichField whichSection whatTime
=================================================================
userA edit userB emailId User time_whenDid
userA edit userB fullName User time_whenDid
=================================================================
How about running something like the following query to create the log entry just before the update:
PreparedStatement psmt = conn.prepareStatement("
SELECT CASE WHEN val1 = ?
THEN ''
ELSE 'val1 changed from ' + val1 + ' to ' + ? + '\n'
END CASE +
CASE WHEN val2 = ?
THEN ''
ELSE 'val2 changed from ' + val2 + ' to ' + ? + '\n'
END CASE +
CASE WHEN val3 = ?
THEN ''
ELSE 'val3 changed from ' + val3 + ' to ' + ? + '\n'
END CASE +
CASE WHEN val4 = ?
THEN ''
ELSE 'val4 changed from ' + val4 + ' to ' + ? + '\n'
END CASE AS logentry
FROM tableName
WHERE id=1
");
psmt.setString(1, myValue1);
psmt.setString(2, myValue1);
psmt.setString(3, myValue2);
psmt.setString(4, myValue2);
psmt.setString(5, myValue3);
psmt.setString(6, myValue3);
psmt.setString(7, myValue4);
psmt.setString(8, myValue4);
ResultSet rs = psmt.executeQuery();
String logentry = rs.getString("logentry");
Disclaimer: This is off the top of my head and completely untested so may need a bit of tweaking - should be enough to get you going though.
Possible issue: Even if you execute the query just before the update there's a chance data may have changed between the two. Depending on various factors this may or may not be an issue in practice but it's worth mentioning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With