Re: How to use JDBC to update LargeObject - Mailing list pgsql-jdbc

From Satish Burnwal (sburnwal)
Subject Re: How to use JDBC to update LargeObject
Date
Msg-id 3A8C969225424C4D8E6BEE65ED8552DA89D6E5@XMB-BGL-41C.cisco.com
Whole thread Raw
In response to Re: How to use JDBC to update LargeObject  (Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>)
Responses Re: How to use JDBC to update LargeObject  (Kris Jurka <books@ejurka.com>)
List 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
thereis a way (without changing the OID of the large object).
 

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

-----Original Message-----
From: Heikki Linnakangas [mailto:heikki.linnakangas@enterprisedb.com] 
Sent: Thursday, January 21, 2010 1:27 PM
To: Satish Burnwal (sburnwal)
Cc: pgsql-jdbc@postgresql.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

pgsql-jdbc by date:

Previous
From: Heikki Linnakangas
Date:
Subject: Re: How to use JDBC to update LargeObject
Next
From: Saurabh Jain
Date:
Subject: Java equivalent data type for chkpass.