Muruga
Muruga

Reputation: 123

ResultSet.getString(Date) differs based on driver

I am using "Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options"

I have a table whose schema is

COLUMN_NAME     DATA_TYPE   DATA_TYPE_MOD   DATA_TYPE_OWNER    DATA_LENGTH
CITY            VARCHAR2    (null)              (null)          30
COUNTRY         VARCHAR2    (null)              (null)          30
DATE_TYPE       DATE        (null)              (null)           7
PARTNO          NUMBER      (null)              (null)          22
STATE           VARCHAR2    (null)              (null)          30
ZIP             VARCHAR2    (null)              (null)          30

I have written a simple java client to fetch the DATE_TYPE.

 public class DateIssue {  
  private void testDateOutput() {  
       Connection con = null;  
       Statement psmt = null;  
       try {  
            con = getConnection();  
            con.setAutoCommit(true);     
            psmt = con.createStatement();  
            String sql = "SELECT DATE_TYPE FROM EMP";  
            ResultSet rs = psmt.executeQuery(sql);  
            while (rs.next()) {  
                 String dateString = rs.getString(1);  
                 System.out.println("As String :"+ dateString);  
            }  
            con.close();  
       }  
       catch (SQLException e) {  
            e.printStackTrace();  
       }  
  }  
  private static Connection getConnection() {  
       Connection connection = null;  
       try {  
            Class.forName("oracle.jdbc.pool.OracleDataSource");  
            java.util.Properties info = new java.util.Properties();  
            info.put("user", "myuser");  
            info.put("password", "mypass");  
            info.put("oracle.jdbc.mapDateToTimestamp", "false");  
       connection = DriverManager.getConnection("jdbc:oracle:thin:@myserver:1521:myservicename", info);  
       }  
       catch (ClassNotFoundException e) {  
            e.printStackTrace();  
       } catch (SQLException e) {  
            e.printStackTrace();  
       }  
       return connection;  
  }  
   public static void main(String rgs[]) throws Exception {  
       DateIssue di = new DateIssue();  
       di.testDateOutput();  
       System.out.println("----------------------------------------------------");  
  }  

With ojdbc6.jar(12.1.0.1.0) the output is : As String :2013-11-12
With ojdbc6.jar(11.2.0.2.0) the output is : As String :2013-11-12 11:10:09

Java version : 1.7

Why the behavior changes in ojdbc6.jar(12.1.0.1.0) ? If I need the output in the format 2013-11-12 11:10:09 using ojdbc6.jar(12.1.0.1.0) what should I do?

Upvotes: 4

Views: 6957

Answers (3)

PbxMan
PbxMan

Reputation: 7623

In my case I couldn't afford to change thousands of code lines. It seems that oracle takes by default a variable called NLS_LANG. Setting this up solved the problem for me e.g. NLS_LANG=SPANISH_SPAIN.WE8ISO8859P15

http://www.oracle.com/technetwork/products/globalization/nls-lang-099431.html

Upvotes: 0

Stefan Großmann
Stefan Großmann

Reputation: 1026

There is a changed behaviour in ResultSet#getDate() which seems to be the root cause for this problem.

See: ResultSet#getDate() semantics.

Upvotes: 1

Mureinik
Mureinik

Reputation: 311428

You should never rely on the driver to implicitly convert a date to any particular string format - the format is an implementation detail of the driver. You should handle the conversion yourself.

This can either be done in the Java level:

/* executing the statement, etc. - snipped for clarity */
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (rs.next()) {  
    Date date = rs.getTimestamp(1);
    System.out.println("As String :"+ formatter.format(date));  
}

Or by the query itself:

/* Setting up the connection, etc. - snipped for clarity */
String sql = "SELECT TO_CHAR(date_type, 'yyyy-mm-dd hh24:mi:ss') FROM emp";
ResultSet rs = psmt.executeQuery(sql);  
while (rs.next()) {  
    String dateString = rs.getString(1);  
    System.out.println("As String :" + dateString);      
}

Upvotes: 7

Related Questions