Can't I fetch bytea values from my postgres db

718 views
Skip to first unread message

Manuel Pettersson

unread,
Jan 21, 2021, 12:12:29 PM1/21/21
to jDBI
Hello,

I am trying to map a postgres bytea value to a java byte[].

This is my table:
Screen Shot 2021-01-21 at 18.05.27.png
This is what it contains:
Screen Shot 2021-01-21 at 18.07.17.png

My DAO has this method:
@SqlQuery("SELECT ba from my_test fetch first 1 row only")
byte[] selectBytea();


But when I execute it, I get:
... Caused by: org.postgresql.util.PSQLException: Bad value for type byte : \x01 at org.postgresql.jdbc.PgResultSet.getByte(PgResultSet.java:2135) at org.jdbi.v3.core.mapper.PrimitiveMapperFactory.lambda$primitiveMapper$0(PrimitiveMapperFactory.java:64) at org.jdbi.v3.core.mapper.SingleColumnMapper.lambda$new$0(SingleColumnMapper.java:41) at org.jdbi.v3.core.mapper.SingleColumnMapper.map(SingleColumnMapper.java:55) at org.jdbi.v3.core.result.ResultSetResultIterator.next(ResultSetResultIterator.java:83)

Does anyone know what is going on?

I have also posted this question here on SO.

I am wondering if the wrong mapper is being used? ResultSet::getByte when it should be  ResultSet::getBytes?

Matthew Hall

unread,
Jan 21, 2021, 7:16:06 PM1/21/21
to jd...@googlegroups.com
I think the array return type is being interpreted as a set of bytes on individual rows, due to the array type. Try adding the @SingleValue annotation to the method.


-Matthew

--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jdbi/fa977461-bc49-4e3c-b904-3fbf718a805en%40googlegroups.com.

Brian McCallister

unread,
Jan 21, 2021, 7:45:37 PM1/21/21
to jd...@googlegroups.com
I think the jdbc driver also likes to return bytea as an InputStream

Manuel Pettersson

unread,
Jan 22, 2021, 2:04:55 AM1/22/21
to jDBI
thank you guys,
Matthew, yeah that seems to have been it. Works with @SingleValue.

Reply all
Reply to author
Forward
0 new messages