0

I Have a Table WRITE_DATE_DATA having column DATE_DATA (DATE type) and TIMESTAMP_DATA (TIMESTAMP(6) Type).

When I am executing:

ResultSet rsmeta = ps.executeQuery("Select DATE_DATA,TIMESTAMP_DATA from WRITE_DATE_DATA")
ResultSetMetaData rsmeta = resultSet.getMetaData()  ;
int imax = rsmeta.getColumnCount() ;
HashMap<String, Integer> hmRsColTypes = new HashMap<String, Integer>();
for ( int ii = 0 ; ii < imax ; ii++ )
{
  String strColumn = rsmeta.getColumnName(ii+1);
  System.out.println(rsmeta.getColumnType(ii+1));// line 1
  System.out.println(rsmeta.getColumnType(ii+1));// line 2
}

Output:

at line 1 - TIMESTAMP_DATA

at line 2 -93 Type for java.sql.TimeStamp

at line 1 - DATE_DATA

at line one - 93 Type for java.sql.TimeStamp

Expected data type for DATE_DATE will be 91 Type for java.sql.Date

Why is ResultSetMetaData returning the wrong data type TimeStamp instead of Date data type?

Is this expected behavior, if yes then please share document or should i do something else to handle it.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Afgan
  • 1,022
  • 10
  • 32
  • @MarkRotteveel That question was answered for diffrence between Date and TIMESTAMP,,I am not asking difference , I am getting wrong data type of column in ResultSetMetaData. Is this expected behavior or should i do something else – Afgan Dec 08 '17 at 11:08

1 Answers1

3

The output shown is correct. An Oracle DATE is a java.sql.Timestamp (java.sql.Types.TIMESTAMP) as far as JDBC (and the SQL standard) is concerned as it has both a date and time component.

An Oracle DATE is simply not a date as defined in JDBC and the SQL standard. Use of java.sql.Date (and java.sql.Types.DATE) is for types that have only a date component (day, month, year).

See also: Difference between oracle DATE and TIMESTAMP

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • If both are behaving same then what's the need of Date type itself. Is there any Oracle official document, which elaborate this issue. – Afgan Dec 08 '17 at 11:31
  • @Afgan The oracle `DATE` type predates the SQL standard, other databases from that era have the same 'problem' (eg Firebird in the deprecated dialect 1, date is also a timestamp), Oracle **later** introduced `TIMESTAMP`, which provides sub-second precision options with closer alignment to the SQL standard. And, re _"Is there any Oracle official document, which elaborate this issue."_ probably, but you should search for that yourself, SO is not a 'mechanical turk'-search engine. – Mark Rotteveel Dec 08 '17 at 12:10