0

I am using oracle database for retrieving date and time but it is returning only date when I am using java.sql.Date . I have tried with java.sql.Timestamp but its gave exception as wrong column type. I tried with V8Compatible configuration. But it is also not working. I cannot change anything in the DB side. In DB that column is of type DATE.

Please Give a suggestion

4 Answers 4

0

Some code would be nice, but did you try to annotate your date field with @Temporal(TemporalType.TIMESTAMP)?

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

3 Comments

I can use @Temporal only for java.util.Date field. but i need to use java.sql.Date
Sorry, I thought you already are using java.util.Date. If you can change the type, does it work with java.util.Date? If needed, you can later convert it to java.sql.Date using java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
I tried with java.util.Date. But its not working for me
0

For this, you should use ojdbc14.jar and oracle.sql.DATE

Source

You can try for inserting timestamp with precision of 2. Check this Stack Overflow Question. As Oracle Date datatype is equivalent to java.sql.Timestamp in Java. Reference

Or simply go for java.util.Date, It should work. An easy way to format and insert. Something like this example

7 Comments

Thanks for your reply.. I can go with your first option but i need to handle both sql and oracle database. So if i use oracle.sql.DATE it wont satisfy both the database.
The second option i have tried. but its showing wrong column type exception.(found Date : expected timestamp). Also the third option is also not working for me. please help
Any way i have tried with the first option but its throwing error as Found: date, expected: raw(255)
For second option go with precison 2, it should work as per Oracle Documentation. Try formatting like DD-MON-YY and insert.docs.oracle.com/cd/B28359_01/server.111/b28318/…
But i cant change anything in inserting the data. It is from server side. i cant change those configuration
|
0

Prior to 9.2, the Oracle JDBC drivers mapped the DATE SQL type to java.sql.Timestamp. This made a certain amount of sense because the Oracle DATE SQL type contains both date and time information as does java.sql.Timestamp. The more obvious mapping to java.sql.Date was somewhat problematic as java.sql.Date does not include time information. It was also the case that the RDBMS did not support the TIMESTAMP SQL type, so there was no problem with mapping DATE to Timestamp.

In 9.2 TIMESTAMP support was added to the RDBMS. The difference between DATE and TIMESTAMP is that TIMESTAMP includes nanoseconds and DATE does not. So, beginning in 9.2, DATE is mapped to Date and TIMESTAMP is mapped to Timestamp. Unfortunately if you were relying on DATE values to contain time information, there is a problem.

There are several ways to address this problem in the 9.2 through 10.2 drivers:

Alter your tables to use TIMESTAMP instead of DATE. This is probably rarely possible, but it is the best solution when it is. For more information
Refer: http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-faq-090281.html#08_01

Comments

-1

make sure go with below code once, as first one is for timestamp creation and then go with get method for retrieving the date android time from oracle database.

......................

java.util.Date android = new java.util.Date();
      long t = android.getTime();
      java.sql.Date sqlDate = new java.sql.Date(t);
      java.sql.Time sqlTime = new java.sql.Time(t);
      java.sql.Timestamp sqlTimestamp = new java.sql.Timestamp(t);

......................

pstmt.getDate(2, sqlDate);
      pstmt.getTime(3, sqlTime);
      pstmt.getTimestamp(4, sqlTimestamp);
      pstmt.executeUpdate();

2 Comments

How does your answer reflect on the entire question? He's using hibernate, so one might assume that he's not trying to use prepared statements.
i thought it might give him a idea for getting code line to his mind...... as the timestamp will be reflecting to database to save the data in that format if we use, during insertion of the data, same way to reverse process to get the data.....as the saved format has to be same as the getting the data format.

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.