keeping the history of table in java

2019-01-18 17:12发布

I need the sample program in Java for keeping the history of table if user inserted, updated and deleted on that table. Can anybody help in this?

Thanks in advance.

11条回答
神经病院院长
2楼-- · 2019-01-18 17:50

Well, we normally have our own history tables which (mostly) look like the original table. Since most of our tables already have the creation date, modification date and the respective users, all we need to do is copy the dataset from the live table to the history table with a creation date of now().

We're using Hibernate so this could be done in an interceptor, but there may be other options as well, e.g. some database trigger executing a script, etc.

查看更多
聊天终结者
3楼-- · 2019-01-18 17:51

I think you can use the redo log of your underlying database to keep track of the operation performed. Is there any particular reason to go for the program?

查看更多
何必那么认真
4楼-- · 2019-01-18 17:53

I think this can be achieved by creating a trigger in the sql-server. you can create the TRIGGER as follows:

Syntax:

CREATE TRIGGER trigger_name {BEFORE | AFTER } {INSERT | UPDATE | DELETE } ON table_name FOR EACH ROW triggered_statement

you'll have to create 2 triggers one for before the operation is performed and another after the operation is performed. otherwise it can be achieved through code also but it would be a bit tedious for the code to handle in case of batch processes.

查看更多
Ridiculous、
5楼-- · 2019-01-18 17:57

You should try using triggers. You can have a separate table (exact replica of your table of which you need to maintain history) .

This table will then be updated by trigger after every insert/update/delete on your main table.

Then you can write your java code to get these changes from the second history table.

查看更多
We Are One
6楼-- · 2019-01-18 17:57

Triggers are not suggestable, when I stored my audit data in file else I didn't use the database...my suggestion is create table "AUDIT" and write java code with help of servlets and store the data in file or DB or another DB also ...

查看更多
叼着烟拽天下
7楼-- · 2019-01-18 18:02

Like @Ashish mentioned, triggers can be used to insert into a seperate table - this is commonly referred as Audit-Trail table or audit log table.

Below are columns generally defined in such audit trail table : 'Action' (insert,update,delete) , tablename (table into which it was inserted/deleted/updated), key (primary key of that table on need basis) , timestamp (the time at which this action was done)

It is better to audit-log after the entire transaction is through. If not, in case of exception being passed back to code-side, seperate call to update audit tables will be needed. Hope this helps.

查看更多
登录 后发表回答