Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ResultSet.getString(Date) differs based on driver

Tags:

oracle

jdbc

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?

like image 680
Muruga Avatar asked Feb 15 '23 18:02

Muruga


1 Answers

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);      
}
like image 179
Mureinik Avatar answered Mar 05 '23 08:03

Mureinik