Subject PreparedStatement with Date as Object
Author Steven Jardine
Hello all,

Here is another problem I have found with PreparedStatements. I have
found the error in both Jaybird 1.0.? and Jaybird 1.5 RC3. I have an
interface that provides access to different types of databases. My
implementation of this interface takes a String query and an Object[]
for parameters. I call setObject on each parameter in the Object[]. I
get the following error when I pass a java.util.Date object. I have
included a working code example and stack trace. Please let me know
what you think and if you need any additional information. Thanks.

Steve

try
{
//Gets a connection via FirebirdSQL Connection Pool.
Connection connection = Database.get();
try
{
String SQL = "update carriers set ca_expired = 'true' "
+ "where (upper(ca_status) = 'APPROVED' or upper(ca_status) =
'PENDING')"
+ "and ca_id in (select ca_id from carrier_insurance "
+ "where upper(cai_type) <> 'LIABILITY' and cast(cai_expire as
date) <= ?)";
PreparedStatement statement = connection.prepareStatement(SQL);
try
{
Calendar today = Calendar.getInstance();
statement.setObject(1, today.getTime());
statement.execute();
}
finally
{
statement.close();
}
connection.commit();
}
catch (SQLException e)
{
connection.rollback();
throw e;
}
finally
{
connection.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}

//Stack Trace.
org.firebirdsql.jdbc.field.TypeConvertionException: Error converting to
object.
at org.firebirdsql.jdbc.field.FBField.setObject(FBField.java:738)
at
org.firebirdsql.jdbc.AbstractPreparedStatement.setObject(AbstractPreparedStatement.java:208)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at
org.firebirdsql.pool.PooledPreparedStatementHandler.invoke(PooledPreparedStatementHandler.java:170)
at org.firebirdsql.pool.$Proxy1.setObject(Unknown Source)
at
manualdbadjustment.ManualAdjustment.executeCode(ManualAdjustment.java:110)
at
manualdbadjustment.ManualAdjustment.btnExecute_actionPerformed(ManualAdjustment.java:91)
at
manualdbadjustment.ManualAdjustment$1.actionPerformed(ManualAdjustment.java:77)
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1786)
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1839)
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at
javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:245)
at java.awt.Component.processMouseEvent(Component.java:5100)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at
java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3198)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)