简体   繁体   中英

Hibernate java.sql.Date field return only date for Oracle

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

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

某些代码会很好,但是您是否尝试使用@Temporal(TemporalType.TIMESTAMP)注释日期字段?

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

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM