Java JDBC ResultSet getDate 丢失精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3266530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:09:03  来源:igfitidea点击:

JDBC ResultSet getDate losing precision

javaoracledatejdbcresultset

提问by orbfish

I am losing precision in my ResultSet.getDate(x) calls. Basically:

我在 ResultSet.getDate(x) 调用中失去了精度。基本上:

rs = ps.executeQuery();
rs.getDate("MODIFIED");

is returning dates truncated to the day where MODIFIED is an Oracle TIMESTAMP field of default precision. I think there may be some JDBC tweak I'm missing; usually TIMESTAMP is compatible with DATE, but I'm hoping I don't have to redefine the entire table.

返回截断到 MODIFIED 是默认精度的 Oracle TIMESTAMP 字段的日期的日期。我认为可能缺少一些 JDBC 调整;通常 TIMESTAMP 与 DATE 兼容,但我希望我不必重新定义整个表。

采纳答案by Affe

ResultSet.getDate()returns a java.sql.Date, not a java.util.Date. It is defined to be a timeless date. If you want a timestamp, use ResultSet.getTimestamp()!

ResultSet.getDate()返回 a ,而不是 a 。它被定义为一个永恒的日期。如果您想要时间戳,请使用!java.sql.Datejava.util.DateResultSet.getTimestamp()

回答by dpatchery

You should use java.sql.Timestamp instead of java.sql.Date. You can use it as a java.util.Date object afterward if necessary.

您应该使用 java.sql.Timestamp 而不是 java.sql.Date。如有必要,您可以在之后将其用作 java.util.Date 对象。

rs = ps.executeQuery();
Timestamp timestamp = rs.getTimestamp("MODIFIED");

Hope this helps.

希望这可以帮助。

回答by Ali

Using Timestap is the correct way. Please take not that with Timestamp you will not be able to set the columns to nullable if you were to use Liquibase.

使用 Timestap 是正确的方法。请注意,如果您使用 Liquibase,您将无法使用 Timestamp 将列设置为可为空。

A problem I came across as well.

我也遇到了一个问题。