From: chris humphrey <chris@parallelsw.com>
To: pgsql-jdbc@postgresql.org
Subject: Re: bytea problem
Date: Tue, 22 Nov 2011 13:00:19 -0700
Message-ID: <4ECBFF53.9020303@parallelsw.com> (raw)
In-Reply-To: <jagpe2$ikk$1@dough.gmane.org>
References: <1321983450992-5014176.post@n5.nabble.com>
<jagpe2$ikk$1@dough.gmane.org>
I had this problem recently.
I had just upgraded from postgres 8 to 9, but did not get the jdbc
driver updated.
I read a posting that the bytea datatype was refactored in version 9,
and one indication was that twice as many bytes came out as went in...
this seems to be the case for you, so try looking at the version of the
jdbc driver and make sure you have a matching driver for your instance.
your code for the prepareStatement is how I am doing it and it now works
perfectly, since I updated my driver.
hope this helps.
On 11/22/2011 11:25 AM, Thomas Kellerer wrote:
> 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);
>
>
>
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-jdbc@postgresql.org
Cc: chris@parallelsw.com
Subject: Re: bytea problem
In-Reply-To: <4ECBFF53.9020303@parallelsw.com>
References: <1321983450992-5014176.post@n5.nabble.com> <jagpe2$ikk$1@dough.gmane.org> <4ECBFF53.9020303@parallelsw.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox