Updating with null values

454 views
Skip to first unread message

zorgbargle

unread,
Feb 1, 2012, 4:04:18 PM2/1/12
to jDBI
Hi, again, separate question :)

Consider the following example method:

@SqlQuery("insert into mytable (field1, field2) values
(:field1, :field2)")
int storeFields(@Bind("field1") Integer field1, @Bind("field2")
Integer field2);

Assuming my table was created to allow null for field2, I expected to
be able to do:
myDAO.storeFields(1, null);
but this didn't work.

Is there a way of specifying a null value when binding? My short-term
solution was to create a second method that didn't require field2, but
this isn't ideal for me.

Thanks again,
Z

Brian McCallister

unread,
Feb 1, 2012, 6:10:12 PM2/1/12
to jd...@googlegroups.com

I need to look into the code, but binding null *should* work.

>
> Thanks again,
> Z

zorgbargle

unread,
Feb 2, 2012, 4:28:39 AM2/2/12
to jDBI
Hi,

I've put together two unit tests, one using JDBI and one using JDBC.
Both try to insert a null value for one of the columns. Using a
PreparedStatement directly seems to work, however using JDBI fails.
I've placed the two classes on gist:
https://gist.github.com/1722476

I've just also tried with the fluent API - using .bind("message",
(String)null) appears to work, however .bind(2, (String)null) doesn't.

It may be Derby-specific - in ObjectArgument.apply,
statement.setNull(position, Typs.OTHER) is called if value is null. It
looks like Derby's implementation expects the correct Type to still be
used.

Thanks - I've only been playing with JDBI for a day now, but I really
like what I'm seeing.

Z

On Feb 1, 11:10 pm, Brian McCallister <bri...@skife.org> wrote:
Reply all
Reply to author
Forward
0 new messages