Java: ResultSet getString () is different between environments

I have an SQL query that returns an oracle Date object. eg:.

SELECT sysdate FROM DUAL

There is currently code:

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

The problem is that this returns a different date format in different environments (the database is the same).

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

Another will return something stranger: 2011-1-31 12.15.32.0(time is separated by decimal places)

Perhaps this has something to do with Locale ... one machine is English (Canada), as java reports, the other is English (USA).

What interests me when the result set converts a date object to a string, where does this format come from?

+3
source share
5 answers

Oracle:

JDBC NLS_LANGUAGE NLS_TERRITORY , Java, JDBC

, , , . getDate() getTimestamp() , .

+6

getString . , , , . getDate getTimestamp. ( , , sysdate.)

, .

, , . - , ; . , .

+4

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 .

: 10 11 :

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

Can your environments use different versions of Oracle JDBC drivers?

+4
source

If you really want this to be a string, you'd better use to_char in the request.

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

It will be consistent.

+3
source

Do not use getString, use getDate(or getTimestamp). Thus, he will not rely on the database engine to convert it to a string.

+1
source

All Articles