Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

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: Thu, 5 Feb 2009 18:33:22 -0500 (EST)
  • Message-id: <Pine.BSO.4.64.0902051821080.17892@leary.csoft.net> <text/plain>



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



Home | Main Index | Thread Index

Privacy Policy | About PostgreSQL
Copyright © 1996 – 2012 PostgreSQL Global Development Group