How to insert Current time in TIMESTAMP column(Oracle) from java class using jdbc - oracle

i have tryed many things but not able to insert data in my timestamp column.
from toad its possible using this
UPDATE SUPPORTSTAFF
SET SUPPSTAFFJOINDATE=to_timestamp('27/02/2002 15:51.12.539880', 'dd/mm/yyyy hh24:mi.ss.ff')
where JDBCUSERID='5700';
its working
but how can i insert data from java class using create statment and execute query its giving me invalid month error

Use a PreparedStatement with a parameter for the timestamp, e.g.
UPDATE SUPPORTSTAFF SET SUPPSTAFFJOINDATE = ? where JDBCUSERID = ?
and then set the parameters:
statement.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
statement.setString(2, "your ID");
(Then execute the statement, obviously.)

Related

How does one insert timestampz's into postgres using spring jdbc

I have a table in postgres called retailPrices that has a column of type timestampz. I can insert a timestamp into it using the shell and programmatically using jdbc by concatenating it into a query and passing it into jdbc.update(). However I've been enlightened to the better way of running statements through jdbc which is through the use of the varargs overload as shown below.
ZonedDateTime ts = ZonedDateTime.now();
String tsFormatted = ts.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
String insertRetailPrice = "insert into retailprices values (?, ?, TIMESTAMP WITH TIME ZONE ?);";
int retailPriceAffectedRows = jdbcTemplate.update(insertRetailPrice, SKU, price, tsFormatted);
However I keep getting runtime exceptions when specifically trying to insert timestampz's. I've tried it with and without the prefix "TIMESTAMP WITH TIME ZONE" with both the ZonedDateTime, ts, and the string, tsFormatted, with no avail.

Datastage issue in loading zero

Using Datastage 11.5.0.2, Jobs failed when it try to load the data as "0" into DATE FIELD (DB2)..
In source DB, the column is VARCHAR whereas in target it is DATE field.. the only value in source which failed to load is 0. how to resolve.. any idea pl
Create one stage variable: if (input column value ) = 0 then use StringToDate("0001-01-01", "%dd:%mm:%yyyy")
Else StringToDate(inputcolumn, "%dd:%mm:%yyyy")
You can use the in the transformer stage,
StringToTimestamp(Column,"%yyyy-%mm-%dd")
also you can convert data in Source qualifier by using below in select query
to_date(column,"%yyyy-%mm-%dd")
Can use the to_date function
to_date(column,'YYYY-MM-DD')
TO_DATE(column, 'YYYY-MM-DD HH24:MI:SS')

ExecuteSQL doesn't select table if it having dateTime Offset value?

I have created table with single column having data type -dateTimeOffset value and inserted some values.
create table dto (dto datetimeoffset(7))
insert into dto values (GETDATE()) -- inserts date and time with 0 offset
insert into dto values (SYSDATETIMEOFFSET()) -- current date time and offset
insert into dto values ('20131114 08:54:00 +10:00') -- manual way
In Nifi,i have specified "Select * from dto" query in Execute SQL .
It shows below error..,
java.lang.IllegalArgumentException: createSchema: Unknown SQL type -155 cannot be converted to Avro type
If i change that column into dateTime then ExecuteSQL runs correctly but it doesn't worked in DateTimeOffset column.
Any help appreciated.
Many thanks
datetimeoffset is a MSSQL-specific JDBC type and is not supported by ExecuteSQL (which supports the standard JDBC types). You could try to cast the datetimeoffset field into some other standard type such as datetime, as described here.
I've created a Custom Processor and adapted the JdbcCommon.java class to include SQL Server's DATETIMEOFFSET. It's just one line of code. I'll try to see if I can ask them to merge this on the official repo.
This is a piece of my JdbcCommon.java:
case TIMESTAMP:
case TIMESTAMP_WITH_TIMEZONE:
case -101: // Oracle's TIMESTAMP WITH TIME ZONE
case -102: // Oracle's TIMESTAMP WITH LOCAL TIME ZONE
case -155: // SQL Server's DATETIMEOFFSET <---- added this line
addNullableField(builder, columnName,
u -> options.useLogicalTypes
? u.type(LogicalTypes.timestampMillis().addToSchema(SchemaBuilder.builder().longType()))
: u.stringType());
break;

How to use CURRENT_TIMESTAMP in JDBC (Oracle DB)

In MY_TABLE, when ORD_AMT field is updated then LST_UPDT_TS is updated with current_timestamp too. Presented by following query:
UPDATE MY_TABLE
SET ORD_AMT = 50, LST_UPDT_TS = CURRENT_TIMESTAMP
WHERE ORD_ID = 'ORD_123'
I need implement this business by JDBC code, and my Web server and DB server are difference timezone. If I pass Date().getTime() to JDBC, it's the current of Web server but I need the current time of DB server.
Is there a way for passing CURRENT_TIMESTAMP like this:
preparedStatement = dbConnection.prepareStatement(updateOrderSql);
preparedStatement.setFloat(1, 50);
preparedStatement.setTimestamp(2, Timestamp.CURRENT_TIMESTAMP);
preparedStatement.setString(3, 'ORD_123');
preparedStatement.executeUpdate();
Thanks for your help.
Instead of passing the current timestamp parameter to oracle, you can make use of SYSTIMESTAMP like below
UPDATE MY_TABLE
SET ORD_AMT = 50, LST_UPDT_TS = SYSTIMESTAMP
WHERE ORD_ID = 'ORD_123'
ultimately both might be same if FRONT END server and DB server are in same machine, i mean single tier architecture. If not you can type cast it to string and store the same in table with varchar2 datatype.
To update LST_UPDT_TS field with JDBC use the following code:
preparedStatement.setTimestamp(2, new Timestamp(System.currentTimeMillis()));
And you'll see in DB updated LST_UPDT_TS field.

Sybase JDBC get generated keys

In Postgres, I can write
INSERT .. RETURNING *
To retrieve all values that had been generated during the insert. In Oracle, HSQLDB, I can use
String[] columnNames = ...
PreparedStatement stmt = connection.prepareStatement(sql, columnNames);
// ...
stmt.execute();
stmt.getGeneratedKeys();
To retrieve all values that had been generated. MySQL is a bit limited and only returns columns that are set to AUTO_INCREMENT. But how can this be done with Sybase SQL Anywhere? The JDBC driver does not implement these methods, and there is no INSERT .. RETURNING clause, as in Postgres. Is there way to do it, other than maybe running
SELECT ##identity
immediately after the insert?
My current implementation executes three consecutive SQL statements:
-- insert the data first
INSERT INTO .. VALUES (..)
-- get the generated identity value immediately afterwards
SELECT ##identity
-- get the remaining values from the record (possibly generated by a trigger)
SELECT * FROM .. WHERE ID = :previous_identity
The third statement can be omitted, if only the ID column is requested

Resources