Thread: Storing/retreiving Large Objects

Storing/retreiving Large Objects

From
Wieger Uffink
Date:
Hi,

Im trying to store images in my postgres db through jdbc access. The
connections are pooled through poolman. So far I have only tried to get
the manual's example in section 8.5 to work. Ive included the example
below.

I have literally c&p the example and the inserting the image seems to go
well since it results in populated row in table 'images'.
WHen I try to retreive the the image either through ps.getBinaryStream()
or ps.getBlob() I get a Class cast exception thrown at me, nl:

java.lang.ClassCastException: java.lang.Integer
    at
com.codestudio.sql.PoolManResultSet.getBlob(PoolManResultSet.java:414)

Apparently the oid stored in the table is retreived as an integer, which
obviously cannot be cast into a BinaryStream.
ANyone have any idea what could be going wrong here? Maybe some poolman
type mapping configuration or something.

Thanks in advance,
Wieger



//Example 8-2. Using the JDBC Large Object Interface

//For example, suppose you have a table containing the file name of an
//image and you have a large object containing that image:

//CREATE TABLE images (imgname text, imgoid oid);

//To insert an image, you would use:

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

//retreiving
PreparedStatement ps = con.prepareStatement("SELECT oid FROM images
WHERE name=?");
ps.setString(1, "myimage.gif");
ResultSet rs = ps.executeQuery();
if (rs != null) {
    while(rs.next()) {
        InputStream is = rs.getBinaryInputStream(1);
        // use the stream in some way here
        is.close();
    }
    rs.close();
}
ps.close();


--

Wieger Uffink
tel: +31 20 468 6868
fax: +31 20 470 6905
web: http://www.usmedia.nl
email: wieger@usmedia.nl

--


Re: Storing/retreiving Large Objects

From
Barry Lind
Date:
FYI.  This should have gone to the pgsql-jdbc mail list instead of
pgsql-general.

What version are you using?  This area of the code has undergone some
significant work in 7.2.  I would suggest you try using the 7.2 jdbc
code (which will run fine against a 7.1 database).  Also look at the 7.2
documentation as it also has been updated significantly in this area.

I cannot reproduce your problem using the 7.2 drivers given the test
case below.

thanks,
--Barry




Wieger Uffink wrote:

> Hi,
>
> Im trying to store images in my postgres db through jdbc access. The
> connections are pooled through poolman. So far I have only tried to get
> the manual's example in section 8.5 to work. Ive included the example
> below.
>
> I have literally c&p the example and the inserting the image seems to go
> well since it results in populated row in table 'images'.
> WHen I try to retreive the the image either through ps.getBinaryStream()
> or ps.getBlob() I get a Class cast exception thrown at me, nl:
>
> java.lang.ClassCastException: java.lang.Integer
>     at
> com.codestudio.sql.PoolManResultSet.getBlob(PoolManResultSet.java:414)
>
> Apparently the oid stored in the table is retreived as an integer, which
> obviously cannot be cast into a BinaryStream.
> ANyone have any idea what could be going wrong here? Maybe some poolman
> type mapping configuration or something.
>
> Thanks in advance,
> Wieger
>
>
>
> //Example 8-2. Using the JDBC Large Object Interface
>
> //For example, suppose you have a table containing the file name of an
> //image and you have a large object containing that image:
>
> //CREATE TABLE images (imgname text, imgoid oid);
>
> //To insert an image, you would use:
>
> File file = new File("myimage.gif");
> FileInputStream fis = new FileInputStream(file);
> PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES
> (?, ?)"); (1)
> ps.setString(1, file.getName());
> ps.setBinaryStream(2, fis, file.length());
> ps.executeUpdate();
> ps.close();
> fis.close();
>
> //retreiving
> PreparedStatement ps = con.prepareStatement("SELECT oid FROM images
> WHERE name=?");
> ps.setString(1, "myimage.gif");
> ResultSet rs = ps.executeQuery();
> if (rs != null) {
>     while(rs.next()) {
>         InputStream is = rs.getBinaryInputStream(1);
>         // use the stream in some way here
>         is.close();
>     }
>     rs.close();
> }
> ps.close();
>
>
>