| From: | "Uwe Kubosch" <donv(at)crusaders(dot)no> | 
|---|---|
| To: | <pgsql-jdbc(at)postgresql(dot)org> | 
| Subject: | binary data | 
| Date: | 2003-03-02 12:52:19 | 
| Message-ID: | CHECLDNPGHELOIINBBLEKEEEEIAA.donv@crusaders.no | 
| Views: | Whole Thread | Raw Message | Download mbox | Resend email | 
| Thread: | |
| Lists: | pgsql-jdbc | 
Hi all!
I'm trying to store image data in a postgresql 7.3.2 data using the 7.3.2
jdbc3 drivers.  The table "tfile" contains an integer field "id" and a bytea
field "content".
I store the image with code similar to this:
  InputStream is;
  PreparedStatement statement;
  int rowsAffected;
  is = new FileInputStream("image.png");
  statement = getConnection().prepareStatement("UPDATE tfile SET content=?
WHERE id=4");
  getConnection().setAutoCommit(false);
  statement.setBinaryStream(1, is, MAX_FILE_SIZE);
  rowsAffected = statement.executeUpdate();
  getConnection().commit();
  getConnection().setAutoCommit(true);
  is.close();
}
This works fine, and the size of the "content" field in the database is
equal to the size of the file, 4342 bytes.
I fetch the image back from the database with code similar to this:
String query;
ResultSet resultSet;
query = "SELECT content FROM tfile WHERE id=4";
resultSet = null;
try {
  boolean more;
  resultSet = executeTheQuery(query);
  more = resultSet.next();
  if (more) {
    InputStream is;
    is = resultSet.getBinaryStream("content");
    setContent(is);
  }
} catch (SQLException anSQLException) {
}
I've read and re-read the documentation, and this should work.  However,
checking the size of the content InputStream shows that the size is 5527
bytes instead of 4342.  Doing a compare with the original image reveals what
seems like unicode character sequences.  For example "å" is replaced with
"Ã¥".
Is this correct behaviour? Must I convert binary data from a bytea field?
If I have missed something obvious, I apologize, but I hope someone can help
me, please.  Any help is greatly appreciated.
With kind regards,
Uwe Kubosch
_________________________________________
Uwe Kubosch
Adviser eCommerce
ICQ#: 71437007
More ways to contact me: http://wwp.icq.com/71437007
_________________________________________
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Anders Hermansen | 2003-03-02 13:21:00 | Re: binary data | 
| Previous Message | Tarjei Skorgenes | 2003-03-02 12:24:06 | Patch for possible PSQLException bug |