Oracle - update record and return updated date in same query

前端 未结 2 753
一向
一向 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 decided to use JDBCTemplate most probably to simplify the code in comparison to plain JDBC.

    This particular problem IMHO makes the plain JDBC solution as proposed in other answer much simpler, so I'd definitively recommend to get the database connection from JDBCTemplate and make the insert in a JDBC way.

    The simplest solution using JDBCTemplate that comes to my mind is to wrap the insert in a PROCEDURE and return the timestamp as an OUT parameter.

    Simple example (Adjust the time logik as required)

    create procedure insert_with_return_time (p_str VARCHAR2, p_time OUT DATE) as
    BEGIN 
       insert into identity_pk(pad) values(p_str);
       p_time := sysdate;
    END;
    /
    

    The call is done using SimpleJdbcCall

    SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName("insert_with_return_time");
    SqlParameterSource params = new MapSqlParameterSource().addValue("p_str", str);
    Map out = jdbcCall.execute(params);
    

    The Map contains the returned value e.g. [P_TIME:2019-10-19 11:58:10.0]

    But I can only repeat, in this particular use case is IMHO JDBC a rescue from JDBCTemplate;)

提交回复
热议问题