Java:ResultSet getString() 因环境而异
我有一个返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来自甲骨文网站:
所以是的,响应的差异是因为机器指定了不同的区域设置。正确的解决方案应该是使用 getDate() 或 getTimestamp() 或让数据库服务器以前面提到的特定格式将日期作为字符串返回。
From Oracle's site:
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.
我建议根本不要调用
getString
。您要求的值不是字符串,而是日期或时间戳。因此,请改为调用getDate
或getTimestamp
。 (我不知道其中哪一个最合适;这取决于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 callgetDate
orgetTimestamp
instead. (I don't know which of them is most appropriate offhand; it depends on the exact semantics ofsysdate
.)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.
在 11g 驱动程序中,格式似乎是硬编码的。对结果集调用 getString() 最终会调用此方法:
对于日期,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:
11.2.0.2.0:
您的环境是否正在使用不同版本的Oracle JDBC 驱动程序?
In the 11g drivers the format seems to be hard-coded. Calling
getString()
on the result set ultimately calls this: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:
11.2.0.2.0:
Could your environments be using different versions of the Oracle JDBC drivers?
如果您确实希望将其作为字符串,那么最好在查询中使用 to_char 。
这将是一致的。
If you are really wanting it as a String you are better off using to_char in the query.
This will be consistent.
不要使用
getString
,而使用getDate
(或getTimestamp
)。这样它就不会依赖数据库引擎将其转换为字符串。Don't use
getString
, usegetDate
(orgetTimestamp
). That way it will won't rely on the database engine to convert it to a string.