I have an SQL which looks into a dimension table (which stores every dates until year 2020) and then shall retrieve the todays row. I watched into the table, todays date is in there. The problem is, that SQL does not return any result.
I am thinking of a problem related to the use of java.sql.PreparedStatement.setDate method. In past i think this was working fine, now I did some kine of regression test and it failed. The differences to the past are having Oracle 12 DB now instead of 11 in past and running it on CentOS 6.5 instead of AIX.
On search I found this topic here: Using setDate in PreparedStatement
As far as I can see, I am doing as suggested.
Heres the java code and the query:
public static String SELECT_DATUM = "SELECT TIME_ID, DATE, DAY_NAME, WEEK_NAME, MONTH_NAME, YEAR_NAME, SORTING, RELATIONDATE, VALID_TO, VALID_FROM FROM DIM_TIME WHERE DATE = :date";
java.util.Calendar now = Calendar.getInstance();
now.clear(Calendar.HOUR);
now.clear(Calendar.MINUTE);
now.clear(Calendar.SECOND);
now.clear(Calendar.MILLISECOND);
Date tmpDate = now.getTime();
Date tmpDate2 = new Date(((java.util.Date)tmpDate ).getTime());
statement.setDate(1, tmpDate2 );
I notice that getTime() is called twice. But I dont think its that bad. I also noticed some displaying formats: in Database the date-colums shows me the date like this: '08.11.2015' in java while debugging tmpDate2 shows me a date like this: '2015-11-08' in java while debugging tmpDate shows me a date like this 'Sun Nov 08 12:00:00 CET 2015'
But again, these are just display formattings while it is a dateobject in background and a date-type in database. I would expect that je JDBC driver would map this itself without formattings, that why we are using setDate method and not setString.
What am I doing wrong? What could I do for further debugging to get it? I would like see the resulting SQL query which is finally executed with the parameter.
I tried this sql on db isntance:
SELECT * FROM v$sql s WHERE s.sql_text LIKE '%select time%' ;
but only getting this then: "... where date = trunc(:1 )" On this row at least I can see that it was using the right schema I expected it to use and where I checked whether todays date is available.
Edit: something I found out: I saw another code using the same function but giving an GregorianCalendar instead Calendar. When using new GregorienCalandar(); instead of Calendar.getInstance(); Theres no difference. But when I assign a date and dont let the system take the current time, then it works: Using new GregorianCalendar(2015, Calendar.NOVEMBER, 8); Would retrieve the row I want from SQL.