Re: bytea problem - Mailing list pgsql-jdbc

From Thomas Kellerer
Subject Re: bytea problem
Date
Msg-id jagpe2$ikk$1@dough.gmane.org
Whole thread Raw
In response to bytea problem  (alkampfer <kazucat@gmail.com>)
Responses Re: bytea problem
Re: bytea problem
List pgsql-jdbc
alkampfer wrote on 22.11.2011 18:37:
> Hello, this is my problem :
> I use java + postgres and I want to store image files into a table
>
> my table : (id int,  image bytea)
>
> the code :
>                  byte bytes[] = new byte[(int)file.length()];
>
>                  sqlq ="UPDATE table set image= ? where id=?";
>                  ps = .....prepareStatement(sqlq);
>                  ps.setBytes(2,bytes);
>                  ps.setString(2,id);
>
> put the file into my table, stream of 41054 bytes, OK.
>
> but when I retrieve it from the database resultSet.getBytes("image") gives
> an array of 82108 bytes!!
> worst of all those bytes I get have no sense at all, like 00 00  00 or 11
> 11....

How do you read the data into the bytes[] array?

I usually use setBinaryStream() instead of setBytes(), that saves me from "manually" reading the file content into an
array:

InputStream in = new FileInputStream(file);
sqlq ="UPDATE table set image= ? where id=?";
ps = .....prepareStatement(sqlq);
ps.setBinarayStream(1, in, (int)file.length());
ps.setString(2,id);



pgsql-jdbc by date:

Previous
From: alkampfer
Date:
Subject: Re: bytea problem
Next
From: Mikko Tiihonen
Date:
Subject: SslTests failures