pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to use JDBC to update LargeObject
6+ messages / 3 participants
[nested] [flat]
* How to use JDBC to update LargeObject
@ 2010-01-21 07:39 Satish Burnwal (sburnwal) <sburnwal@cisco.com>
0 siblings, 1 reply; 6+ messages in thread
From: Satish Burnwal (sburnwal) @ 2010-01-21 07:39 UTC (permalink / raw)
To: pgsql-jdbc
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.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to use JDBC to update LargeObject
@ 2010-01-21 07:56 Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
parent: Satish Burnwal (sburnwal) <sburnwal@cisco.com>
0 siblings, 1 reply; 6+ messages in thread
From: Heikki Linnakangas @ 2010-01-21 07:56 UTC (permalink / raw)
To: Satish Burnwal (sburnwal) <sburnwal@cisco.com>; +Cc: pgsql-jdbc
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
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to use JDBC to update LargeObject
@ 2010-01-21 12:10 Satish Burnwal (sburnwal) <sburnwal@cisco.com>
parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
0 siblings, 1 reply; 6+ messages in thread
From: Satish Burnwal (sburnwal) @ 2010-01-21 12:10 UTC (permalink / raw)
To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: 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.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
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to use JDBC to update LargeObject
@ 2010-01-21 15:20 Kris Jurka <books@ejurka.com>
parent: Satish Burnwal (sburnwal) <sburnwal@cisco.com>
0 siblings, 1 reply; 6+ messages in thread
From: Kris Jurka @ 2010-01-21 15:20 UTC (permalink / raw)
To: Satish Burnwal (sburnwal) <sburnwal@cisco.com>; +Cc: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; pgsql-jdbc
On Thu, 21 Jan 2010, Satish Burnwal (sburnwal) wrote:
> Even setBlob() method seems to be updating large object only partially.
If you want to reuse an existing blob you need to truncate it first:
http://java.sun.com/javase/6/docs/api/java/sql/Blob.html#truncate(long)
Note that there is a bug with truncate in all but the CVS JDBC drivers
that prevents you from truncating a blob to zero length. Released
drivers can only truncate down to one byte, but that's probably fine for
you at the moment.
Kris Jurka
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to use JDBC to update LargeObject
@ 2010-01-22 04:53 Satish Burnwal (sburnwal) <sburnwal@cisco.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 6+ messages in thread
From: Satish Burnwal (sburnwal) @ 2010-01-22 04:53 UTC (permalink / raw)
To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc
I am using Postgres 8.1 where truncate feature is not supported. Can you
pls confirm this ? This is src code in 8.3-605 AbstractJdbc2BlobClob
class and I do get the exception:
public synchronized void truncate(long len) throws SQLException
{
checkFreed();
if (!conn.haveMinimumServerVersion("8.3"))
throw new PSQLException(GT.tr("Truncation of large objects
is only implemented in 8.3 and later servers."),
PSQLState.NOT_IMPLEMENTED);
assertPosition(len);
lo.truncate((int)len);
}
-Satish
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to use JDBC to update LargeObject
@ 2010-01-23 00:04 Kris Jurka <books@ejurka.com>
parent: Satish Burnwal (sburnwal) <sburnwal@cisco.com>
0 siblings, 0 replies; 6+ messages in thread
From: Kris Jurka @ 2010-01-23 00:04 UTC (permalink / raw)
To: Satish Burnwal (sburnwal) <sburnwal@cisco.com>; +Cc: pgsql-jdbc
On Fri, 22 Jan 2010, Satish Burnwal (sburnwal) wrote:
> I am using Postgres 8.1 where truncate feature is not supported. Can you
> pls confirm this ? This is src code in 8.3-605 AbstractJdbc2BlobClob
> class and I do get the exception:
OK, then you cannot re-use the existing blob. You've got to remove the
old one and create a new one.
Kris Jurka
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2010-01-23 00:04 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-01-21 07:39 How to use JDBC to update LargeObject Satish Burnwal (sburnwal) <sburnwal@cisco.com>
2010-01-21 07:56 ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2010-01-21 12:10 ` Satish Burnwal (sburnwal) <sburnwal@cisco.com>
2010-01-21 15:20 ` Kris Jurka <books@ejurka.com>
2010-01-22 04:53 ` Satish Burnwal (sburnwal) <sburnwal@cisco.com>
2010-01-23 00:04 ` Kris Jurka <books@ejurka.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox