From: | Carter Harrison <gtg459b(at)mail(dot)gatech(dot)edu> |
---|---|
To: | pgsql-jdbc(at)postgresql(dot)org |
Subject: | Storing Large Objects: ClassCastException |
Date: | 2004-04-03 20:38:14 |
Message-ID: | D4857531-85AE-11D8-88DD-000A95A79856@mail.gatech.edu |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-jdbc |
I'm using Java to write an application that needs the ability to store
and retrieve pdf files from a postgresql database. I'm using the code
from the postgresql docs to store a pdf. At the bottom of this message
is the code I'm using but right here is the line that is producing a
ClassCastException:
LargeObjectManager lobj =
((org.postgresql.PGConnection)conn).getLargeObjectAPI();
I found a post earlier that dealt with this same problem but it wasn't
very helpful. Any help would be appreciated. Thanks in advance.
-----------------------------
To insert an image, you would use:
// 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();
-----------------------------
Carter R. Harrison
From | Date | Subject | |
---|---|---|---|
Next Message | Oliver Jowett | 2004-04-03 23:52:05 | Re: Storing Large Objects: ClassCastException |
Previous Message | Brian Olson | 2004-04-03 18:44:19 | AJ1Stmt.setObject() |