I have an Oracle database with column called UPDATED
marked in sqlDeveloper as TIMESTAMP(6)
. An example entry in that column is 05-MAY-18 04.49.45.000000000
I want to execute following query against it using prepared statement:
SELECT * FROM EXAMPLE_TABLE WHERE UPDATED > TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS'
.
The question is - how do I bind Java date timeobject (from org.joda.time.LocalDateTime
or java.time.LocalDateTime
or any other that keeps both data and time?
Here is a test snippet, no matter what I do I can't make it work (apart from situation where I bind new Date(2018, 5,5)
but firstly it is just date without time and secondly sql.Date
is outdated.
public class Dbaccess {
public static void main(String[] args) throws SQLException {
Connection connection = DriverManager.getConnection("someurl", "some_username", "some_password");
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM EXAMPLE WHERE UPDATED > TO_DATE(?, 'YYYY-MM-DD HH24:MI:SS')");
preparedStatement.setObject(1, LocalDateTime.now());
ResultSet resultSet = preparedStatement.executeQuery();
System.out.println("resultSet:" + resultSet);
while(resultSet.next()) {
System.out.println(resultSet.getString("some_id"));
}
}
}
Unfortunately, I am getting Exception in thread "main" java.sql.SQLException: Invalid column type
.
How should we these days binds such variable that is supposed to represent datetime?