Oracle - update record and return updated date in same query

前端 未结 2 786
一向
一向 2021-01-27 04:49

I\'m using Java 8 with Spring\'s JdbcTemplate and Oracle 12.1,

I want to update record and get the exact time record was updated

jdbcTempla         


        
2条回答
  •  难免孤独
    2021-01-27 05:28

    You're right that passing new Date() would store the server time rather than the DB time.

    To store the DB time you can set your timestamp to the DB system timestamp systimestamp then you could run a query to retrieve that row and its updated timestamp.

    If you want to update the row and get the updated timestamp in a single execution then you could do the following using RETURNING INTO where TimestampUpdated is your column name:

    Connection con = ...;
    String sql = "UPDATE TableName SET  , TimestampUpdated = systimestamp RETURNING TimestampUpdated INTO ?";
    CallableStatement statement = con.prepareCall(sql);
    statement.registerOutParameter(1, Types.TIMESTAMP);
    
    int updateCount = statement.executeUpdate();
    Timestamp timestampUpdated  = statement.getInt(1);
    System.out.println("Timestamp Updated = " + timestampUpdated);
    

    Here is a related link doing this with JdbcTemplate

提交回复
热议问题