tags:

views:

251

answers:

3

I have this JDBC SQL query:

select *
from table
where TX_DATE = {d '2009-01-05'} and TX_TIME = {t '15:23:39'}

This returns some rows. Note that, since Oracle has no TIME type, both columns are of type DATE.

But it fails when I use JDBC parameters:

select *
from table
where TX_DATE = ? and TX_TIME = ?

where the first parameter is new java.sql.Date(...) and the second is new java.sql.Time(...). I print both parameters to stdout and they look good, so the values are correct. But I don't get any rows. Why? What's different between {t '15:23:39'} and new java.sql.Time()?

[EDIT] Here is the code that fills the PreparedStatement:

public static void setParameters (final PreparedStatement stmt,
        final Object... param)
{
    for (int i=0; i<param.length; i++)
    {
        Object debug = param[i];
        String type = null;

        if (param[i] == null)
            stmt.setString(i+1, null);
        else if (param[i] instanceof java.sql.Time)
             stmt.setTime (i+1, (java.sql.Time)param[i]);

I've set a breakpoint in setTime() and it gets called. param[1].toString() prints 15:23:39, so I know the value is correct.

My guess is that since Oracle doesn't have a TIME type, there is a bug in the driver and the DATE part of the time is not ignored.

If I use select * on the whole table, I get

TX_DATE     TX_TIME
2009-01-08  2009-08-01

As you can seem, the time column is treated like a date by default. If I use TO_CHAR(TX_TIME, 'HH24:MI:SS'), I get:

TX_DATE     TX_TIME
2009-01-08  15:23:39

Where does 2009-08-01 come from?

A: 

I assume that you are using a PreparedStatement in this?

PrepapredStatement stmt = conn.prepareStatement(
               "select * from table where TX_DATE = ? and TX_TIME = ?")
stmt.setDate(1, new java.sql.Date(myDate.getTime));
stmt.setTimestamp(2, new java.sql.Timestamp(myDate.getTime));

ResultSet rs = stmt.executeQuery();
oxbow_lakes
As I wrote, I'm using java.sql.Time, not java.sql.Timestamp.
Aaron Digulla
I'm initializing the time with a SimpleDateFormat.parse() with the format "HH:mm:ss" and the value "15:23:39".
Aaron Digulla
Can you post yuour *actual code*. It's difficult to debug code which I can't see! Are you using `PreparedStatement` and saetting the parameters as I said (albeit with a time)? Have you tried using a timestamp instead, or just used the date bit to see if that worked?
oxbow_lakes
See my edit; my guess is that there is a bug in the Oracle driver due to the fact that Oracle doesn't have a real "TIME" type.
Aaron Digulla
Note that the type of both columns is DATE (since there is no native TIME type in Oracle).
Aaron Digulla
A: 

all your question about jdb:

http://java.sun.com/docs/books/tutorial/jdbc/index.html

If you want real help you will also have to specify what you consider 'Fails' no return values ? Sql Exception ,....

Peter
+2  A: 

Oracle does not have a Java type for time. The java.sql.Time class is mapped to the oracle.sql.DATE class, even in the latest drivers. Documentation Here What you should do is have the "day" portion of TX_TIME be some standard value (say 1970-01-01). Then you can query on this column with a static "day" and have the time work as expected.

Depending upon what version of the Oracle drivers you're using, the handling of java.sql.Date and java.sql.Timestamp for an Oracle DATE column type is different. Check the JDBC FAQ for more info. Good luck!

public static void setParameters (final PreparedStatement stmt,
    final Object... param)
{
  for (int i=0; i<param.length; i++)
  {
      Object debug = param[i];
      String type = null;

      if (param[i] == null)
        stmt.setString(i+1, null);
      else if (param[i] instanceof java.sql.Time) 
      {
        stmt.setTime (
          i+1, 
          java.sql.Timestamp.valueof("1970-01-01 " + param[i] + ".000000000")
         );
      }
Adam Hawkes
Thanks; I was somehow expecting that Oracle would use the same "day" part if I didn't specify one instead of "1st of the current month".
Aaron Digulla