8

I am unable to insert a null value in my postgres database where the datatype is bytea(blob). This is my code snippet from java:

ps.setNull(++index, java.sql.Types.BLOB);

The bytea column is a nullable column. The following is the table description.

testdb=# \d+ plan 
                           Table "public.plan"
   Column    | Type  | Modifiers | Storage  | Stats target | Description 

-------------+-------+-----------+----------+--------------+-------------

description | bytea |           | extended |              | 

Has OIDs: no

I am getting the following exception java.sql.BatchUpdateException: Batch entry 11 INSERT INTO public.plan(description) VALUES(NULL) was aborted. Call getNextException to see the cause.

5
  • And what is the error you get? Commented Dec 7, 2015 at 7:07
  • 1
    java.sql.BatchUpdateException: Batch entry 11 INSERT INTO public.plan(description) VALUES(NULL) was aborted. Call getNextException to see the cause. This is the exception. Commented Dec 7, 2015 at 7:16
  • column "description" is of type bytea but expression is of type oid. How is it getting converted to oid. Commented Dec 7, 2015 at 7:40
  • Try setObject(++index, null) or setNull(++index, Types.OTHER) Commented Dec 7, 2015 at 7:41
  • Thanks man, it worked with setNull(++index, Types.OTHER). :) Commented Dec 7, 2015 at 7:46

2 Answers 2

7

Postgres has two different "BLOB" types: bytea, which is essentially what the SQL standard defines as a BLOB. And "large objects" which is more or less a "pointer" to binary storage (it's still stored inside the DB).

The Postgres JDBC has always treated "large objects" as the equivalent to BLOB (which I have never understood) and thus ps.setNull(++index, java.sql.Types.BLOB); makes the driver think you are dealing with a "large object" (aka "oid") column.

To overcome this, use

ps.setNull(++index, Types.OTHER);

alternatively you can use:

ps.setObject(++index, null);
Sign up to request clarification or add additional context in comments.

1 Comment

One question remains, how portable are these solutions? For posterity: I've positively tested ps.setObject(++index, null); on PostgreSQL 10.10, Oracle 12.1, MySQL 8.0.18 and Derby 10.14.
1

This also worked for me:

ps.setNull(++index, Types.BINARY);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.