Storing Large Objects: ClassCastException - Mailing list pgsql-jdbc

From Carter Harrison
Subject Storing Large Objects: ClassCastException
Date
Msg-id D4857531-85AE-11D8-88DD-000A95A79856@mail.gatech.edu
Whole thread Raw
Responses Re: Storing Large Objects: ClassCastException
List 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:


<fontfamily><param>Courier</param><x-tad-bigger>LargeObjectManager
lobj = ((org.postgresql.PGConnection)conn).getLargeObjectAPI();

</x-tad-bigger></fontfamily>

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.


-----------------------------

<fontfamily><param>Times</param><bigger><bigger>
</bigger></bigger></fontfamily>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



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



pgsql-jdbc by date:

Previous
From: Brian Olson
Date:
Subject: AJ1Stmt.setObject()
Next
From: Oliver Jowett
Date:
Subject: Re: Storing Large Objects: ClassCastException