4

I'm trying to convert the systimestamp of the (Oracle) database to a java.sql.Timestamp

(Part of) the code I have for testing the process is:

try {
    conn = this.getConnection();
    Statement stmt = conn.createStatement();

    stmt.execute("SELECT SYSTIMESTAMP FROM DUAL");

    ResultSet rs = stmt.getResultSet();
    ResultSetMetaData rsm = rs.getMetaData();

    while(rs.next()) {
        TIMESTAMPTZ ts = (TIMESTAMPTZ) rs.getObject(1);
        System.out.println(ts);
        System.out.println(ts.getClass());
        System.out.println(ts.timestampValue());
    }
} catch (SQLException e) {
    e.printStackTrace();
    System.out.println(e);
}

The output I get is:

oracle.sql.TIMESTAMPTZ@1e97f9f
class oracle.sql.TIMESTAMPTZ

and then an exception:
java.sql.SQLException: Conversion to Timestamp failed
at oracle.sql.Datum.timestampValue(Datum.java:283)
at com.sumawest.test.model.UnitTest.testTimezone(UnitTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at oracle.jdevimpl.junit.runner.TestRunner.doRun(TestRunner.java:111)
at oracle.jdevimpl.junit.runner.TestRunner.start(TestRunner.java:91)
at oracle.jdevimpl.junit.runner.TestRunner.main(TestRunner.java:43)
java.sql.SQLException: Conversion to Timestamp failed

Does anyone have an idea how I can solve this?

2 Answers 2

5

If you decompile the code you can see that you are calling the timestampValue method of the parent Datum class.

This directly throws the exception (no other code in the method). You need to call the timestampValue method of TIMESTAMPTZ itself passing a Connection.

Sign up to request clarification or add additional context in comments.

Comments

0

You could use the to_char function to convert it to a string and then use the value.

select to_char(systimestamp, 'IYYY-MM-DD HH24:MI:SS.FF TZD') as d from dual;

1 Comment

How does this answer helps converting to java.sql.Timestamp?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.