This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
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

Win a copy of Head First Android this week in the Android forum!
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: 3740

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: 32319

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]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
I agree. Here's the link:
subject: byte[] to blob
jQuery in Action, 3rd edition