When we work with JPA and JPQL we can use some date/time expressions which make the query DMBS independent. For instance let's say if I want to set the ending time of a session in my database I could simply use CURRENT_TIMESTAMP
expression as follows:
String jpql = "UPDATE SessionJpa s SET s.end = CURRENT_TIMESTAMP WHERE s.id = :id";
entityManager.getTransaction().begin();
Query query = entityManager.createQuery(jpql);
query.setParameter("id", someIdValue);
query.executeUpdate();
entityManager.getTransaction().commit();
This way the same JPQL should work with Oracle, MySQL, PostreSQL, etc as DBMS.
Now my question: Is there a way to achieve the same when using JDBC instead of JPA?
This is what I have so far:
String sql = "UPDATE Sessions SET end = SYSDATE WHERE id = ?";
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setLong(1, someIdValue);
int updatedRows = statement.executeUpdate();
// ...
} catch(SQLException ex) {
Logger.getLogger(SessionsBean.class.getName()).log(Level.SEVERE, null, ex);
}
But of course SYSDATE
is not a generic expression and it will work only with Oracle as DBMS most likely.