Thread: anyone know why this is failing

anyone know why this is failing

From
Dave Cramer
Date:
The following code fails at the getObject(1)

Dave

package org.postgresql.test.jdbc2;

import org.postgresql.test.TestUtil;
import java.util.Calendar;
import junit.framework.*;
import java.sql.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class GetXXXTest extends TestCase
{
  Connection con = null;

  public GetXXXTest(String name )
  {
    super(name);
  }
  protected void setUp() throws Exception
  {
    super.setUp();

    con = TestUtil.openDB();
    TestUtil.createTempTable(con, "test_interval", "initial timestamp
with time zone, final timestamp with time zone");

  }

  protected void tearDown() throws Exception
  {
    super.tearDown();
    con.close();
  }

  public void testGetObject() throws SQLException
  {
    PreparedStatement pstmt = con.prepareStatement("insert into
test_interval values (?,?)");
    Calendar cal = Calendar.getInstance();
    cal.add( Calendar.DAY_OF_YEAR, -1 );

    pstmt.setTimestamp(1, new Timestamp( cal.getTimeInMillis()));
    pstmt.setTimestamp(2, new Timestamp( System.currentTimeMillis() ));
    assertTrue(pstmt.executeUpdate() == 1);
    ResultSet rs = pstmt.executeQuery("select (final-initial) as diff
from test_interval");
    while (rs.next() )
    {
      String str = (String)rs.getString(1);

      assertNotNull(str);
      str = (String)rs.getObject(1);
      assertNotNull(str);
    }
  }

}
--
Dave Cramer
519 939 0336
ICQ # 14675561


Re: anyone know why this is failing

From
Kris Jurka
Date:

>     ResultSet rs = pstmt.executeQuery("select (final-initial) as diff
> from test_interval");
>     while (rs.next() )
>     {
>       String str = (String)rs.getString(1);
>
>       assertNotNull(str);
>       str = (String)rs.getObject(1);
>       assertNotNull(str);
>     }
>   }

The difference of two timestamps is an interval, a type which has no
equivalent Java class.  The driver tries to determine a custom class
mapping (like those for the geometry types) in

AbstractJdbc1Connection.getObject(String type, String value)

which has a bug in it.  I'm not sure I agree with your expectation that it
return a String, but that is certainly a position which could be argued.
I would have expected this to throw an Exception indicating that it
couldn't determine what to do with it.  Another option would be to provide
a PGInterval class and map it appropriately.

Kris Jurka