Re: JDBC Blob helper class & streaming uploaded data into PG

From: Kris Jurka <books(at)ejurka(dot)com>
To: David Wall <d(dot)wall(at)computer(dot)org>
Cc: pgsql-jdbc <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: JDBC Blob helper class & streaming uploaded data into PG
Date: 2009-02-05 23:33:22
Message-ID: Pine.BSO.4.64.0902051821080.17892@leary.csoft.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On Wed, 4 Feb 2009, David Wall wrote:

> 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

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message David Wall 2009-02-05 23:57:40 Re: JDBC Blob helper class & streaming uploaded data into PG
Previous Message Kris Jurka 2009-02-05 23:21:05 Re: getHost()