Thread: Fwd: Large Objects (please help)

Fwd: Large Objects (please help)

From
Peter T Mount
Date:
I'm forwarding this to the jdbc list.

Peter

>Hi Peter,
>
>I am trying to insert and/or select from Postgres a gif image by using
>the Large Object type.   I am writing a Java client and middle tier to a
>Postgres db backend using your JDBC driver and I have hit an issue with
>what your documantation shows and the exceptions I am seeing when I run
>a simple comand line program.
>
>here is the java code I am running:
>
>....
>   File file = new File("...(some path).../tomcat.gif");
>   FileInputStream fis = new FileInputStream(file);
>   PreparedStatement ps = db.prepareStatement("insert into image values
>(?,?)");
>   ps.setString(1,file.getName());
>   //ps.setObject(2,fis,java.sql.Types.LONGVARBINARY);
>   ps.setBinaryStream(2,fis, file.length());
>   ps.executeUpdate();
>   ps.close();
>   fis.close();
>....
>
>The code is almost verbatim from your documentation.
>"image" is a table  containing a image name and oid attributes.  I have
>tried both the statements,  ps.setBinaryStream(2,fis, file.length())
>and  ps.setObject(2,fis,java.sql.Types.LONGVARBINARY).  The first
>statement produces a compile time error :
>
>main.java:70: setBinaryStream(int,java.io.InputStream,int) in
>java.sql.PreparedStatement cannot be applied to
>(int,java.io.FileInputStream,long)
>   ps.setBinaryStream(2,fis, file.length());
>
>The JDBC API confuses me a little.  Why doesn't the JDBC interface
>define a long for this argument?  After all, we are dealing with big
>byte streams.  Hmm.
>
>The second statement, ps.setObject(2,fis,java.sql.Types.LONGVARBINARY),
>which I thought would be a good substitute produces a runtime exception:
>
>Unknown Types value.
>  at
>org.postgresql.jdbc2.PreparedStatement.setObject(PreparedStatement.java:492)
>
>  at
>org.postgresql.jdbc2.PreparedStatement.setObject(PreparedStatement.java:498)
>
>  at main.main(main.java:69)
>
>Does your driver fully implement the JDBC API?  LONGVARBINARY  is a
>valiid java.sql data type.  Maybe I have an old version of your driver.
>
>Here is my environment:
>PostgreSQL 7.0.3 on i686-pc-linux-gnu, compiled by gcc 2.96
>jdbc7.0-1.2.jar
>java full version "1.4.0-beta2-b77"
>uname -a
>Linux localhost.localdomain 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686
>unknown
>
>Any help would be appreciated.  Thank you.
>
>Cheers,
>Brian Buck



Re: Fwd: Large Objects (please help)

From
Dave Harkness
Date:
Hi Brian,

At 07:03 AM 10/8/2001, you wrote:
>>I am trying to insert and/or select from Postgres a gif image by using
>>the Large Object type. ...
>>
>>The first statement [ps.setBinaryStream(2,fis, file.length())] produces a
>>compile time error :
>>
>>main.java:70: setBinaryStream(int,java.io.InputStream,int) in
>>java.sql.PreparedStatement cannot be applied to
>>(int,java.io.FileInputStream,long)
>>   ps.setBinaryStream(2,fis, file.length());
>>
>>The JDBC API confuses me a little.  Why doesn't the JDBC interface
>>define a long for this argument?  After all, we are dealing with big
>>byte streams.  Hmm.

I don't know, but that's a JDBC issue. Have you tried simply casting the
long to an int? I'm using the same code as you and it works. The next
problem you're sure to encounter is an invalid OID value (0). This is
caused by trying to access large objects outside of a transaction. I assume
you aren't using transactions since I don't see a "conn.commit()" after the
executeUpdate() call.

>>Maybe I have an old version of your driver.
>>
>>jdbc7.0-1.2.jar

Yes indeed. 7.1 is the latest stable release driver, and 7.2 is in development.

Peace,
Dave