Java:ResultSet getString()在环境之间不同 [英] Java: ResultSet getString() differs between environments

查看:337
本文介绍了Java:ResultSet getString()在环境之间不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


SELECT sysdate FROM DUAL


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


String s = rs.getString(sysdate); p>

问题是,这会在不同的环境中返回不同的日期格式(数据库是一样的)。



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



另一个将返回一些weirder:
2011-1-31 12.15.32.0 (时间用小数分隔)



也许这与Locale有关...一个机器是由java报导的英语(加拿大),另一个是英语(美国)。



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

解决方案

从Oracle的网站:


在数据库连接时,JDBC
类库设置服务器
NLS_LANGUAGE和NLS_TERRITORY
参数以对应于运行JDBC
驱动程序的Java VM的区域设置


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


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?

解决方案

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.

这篇关于Java:ResultSet getString()在环境之间不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆