Re: [GENERAL] Large objects + JDBC - Mailing list pgsql-general

From Gunther Schadow
Subject Re: [GENERAL] Large objects + JDBC
Date
Msg-id 38552195.43DA2CD9@aurora.rg.iupui.edu
Whole thread Raw
In response to Large objects + JDBC  (Marcin Mazurek - Multinet SA - Poznan <m.mazurek@multinet.pl>)
List pgsql-general
Marcin Mazurek - Multinet SA - Poznan wrote:
> Hi,
> I'm put several gifa into a table. I did as a exercise:) form psql using:
> INSERT INTO images (id, data)
>     VALUES (3, lo_import('/usr/local/apache/servlets/images/a.gif'));

are you sure this lo_import(...) thing in the SQL will work? I have no
idea ...

> but I have a problem with creating Java stream to read these data. Here
> serveral lines of code I was using:
> PreparedStatement ps=db.prepareStatement("select oid from imag where  id=?");
>        ps.setInt(1,1);
>        ResultSet rs = ps.executeQuery();
>        rs.next();
>        InputStream is = rs.getBinaryStream(0);
> and nothing happens:( Several messages from exceptions:
> [13/12/1999 18:11:04:815 CET] ShowImageServlet1: 0
> null
> Fastpath: ERROR:  lo_tell: large object descriptor (-1) out of range

I see two problems with your above Java code snippet:

#1: you SELECT oid FROM imag ... only the OID? Would you not have to select
the data?  A SELECT that would match your insert above would be:

SELECT data FROM images WHERE id=?;


#2: your index in rs.getBinaryStream is zero, but if I recall correctly
the JDBC ResultSet.getXXX(int) functions start counting at 1, not at
0.  So, you should have called:

       PreparedStatement ps=db.prepareStatement(
        "SELECT data FROM image WHERE id=?"); // select data instead of oid
       ps.setInt(1,1);
       ResultSet rs = ps.executeQuery();
       if(rs.next()) {
         InputStream is = rs.getBinaryStream(1);  // start with one (1)

       }

I have never done this BLOB stuff, but you might try whether this
fixes the problem. Also, with the JDBC driver comes a test that
does exactly image storing and retrieval. So you can look there
for a working example.

regards
-Gunther


--
Gunther_Schadow-------------------------------http://aurora.rg.iupui.edu
Regenstrief Institute for Health Care
1050 Wishard Blvd., Indianapolis IN 46202, Phone: (317) 630 7960
schadow@aurora.rg.iupui.edu------------------#include <usual/disclaimer>

Attachment

pgsql-general by date:

Previous
From: Dirk Lutzebaeck
Date:
Subject: vacuum analyze: Tuple is too big (corrupts DB?)
Next
From: Ed Loehr
Date:
Subject: [GENERAL/INTERFACES] Dynamically detecting need to vacuum?