Java:ResultSet getString() 因环境而异

发布于 2024-10-26 11:16:44 字数 445 浏览 3 评论 0原文

我有一个返回 Oracle Date 对象的 SQL 查询。 例如:

从 DUAL 中选择系统日期

当前有代码执行以下操作:

String s = rs.getString("sysdate");

问题是,这在不同环境下返回不同的日期格式(数据库相同)。

将返回一种环境: 2011-01-31 12:59:59.0

另一个会返回更奇怪的东西: 2011-1-31 12.15.32.0 (时间以小数分隔)

也许这与区域设置有关...一台机器是 java 报告的“英语(加拿大)”,另一台机器是“英语(美国)”。

我感兴趣的是,当结果集将日期对象转换为字符串时,该格式来自哪里?

I have a SQL query that is returning an oracle Date object.
e.g.:

SELECT sysdate FROM DUAL

There is code currently that does the following:

String s = rs.getString("sysdate");

The problem is, this returns different date format on different environments (database is the same).

One environment will return:
2011-01-31 12:59:59.0

The other will return something weirder:
2011-1-31 12.15.32.0 (the time is separated by decimals)

Maybe this has something do with Locale... one machine is "English (Canada)" as reported by java, the other is "English (United States)".

What I'm interested in is, when the resultset transforms the date object into a string, where is that format coming from?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄居者 2024-11-02 11:16:44

来自甲骨文网站:

在数据库连接时,JDBC
类库设置服务器
NLS_LANGUAGE 和 NLS_TERRITORY
与语言环境相对应的参数
运行 JDBC 的 Java VM
司机

所以是的,响应的差异是因为机器指定了不同的区域设置。正确的解决方案应该是使用 getDate() 或 getTimestamp() 或让数据库服务器以前面提到的特定格式将日期作为字符串返回。

From Oracle's site:

At database connection time, the JDBC
Class Library sets the server
NLS_LANGUAGE and NLS_TERRITORY
parameters to correspond to the locale
of the Java VM that runs the JDBC
driver

So yes, the difference in the response is because the machines have a different locale specified. The correct solution should be to use getDate() or getTimestamp() or have the database server return the date as a string in a specific format as mentioned earlier.

℉絮湮 2024-11-02 11:16:44

我建议根本不要调用getString。您要求的值不是字符串,而是日期或时间戳。因此,请改为调用 getDategetTimestamp。 (我不知道其中哪一个最合适;这取决于 sysdate 的确切语义。)

如果您然后需要格式化它,您可以这样做以适当的方式。

从根本上来说,在代码和数据库之间引入的文本转换越少越好。这是使用参数化查询的一个原因 - 您不必关心数据库如何解析数值或日期和时间;您只需关心数据库将如何解析数值或日期和时间;您只需提供价值。这是同一类事情,但方向相反。

I suggest not calling getString at all. The value you've asked for isn't a string, it's a date or timestamp. So call getDate or getTimestamp instead. (I don't know which of them is most appropriate offhand; it depends on the exact semantics of sysdate.)

If you then need to format it, you can do so in an appropriate manner.

Fundamentally, the fewer text conversions you introduce between your code and the database, the better. That's one reason to use parameterized queries - you don't have to care about how the database will parse numeric values or dates and times; you just provide the value. This is the same sort of thing, but in reverse.

樱花落人离去 2024-11-02 11:16:44

在 11g 驱动程序中,格式似乎是硬编码的。对结果集调用 getString() 最终会调用此方法:

oracle.sql.TIMESTAMPTZ.toString(int year, int month, int day, int hours, int minutes, int seconds, int nanos, String regionName)

对于日期,oracle.jdbc.driver.DateAccessor.getString() 会使用 nanos=-1 调用此方法,结果为使用格式“yyyy-mm-dd HH:MM:SS”

对于时间戳,格式为“yyyy-mm-dd HH:MM:SS.S”。将包含最多 9 位纳秒。

实际上,另一个有趣的结果是,从 10g 到 11g,这发生了显着变化:

10.2.0.5.0

select a DATE value and use getString to read it

2009-04-20 00:00:00.0

select a TIMESTAMP value and use getString to read it

2010-10-15.10.16. 16. 709928000

11.2.0.2.0

select a DATE value and use getString to read it

2009-04-20 00:00:00

select a TIMESTAMP value and use getString to read it

2010-10-15 10:16:16.709928

您的环境是否正在使用不同版本的Oracle JDBC 驱动程序?

In the 11g drivers the format seems to be hard-coded. Calling getString() on the result set ultimately calls this:

oracle.sql.TIMESTAMPTZ.toString(int year, int month, int day, int hours, int minutes, int seconds, int nanos, String regionName)

For dates, oracle.jdbc.driver.DateAccessor.getString() calls this with nanos=-1 and the result uses the format "yyyy-mm-dd HH:MM:SS"

For Timestamps, the format is "yyyy-mm-dd HH:MM:SS.S". Up to 9 digits of nanoseconds will be included.

Actually another interesting result is that this has changed significantly from 10g to 11g:

10.2.0.5.0:

select a DATE value and use getString to read it

2009-04-20 00:00:00.0

select a TIMESTAMP value and use getString to read it

2010-10-15.10.16. 16. 709928000

11.2.0.2.0:

select a DATE value and use getString to read it

2009-04-20 00:00:00

select a TIMESTAMP value and use getString to read it

2010-10-15 10:16:16.709928

Could your environments be using different versions of the Oracle JDBC drivers?

月下凄凉 2024-11-02 11:16:44

如果您确实希望将其作为字符串,那么最好在查询中使用 to_char 。

SELECT to_char(sysdate, 'MM/DD/YYYY') FROM DUAL; 

这将是一致的。

If you are really wanting it as a String you are better off using to_char in the query.

SELECT to_char(sysdate, 'MM/DD/YYYY') FROM DUAL; 

This will be consistent.

时常饿 2024-11-02 11:16:44

不要使用getString,而使用getDate(或getTimestamp)。这样它就不会依赖数据库引擎将其转换为字符串。

Don't use getString, use getDate (or getTimestamp). That way it will won't rely on the database engine to convert it to a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文