Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 6B9FB9F37F1 for ; Tue, 22 Nov 2011 16:00:27 -0400 (AST) Received: from out2.smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RSwW1-0000l2-Pp for pgsql-jdbc@postgresql.org; Tue, 22 Nov 2011 20:00:27 +0000 Received: from compute3.internal (compute3.nyi.mail.srv.osa [10.202.2.43]) by gateway1.nyi.mail.srv.osa (Postfix) with ESMTP id 5549220A5D for ; Tue, 22 Nov 2011 15:00:13 -0500 (EST) Received: from frontend1.nyi.mail.srv.osa ([10.202.2.160]) by compute3.internal (MEProxy); Tue, 22 Nov 2011 15:00:13 -0500 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=message-id:date:from:mime-version:to :subject:references:in-reply-to:content-type :content-transfer-encoding; s=smtpout; bh=pu0p3gyhg52PaK9ZV0hv/b +Gho8=; b=NOTqd6tr9qVLyFF6mK0a6/dwHKaChMvZ7cch2MxwByuNtZcIy5Nbm0 wc0j5Ee5yleFuUrNCCu1GvTBEnDPL2ug05vEZnzzYr7LX0+dGcXarYh3y3JEOvK7 t/2D7D9Bt1DqRfoXQ1nDSEjHJpAuKZahLhh0cukgcScaIPILp5JNQ= X-Sasl-enc: G5GCcj64IsJemkA/zBBqlZEip7ImVfkscZqiWNyTkmJL 1321992013 Received: from [10.153.98.62] (unknown [162.119.232.100]) by mail.messagingengine.com (Postfix) with ESMTPSA id EB77D8E0178 for ; Tue, 22 Nov 2011 15:00:12 -0500 (EST) Message-ID: <4ECBFF53.9020303@parallelsw.com> Date: Tue, 22 Nov 2011 13:00:19 -0700 From: chris humphrey User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Re: bytea problem References: <1321983450992-5014176.post@n5.nabble.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 201111/71 X-Sequence-Number: 22406 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); > > > >