oracle Java:ResultSet getString() 因环境而异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5396056/
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-09-18 23:07:56  来源:igfitidea点击:

Java: ResultSet getString() differs between environments

javasqloracledatetimeformatting

提问by Will

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

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

SELECT sysdate FROM DUAL

从 DUAL 中选择系统日期

There is code currently that does the following:

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

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

String s = rs.getString("系统日期");

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

一种环境将返回: 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)

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

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

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

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

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

回答by Rich Hill

From Oracle's site:

从 Oracle 的网站:

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

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

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.

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

回答by Jon Skeet

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

我建议根本不要打电话getString。您要求的值不是字符串,而是日期或时间戳。所以打电话getDategetTimestamp代替。(我不知道它们中的哪一个最合适;这取决于 的确切语义sysdate。)

If you thenneed 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 onereason 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.

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

回答by Anthony Hayward

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

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

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"

对于日期,oracle.jdbc.driver.DateAccessor.getString()使用 nanos=-1 调用它,结果使用格式“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.

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

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

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

10.2.0.5.0:

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:

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?

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

回答by Jacob Schoen

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

如果你真的想要它作为一个字符串,你最好在查询中使用 to_char 。

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

This will be consistent.

这将是一致的。

回答by Paul Tomblin

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.

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