Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id 74FB19F37F0 for ; Tue, 22 Nov 2011 14:25:31 -0400 (AST) Received: from lo.gmane.org ([80.91.229.12]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1RSv2A-0002As-Ip for pgsql-jdbc@postgresql.org; Tue, 22 Nov 2011 18:25:31 +0000 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RSv1y-0002OG-98 for pgsql-jdbc@postgresql.org; Tue, 22 Nov 2011 19:25:18 +0100 Received: from host-188-174-136-37.customer.m-online.net ([188.174.136.37]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 22 Nov 2011 19:25:18 +0100 Received: from spam_eater by host-188-174-136-37.customer.m-online.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 22 Nov 2011 19:25:18 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-jdbc@postgresql.org From: Thomas Kellerer Subject: Re: bytea problem Date: Tue, 22 Nov 2011 19:25:07 +0100 Lines: 32 Message-ID: References: <1321983450992-5014176.post@n5.nabble.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: host-188-174-136-37.customer.m-online.net User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.8.1.21) Gecko/20090302 Thunderbird/2.0.0.21 Mnenhy/0.7.5.666 In-Reply-To: <1321983450992-5014176.post@n5.nabble.com> X-Archive-Number: 201111/68 X-Sequence-Number: 22403 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);