简体   繁体   中英

How to compare the current date with the previous date from database in JSP or in Java?

I have a table with a regid (primary key) and a date column with their values as 12 and 2020-09-23 respectively. If i want to retrieve the date in JSP i can simply do it as ${date} . But after updating this particular regid the date changes. So how will I compare the previous date with the current updated date.

For your use case you will have to maintain one more column for previous date value which will be updated when you save the new date.

Or if you just want to show you temporarily for that request then you can use one Transient variable for setting the previous date value and show that field in JSP.

Trigger

A more powerful database will offer the feature of triggers , code that executes on the database server every time a row is being inserted, updated, or deleted.

That trigger code you write will be able to see both the old and new values of the row being modified. That trigger code can calculate the delta, and write that data to another column or to a row in separate history-keeping table. This kind of history-tracking is a common use of triggers.

Your JSP would need to query that other column, or other table, to get the original value or delta (whichever or both that you decide to store).

Before and after vars

Alternatively, if your JSP is making the change to the database, then obviously it can keep the original value and the new value as objects in memory.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM