pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
binary data
15+ messages / 5 participants
[nested] [flat]

* binary data
@ 2003-01-13 20:07 Felipe Schnack <felipes@ritterdosreis.br>
  2003-01-14 01:16 ` Re: binary data Barry Lind <blind@xythos.com>
  0 siblings, 1 reply; 15+ messages in thread

From: Felipe Schnack @ 2003-01-13 20:07 UTC (permalink / raw)
  To: pgsql-jdbc

  I want to set binary data on a database field...
  It's actually a java compiled class, how should I do this? Using
preparedstatement.SetBinaryData and a bytea field?
  How I get it back?

-- 

Felipe Schnack
Analista de Sistemas
felipes@ritterdosreis.br
Cel.: (51)91287530
Linux Counter #281893

Centro Universitário Ritter dos Reis
http://www.ritterdosreis.br
ritter@ritterdosreis.br
Fone/Fax.: (51)32303341




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-01-13 20:07 binary data Felipe Schnack <felipes@ritterdosreis.br>
@ 2003-01-14 01:16 ` Barry Lind <blind@xythos.com>
  2003-01-14 10:34   ` Re: binary data Felipe Schnack <felipes@ritterdosreis.br>
  0 siblings, 1 reply; 15+ messages in thread

From: Barry Lind @ 2003-01-14 01:16 UTC (permalink / raw)
  To: Felipe Schnack <felipes@ritterdosreis.br>; +Cc: pgsql-jdbc

This is pretty well documented in the jdbc chapter of the postgresql 
documentation (section on Binary Data).  (At least all the code examples 
there used to work :-)

--Barry

Felipe Schnack wrote:
>   I want to set binary data on a database field...
>   It's actually a java compiled class, how should I do this? Using
> preparedstatement.SetBinaryData and a bytea field?
>   How I get it back?
> 






^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-01-13 20:07 binary data Felipe Schnack <felipes@ritterdosreis.br>
  2003-01-14 01:16 ` Re: binary data Barry Lind <blind@xythos.com>
@ 2003-01-14 10:34   ` Felipe Schnack <felipes@ritterdosreis.br>
  0 siblings, 0 replies; 15+ messages in thread

From: Felipe Schnack @ 2003-01-14 10:34 UTC (permalink / raw)
  To: pgsql-jdbc

  you're right, i'm sorry :-(

On Mon, 2003-01-13 at 23:16, Barry Lind wrote:
> This is pretty well documented in the jdbc chapter of the postgresql 
> documentation (section on Binary Data).  (At least all the code examples 
> there used to work :-)
> 
> --Barry
> 
> Felipe Schnack wrote:
> >   I want to set binary data on a database field...
> >   It's actually a java compiled class, how should I do this? Using
> > preparedstatement.SetBinaryData and a bytea field?
> >   How I get it back?
> > 
> 
> 
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
-- 

Felipe Schnack
Analista de Sistemas
felipes@ritterdosreis.br
Cel.: (51)91287530
Linux Counter #281893

Centro Universitário Ritter dos Reis
http://www.ritterdosreis.br
ritter@ritterdosreis.br
Fone/Fax.: (51)32303341




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* binary data
@ 2003-03-02 12:52 Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-03 03:35 ` Re: binary data Barry Lind <blind@xythos.com>
  2010-12-07 10:35 ` Re: binary data Werner Donné <werner.donne@re.be>
  2010-12-07 11:04 ` Re: binary data Werner Donné <werner.donne@re.be>
  0 siblings, 4 replies; 15+ messages in thread

From: Uwe Kubosch @ 2003-03-02 12:52 UTC (permalink / raw)
  To: 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
_________________________________________




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
@ 2003-03-02 13:21 ` Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:24   ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  3 siblings, 1 reply; 15+ messages in thread

From: Anders Hermansen @ 2003-03-02 13:21 UTC (permalink / raw)
  To: pgsql-jdbc

* Uwe Kubosch (donv@crusaders.no) wrote:
> Hi all!

Hi

> 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);

Why don't you show us the setContent method?

>   }
> } catch (SQLException anSQLException) {
> }


Anders

--
Anders Hermansen
YoYo Mobile as



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
@ 2003-03-02 13:24   ` Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:34     ` Re: binary data Anders Hermansen <anders@yoyo.no>
  0 siblings, 1 reply; 15+ messages in thread

From: Uwe Kubosch @ 2003-03-02 13:24 UTC (permalink / raw)
  To: Anders Hermansen <anders@yoyo.no>; pgsql-jdbc

Hi!

Thanks for the quick answer!

<code snipped>

> Why don't you show us the setContent method?

I doesn't do much, but here it is:

  private InputStream content;

  /**
   * Set content for this file.
   * @param newValue InputStream
   */
  public void setContent(InputStream newValue) {
    this.content = newValue;
  }

Hope you can make sense of it.

With kind regards,
Uwe Kubosch



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:24   ` Re: binary data Uwe Kubosch <donv@crusaders.no>
@ 2003-03-02 13:34     ` Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:44       ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  0 siblings, 1 reply; 15+ messages in thread

From: Anders Hermansen @ 2003-03-02 13:34 UTC (permalink / raw)
  To: pgsql-jdbc

* Uwe Kubosch (donv@crusaders.no) wrote:
> > Why don't you show us the setContent method?
> 
> I doesn't do much, but here it is:
> 
>   private InputStream content;
> 
>   /**
>    * Set content for this file.
>    * @param newValue InputStream
>    */
>   public void setContent(InputStream newValue) {
>     this.content = newValue;
>   }
> 
> Hope you can make sense of it.

That's not at much help. Let me rephrase that:

Show us the code that saves the InputStream to a file.


Anders

--
Anders Hermansen
YoYo Mobile as



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:24   ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:34     ` Re: binary data Anders Hermansen <anders@yoyo.no>
@ 2003-03-02 13:44       ` Uwe Kubosch <donv@crusaders.no>
  2003-03-02 14:37         ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 15:17         ` Re: binary data Anders Hermansen <anders@yoyo.no>
  0 siblings, 2 replies; 15+ messages in thread

From: Uwe Kubosch @ 2003-03-02 13:44 UTC (permalink / raw)
  To: Anders Hermansen <anders@yoyo.no>; pgsql-jdbc

Hi!

> Show us the code that saves the InputStream to a file.

This is a web-application that streams the InpuStream back to the browser.
The code that does this is like this:

byte[] buffer;
int bytesRead;

buffer = new byte[is.available()];

while (is.available() > 0) {
  bytesRead = is.read(buffer);
  response.getOutputStream().write(buffer, 0, bytesRead);
}

response.getOutputStream().flush();
is.close();


Uwe




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:24   ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:34     ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:44       ` Re: binary data Uwe Kubosch <donv@crusaders.no>
@ 2003-03-02 14:37         ` Anders Hermansen <anders@yoyo.no>
  2003-03-02 15:05           ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  1 sibling, 1 reply; 15+ messages in thread

From: Anders Hermansen @ 2003-03-02 14:37 UTC (permalink / raw)
  To: pgsql-jdbc

* Uwe Kubosch (donv@crusaders.no) wrote:
> This is a web-application that streams the InpuStream back to the browser.
> The code that does this is like this:
> 
> byte[] buffer;
> int bytesRead;
> 
> buffer = new byte[is.available()];
> 
> while (is.available() > 0) {
>   bytesRead = is.read(buffer);
>   response.getOutputStream().write(buffer, 0, bytesRead);
> }
> 
> response.getOutputStream().flush();
> is.close();

I can't see anything in your code that is not binary safe, or I'm
missing something obvious just like you. Maybe the gurus know.


Anders

--
Anders Hermansen
YoYo Mobile as



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:24   ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:34     ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:44       ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 14:37         ` Re: binary data Anders Hermansen <anders@yoyo.no>
@ 2003-03-02 15:05           ` Uwe Kubosch <donv@crusaders.no>
  0 siblings, 0 replies; 15+ messages in thread

From: Uwe Kubosch @ 2003-03-02 15:05 UTC (permalink / raw)
  To: Anders Hermansen <anders@yoyo.no>; pgsql-jdbc

> I can't see anything in your code that is not binary safe, or I'm
> missing something obvious just like you. Maybe the gurus know.

I hope so.  I really need to fix this quickly.  Maybe there are settings
that influence this?


Uwe




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:24   ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:34     ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:44       ` Re: binary data Uwe Kubosch <donv@crusaders.no>
@ 2003-03-02 15:17         ` Anders Hermansen <anders@yoyo.no>
  2003-03-02 15:34           ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  1 sibling, 1 reply; 15+ messages in thread

From: Anders Hermansen @ 2003-03-02 15:17 UTC (permalink / raw)
  To: pgsql-jdbc

* Uwe Kubosch (donv@crusaders.no) wrote:
> This is a web-application that streams the InpuStream back to the browser.
> The code that does this is like this:

Can you try the following? Maybe the result slipped through the driver
without being decoded?

// I think this will work because the is is a ByteArrayInputStream
byte[] buffer = new byte[is.available()];
is.read(buffer);

byte[] buffer2 = org.postgresql.util.PGbytea.toBytes(buffer);

response.getOutputStream().write(buffer2, 0, buffer2.length);
response.getOutputStream().flush();
is.close();


Anders

--
Anders Hermansen
YoYo Mobile as



^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:21 ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:24   ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 13:34     ` Re: binary data Anders Hermansen <anders@yoyo.no>
  2003-03-02 13:44       ` Re: binary data Uwe Kubosch <donv@crusaders.no>
  2003-03-02 15:17         ` Re: binary data Anders Hermansen <anders@yoyo.no>
@ 2003-03-02 15:34           ` Uwe Kubosch <donv@crusaders.no>
  0 siblings, 0 replies; 15+ messages in thread

From: Uwe Kubosch @ 2003-03-02 15:34 UTC (permalink / raw)
  To: Anders Hermansen <anders@yoyo.no>; pgsql-jdbc

> Can you try the following? Maybe the result slipped through the driver
> without being decoded?
>
> // I think this will work because the is is a ByteArrayInputStream
> byte[] buffer = new byte[is.available()];
> is.read(buffer);
>
> byte[] buffer2 = org.postgresql.util.PGbytea.toBytes(buffer);
>
> response.getOutputStream().write(buffer2, 0, buffer2.length);
> response.getOutputStream().flush();
> is.close();

OK, tried it.  buffer is 5527 bytes.  The PGbytea.toBytes(buffer) call
returned an array of size 5485.  Original file size was 4342 bytes.


Uwe




^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
@ 2003-03-03 03:35 ` Barry Lind <blind@xythos.com>
  3 siblings, 0 replies; 15+ messages in thread

From: Barry Lind @ 2003-03-03 03:35 UTC (permalink / raw)
  To: Uwe Kubosch <donv@crusaders.no>; +Cc: pgsql-jdbc

Uwe,

Can you submit a test case that demonstrates the problem?  I use the 
bytea type in my apps without problems, so there must be something 
different about how you are doing this.

thanks,
--Barry


Uwe Kubosch wrote:
> 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
> _________________________________________
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
> 





^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
@ 2010-12-07 10:35 ` Werner Donné <werner.donne@re.be>
  3 siblings, 0 replies; 15+ messages in thread

From: Werner Donné @ 2010-12-07 10:35 UTC (permalink / raw)
  To: pgsql-jdbc

> 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) {
> }

Hi,

I have the same problem. I'm using PostgreSQL 9.0.1, which came with
postgresql-8.4-701.jdbc4.jar. The database is using the UTF8-encoding.
The getInputStream() method seems to return the encoded byte stream
instead of the decoded one.

Best regards,

Werner.





^ permalink  raw  reply  [nested|flat] 15+ messages in thread

* Re: binary data
  2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
@ 2010-12-07 11:04 ` Werner Donné <werner.donne@re.be>
  3 siblings, 0 replies; 15+ messages in thread

From: Werner Donné @ 2010-12-07 11:04 UTC (permalink / raw)
  To: pgsql-jdbc

Hi,

I replaced the driver version with postgresql-9.0-801.jdbc4.jar and now it works
correctly. The version that comes with the PostgreSQL 9.0.1 installation package
for Mac OS X should not be used.

Best regards,

Werner.






^ permalink  raw  reply  [nested|flat] 15+ messages in thread


end of thread, other threads:[~2010-12-07 11:04 UTC | newest]

Thread overview: 15+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-01-13 20:07 binary data Felipe Schnack <felipes@ritterdosreis.br>
2003-01-14 01:16 ` Barry Lind <blind@xythos.com>
2003-01-14 10:34   ` Felipe Schnack <felipes@ritterdosreis.br>
2003-03-02 12:52 binary data Uwe Kubosch <donv@crusaders.no>
2003-03-02 13:21 ` Anders Hermansen <anders@yoyo.no>
2003-03-02 13:24   ` Uwe Kubosch <donv@crusaders.no>
2003-03-02 13:34     ` Anders Hermansen <anders@yoyo.no>
2003-03-02 13:44       ` Uwe Kubosch <donv@crusaders.no>
2003-03-02 14:37         ` Anders Hermansen <anders@yoyo.no>
2003-03-02 15:05           ` Uwe Kubosch <donv@crusaders.no>
2003-03-02 15:17         ` Anders Hermansen <anders@yoyo.no>
2003-03-02 15:34           ` Uwe Kubosch <donv@crusaders.no>
2003-03-03 03:35 ` Barry Lind <blind@xythos.com>
2010-12-07 10:35 ` Werner Donné <werner.donne@re.be>
2010-12-07 11:04 ` Werner Donné <werner.donne@re.be>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox