Обсуждение: Storing Large Objects: ClassCastException

Поиск
Список
Период
Сортировка

Storing Large Objects: ClassCastException

От
Carter 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:


<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



Re: Storing Large Objects: ClassCastException

От
Oliver Jowett
Дата:
Carter Harrison wrote:
> 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();

Are you using a connection pooling implementation?

If so, are you using a recent (CVS is probably safest) driver? In older
drivers the proxy connection objects produced by the driver (and
commonly used by connection pools) failed to implement PGConnection.

Failing that, is it possible that your client code is loading the
postgresql classes (org.postgresql.PGConnection in this case) via a
different classloader to the classloader that loaded the JDBC driver itself?

-O