Re: How to use JDBC to update LargeObject

From: "Satish Burnwal (sburnwal)" <sburnwal(at)cisco(dot)com>
To: "Heikki Linnakangas" <heikki(dot)linnakangas(at)enterprisedb(dot)com>
Cc: <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: How to use JDBC to update LargeObject
Date: 2010-01-21 12:10:29
Message-ID: 3A8C969225424C4D8E6BEE65ED8552DA89D6E5@XMB-BGL-41C.cisco.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Even setBlob() method seems to be updating large object only partially. This is my code:

CREATE TABLE image (name TEXT, oid OID);

select * from image;
name | oid
-----------------+-------
bigfile | 17416
(1 rows)

//update the blob in the database
org.postgresql.jdbc3.Jdbc3Blob blob = new org.postgresql.jdbc3.Jdbc3Blob((org.postgresql.PGConnection)con, 17416);
InputStream is = new FileInputStream("/root/myfile");
byte[] bytes = new byte[128];
int n = 0, pos = 1;
while ((n = is.read(bytes)) >= 1) {
blob.setBytes(pos, bytes, 0, n);
pos = pos + n;
}
is.close();
PreparedStatement sst = con.prepareStatement("update image set oid = ? where name = ?");
sst.setString(2, "bigfile");
sst.setBlob(1, blob);
int count = sst.executeUpdate();

This updates only partially the existing blob. I want the file contents to be changed (from 1 Mb to 100 kb). Tell me if there is a way (without changing the OID of the large object).

Satish
------------------------

-----Original Message-----
From: Heikki Linnakangas [mailto:heikki(dot)linnakangas(at)enterprisedb(dot)com]
Sent: Thursday, January 21, 2010 1:27 PM
To: Satish Burnwal (sburnwal)
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: [JDBC] How to use JDBC to update LargeObject

Satish Burnwal (sburnwal) wrote:
> I am using 8.1 Postgre JDBC. I want to know how can I use JDBC to
> update the contents of a LargeObject. For example, say I have created
> a LO for a file with 1 Mb of data. Later on, file contents have
> changed and are now just 100 kb and I want to update the LO. When I
> try to write to a large object using the method largeObject.write(byte
> [], off, len), it seems to be updating the first 100 kb data, not
> really replacing the existing 100 MB with the new data. pg_largeobject
> table still shows the same number of rows. Any idea how can I update
> the contents of a large object.

See manual, http://jdbc.postgresql.org/documentation/84/binary-data.html:

" To use the Large Object functionality you can use either the
LargeObject class provided by the PostgreSQL™ JDBC driver, or by using
the getBLOB() and setBLOB() methods.
Important

You must access Large Objects within an SQL transaction block. You can
start a transaction block by calling setAutoCommit(false). "

--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Saurabh Jain 2010-01-21 14:47:29 Java equivalent data type for chkpass.
Previous Message Heikki Linnakangas 2010-01-21 07:56:53 Re: How to use JDBC to update LargeObject