Re: JDBC + large objects problem - Mailing list pgsql-interfaces

From Rob Judd
Subject Re: JDBC + large objects problem
Date
Msg-id Pine.LNX.4.21.0008251709380.28163-100000@mlug.missouri.edu
Whole thread Raw
In response to JDBC + large objects problem  (Patrick Goodwill <goodwill@cheese.stanford.edu>)
List pgsql-interfaces
The problem is that the JDBC interface has not yet implemented a lot of
the functionality, so you get these "not supported" Exceptions.  On the
bright side, for every problem like this there seems to be enough
functionality to achieve what you need.

In this case it is a little simpler:

Instead of:

InputStream textStream = stringToStream(text);
pstmt.setBinaryStream(3, textStream, text.length());

you can use:

pstmt.setBytes(3, string.getBytes()) ;

and to retrieve you can use ResultSet.getBytes() to return the byte array.

Hope this helps, Rob

> I'm trying to use JDBC and BLOBS to store large amounts of text in a
> database.  I get a strange error when I try to use the conventional JDBC
> interface... it comes out with a SQL Exceptions of:
> 
> "InputStream as parameter not supported"
> 
> for the code:
> 
>         Connection conn = pool.getConnection();        
>         PreparedStatement pstmt = 
>         conn.prepareStatement("INSERT INTO t" + book 
>                       + "_data (author_id, title, text,
>                                       type) VALUES ( ?, ?, ?, ?)");
>         pstmt.setInt(1, userId);
>         pstmt.setString(2, title);
>         InputStream textStream = stringToStream(text);
>         pstmt.setBinaryStream(3, textStream, text.length());
>         pstmt.setInt(4, type);
>         pstmt.executeUpdate();        
>         pstmt.close();
> 
> 
> ... with some helper functions....
> 
>     private InputStream stringToStream(String string) {
>     byte[] bytes = string.getBytes();
>     ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
>     return (InputStream) stream;
>     }
> 
>     private String      streamToString(InputStream stream) {
>     try {
>         int length = stream.available();
>         byte[] bytes = new byte[length];
>         stream.read(bytes);
>         return new String(bytes);
>     } catch (IOException e) {
>         System.out.println("No Stream");
>     }
>     return null;
>     }
> 
> with an abbreviated schema of....
> 
> >> \d t1_data
>                                Table "t1_data"
>  Attribute |  Type   |                       Modifier
> -----------+---------+-------------------------------------------------------
>  data_id   | integer | not null default
>                                nextval('t1_data_data_id_seq'::text)
>  author_id | integer |
>  title     | text    |
>  text      | oid     |
>  type      | integer |
>  time      | time    |
> Index: t1_data_pkey   
> 
>     
> .... using postgresql 7.0 and the newest JDBC driver from retep.org.uk
> 
> 
> if ya'll have any ideas why it does what it does, i just might kiss your
> feet.  =)
> 
> -Patrick.
> 
> 



pgsql-interfaces by date:

Previous
From: Patrick Goodwill
Date:
Subject: JDBC + large objects problem
Next
From: Joachim Achtzehnter
Date:
Subject: Re: JDBC + large objects problem