X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id BBD00D1B8EB for ; Wed, 23 Jun 2004 08:29:21 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 43173-01 for ; Wed, 23 Jun 2004 11:29:22 +0000 (GMT) Received: from hercules.ellemedia.com (unknown [212.205.129.170]) by svr1.postgresql.org (Postfix) with ESMTP id AA0FFD1B519 for ; Wed, 23 Jun 2004 08:29:14 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by hercules.ellemedia.com (Postfix) with ESMTP id 2FF97150E5; Wed, 23 Jun 2004 14:27:50 +0300 (EEST) Received: from hermes.ellemedia.com (hermes.ellemedia.com [212.205.129.5]) by hercules.ellemedia.com (Postfix) with ESMTP id ED9FF150D9; Wed, 23 Jun 2004 14:27:43 +0300 (EEST) Received: from csd.uoc.gr (corfu.ellemedia.com [212.205.129.107]) by hermes.ellemedia.com (Postfix) with ESMTP id 22E5C32A792; Wed, 23 Jun 2004 14:30:36 +0300 (EEST) Message-ID: <40D969D6.1070900@csd.uoc.gr> Date: Wed, 23 Jun 2004 14:30:30 +0300 From: GP Reply-To: pagomen@csd.uoc.gr Organization: University of Crete User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: el, en-us, en MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org, GP Subject: BLOB support problem !!!! Content-Type: multipart/alternative; boundary="------------020103080006040307020003" X-Virus-Scanned: by AMaViS perl-11 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=1.9 tagged_above=0.0 required=5.0 tests=HTML_MESSAGE, HTML_TITLE_EMPTY, PLING_PLING X-Spam-Level: * X-Archive-Number: 200406/149 X-Sequence-Number: 10351 This is a multi-part message in MIME format. --------------020103080006040307020003 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Hi all, I have some java code to r/w a binary file in the database. According to the tutorial (http://doc.postgresintl.com/jdbc/ch08.html#jdbc-binary-data-example) there are 2 ways: 1) To use a 'bytea' type in the database CREATE TABLE images (imgname text, img bytea); File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)"); ps.setString(1, file.getName()); ps.setBinaryStream(2, fis, file.length()); ps.executeUpdate(); ps.close(); fis.close(); 2) Use the LargeObject API and the 'oid' type in the databse CREATE TABLE imageslo (imgname text, imgoid oid); // All LargeObject API calls must be within a transaction block conn.setAutoCommit(false); // Get the Large Object Manager to perform operations with LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI(); // Create a new large object int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE); // Open the large object for writing LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE); // Now open the file File file = new File("myimage.gif"); FileInputStream fis = new FileInputStream(file); // Copy the data from the file to the large object byte buf[] = new byte[2048]; int s, tl = 0; while ((s = fis.read(buf, 0, 2048)) > 0) { obj.write(buf, 0, s); tl += s; } // Close the large object obj.close(); // Now insert the row into imageslo PreparedStatement ps = conn.prepareStatement("INSERT INTO imageslo VALUES (?, ?)"); ps.setString(1, file.getName()); ps.setInt(2, oid); ps.executeUpdate(); ps.close(); fis.close(); I tried both of them BUT The first one throw an OutofMemoryException for files > 1.5 Mbyte (is this some kind of bug ??) And the second one doesn't throw any Exception or Error but it does NOT write anything in the database !! I did something wrong ? Has anyone experience in r/w blobs in postgres through jdbc ?? Thanks in advance George --------------020103080006040307020003 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit Hi all,

I have some java code to r/w a binary file in the database.
According to the tutorial (http://doc.postgresintl.com/jdbc/ch08.html#jdbc-binary-data-example)
there are 2 ways:

1) To use a 'bytea' type in the database
CREATE TABLE images (imgname text, img bytea);

File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, file.length());
ps.executeUpdate();
ps.close();
fis.close();

2) Use the LargeObject API and the 'oid' type in the databse

CREATE TABLE imageslo (imgname text, imgoid oid);

 // All LargeObject API calls must be within a transaction block
conn.setAutoCommit(false);

// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();

// Create a new large object
int oid = lobj.create(LargeObjectManager.READ | LargeObjectManager.WRITE);

// Open the large object for writing
LargeObject obj = lobj.open(oid, LargeObjectManager.WRITE);

// Now open the file
File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);

// Copy the data from the file to the large object
byte buf[] = new byte[2048];
int s, tl = 0;
while ((s = fis.read(buf, 0, 2048)) > 0) {
    obj.write(buf, 0, s);
    tl += s;
}

// Close the large object
obj.close();

// Now insert the row into imageslo
PreparedStatement ps = conn.prepareStatement("INSERT INTO imageslo VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setInt(2, oid);
ps.executeUpdate();
ps.close();
fis.close();

I tried both of them BUT
The first one throw an OutofMemoryException for files > 1.5 Mbyte (is this some kind of bug ??)
And the second one doesn't throw any Exception or Error but it does NOT write anything in the database !!

I did something wrong ?
Has anyone experience in r/w blobs in postgres through jdbc ??

Thanks in advance
George

--------------020103080006040307020003--