File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes byte[] to blob Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "byte[] to blob" Watch "byte[] to blob" New topic

byte[] to blob

azhar bharat
Ranch Hand

Joined: Jul 17, 2006
Posts: 87
How do i convert a byte array to a Blob(java.sql.Blob) object?
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3753

Check out the PreparedStatement API. In particular look at setBinaryStream(), setUnicodeStream(), and setAsciiStream() which can be used to convert a stream to a blob during an insert/update statement.

[OCA 8 Book] [Blog]
azhar bharat
Ranch Hand

Joined: Jul 17, 2006
Posts: 87
I am aware of other options in prepared statements api.
But, i just want to know how to convert byte[] to Blob.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33106

You can create a ByteArrayInputStream from your byte[] and use that.

To create a Blob directly, you would have to write database specific code - something that should be avoided without a good reason.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: byte[] to blob
It's not a secret anymore!