Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id CB23E632D72 for ; Thu, 5 Feb 2009 19:57:54 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 91446-06 for ; Thu, 5 Feb 2009 19:57:50 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail2.yozons.com (mail2.yozons.com [63.251.168.97]) by mail.postgresql.org (Postfix) with ESMTP id 8127C632D6F for ; Thu, 5 Feb 2009 19:57:50 -0400 (AST) Received: from [192.168.1.4] (pool-98-111-76-154.sttlwa.fios.verizon.net [98.111.76.154]) (authenticated bits=0) by mail2.yozons.com (8.12.10/8.12.10) with ESMTP id n15NvfTL027498 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Thu, 5 Feb 2009 15:57:41 -0800 Message-ID: <498B7CF4.2000501@computer.org> Date: Thu, 05 Feb 2009 15:57:40 -0800 From: David Wall Reply-To: d.wall@computer.org User-Agent: Thunderbird 2.0.0.19 (Windows/20081209) MIME-Version: 1.0 To: Kris Jurka CC: pgsql-jdbc Subject: Re: JDBC Blob helper class & streaming uploaded data into PG References: <4989EA02.5020703@computer.org> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200902/12 X-Sequence-Number: 19287 > >> Does anybody have a JDBC Blob helper class so we can use the >> setBlob()/getBlob() calls in JDBC for PG 8.3? It would be a class >> that implements the java.sql.Blob interface. > > You really ought to use the driver's Blob implementation. Right now > creating a new Blob isn't terribly straightforward. In theory the > JDBC 4 method Connection.createBlob should be used, but that has not > been implemented in the postgresql driver yet. > > The best way to do this at the moment is to insert a row with an empty > blob, retrieve that blob and then write data into it. > > CREATE TABLE test (id int, bigdata oid); > > INSERT INTO test VALUES (1, lo_creat(-1)); > > ResultSet rs = stmt.executeQuery("SELECT bigdata FROM test WHERE id = > 1"); > rs.next(); > Blob blob = rs.getBlob(1); > OutputStream out = blob.setBinaryStream(1); > // from java.util.zip. to compress the data. > GZIPOutputStream gz = new GZIPOutputStream(out); > while (!done) { > gz.write(your data); > } > gz.close(); > rs.close(); > > Kris Jurka Thanks, Kris. Interesting the create empty blob and then update is used since Oracle seems to require something similar, though we don't do much with Oracle any more, I do recall using the EMPTY_BLOB() function and then updating it after getting an Oracle specific Blob class. Yeah, that's why we need a Blob class that implements streaming through the java.sql.Blob interface. We have our own and use simple code like the following to INSERT (and it works for UPDATE, too): YoByteBlob ybb = new YoByteBlob(encryptedCompressedData); stmt.setBlob(1, ybb); When SELECTING, we can use: java.sql.Blob dbBlob = rs.getBlob(1); YoByteBlob ybb = new YoByteBlob(dbBlob); These work for us now, but our version only supports "all in memory" byte arrays and no streaming. I could share YoByteBlob if anybody was interested, but we'll need to do more work to make it all work so we can stream the data in and out. David