anuj
anuj

Reputation: 221

how to get database current time using jooq API

How to get the current database time using the jooq API. Is the below the right approach?

Record result = DSL.using(configuration).fetchOne("Select CURRENT_TIMESTAMP() as NOW");
Timestamp now = result.get("NOW", Timestamp.class);

Upvotes: 1

Views: 2117

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220762

Using plain SQL is always an option, but why not use what's available in jOOQ already, e.g. DSL.currentTimestamp()

Timestamp now = using(configuration)
               .select(currentTimestamp())
               .fetchOne(0, Timestamp.class);

Or even simpler:

Timestamp now = using(configuration).fetchValue(select(currentTimestamp());

As always, these jOOQ queries assume you're using the following static import:

import static org.jooq.impl.DSL.*;

Upvotes: 4

Related Questions