简体   繁体   中英

How to use oracle SYSTIMESTAMP in prepared statement query?

I need to run a insert query where one of the column has to store timestamp. This timestamp should be systemdate generated by oracle database while inserting. I am running a prepared Statement similar to this:

insert into table_name (column1 , timestamp) values(?,SYSTIMESTAMP);

But this is throwing exception in java while executing.

I have tried with this, it was working:

insert into table_name (column1 , timestamp) 
values(?,TO_TIMESTAMP(?, 'DD-MON-RR HH.MI.SSXFF AM'));

Here I am passing timestamp generated by java, which is not my requirement. I have to store timestamp generated by DB.

SQL> 
create table t (col1 number, col2 timestamp default systimestamp);

Table T created.


SQL> 
insert into t (col1) values (0);

1 row inserted.

SQL> 
insert into t (col1) values (1);

1 row inserted.

SQL> 
insert into t (col1, col2) values (2, systimestamp + 3);

1 row inserted.


SQL> 
select * from t;

      COL1 COL2                
---------- --------------------
         0 16-APR-2020 07.07.15
         1 16-APR-2020 07.07.15
         2 19-APR-2020 07.07.15

If you create the table column with default value, when inserting a row, oracle will insert the default value when a value is not provided.

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