I want to create a Blob object from a byte[] input to update a table using PreparedStatement#setBlob()
. In J2SE 6, we have java.sql.Connection#createBlob()
to get this done. Is there anything similar to this available in J2SE 1.5.0? What is the best way to update a BLOB type column with a byte[]
data in J2SE 1.5.0?
Asked
Active
Viewed 4.2k times
19

Buhake Sindi
- 87,898
- 29
- 167
- 228

ssethupathi
- 363
- 1
- 3
- 11
-
1The answer to your question is very much related to what jdbc driver you are using, and what database you are using. – Kaj Jun 08 '11 at 12:52
3 Answers
29
An example, using SerialBlob:
import java.sql.Blob;
import javax.sql.rowset.serial.SerialBlob;
byte[] byteArray = .....;
Blob blob = new SerialBlob(byteArray);

Buhake Sindi
- 87,898
- 29
- 167
- 228
8
You don't have to worry about creating Blob
objects at all. Treat them as blobs on the database, and byte[]
s in Java. For example:
@Entity
@Table(name = "some.table")
public class MyEntity
{
@Id
int myId;
@Lob
byte[] myBlob;
// snip getters & setters
}
If you're really intent on creating a Blob
instance yourself, you can use the SerialBlob
implementation:
byte[] bytes = ...;
Blob myBlob = new SerialBlob(bytes);

Matt Ball
- 354,903
- 100
- 647
- 710
0
You don't need to actually create the Blob itself. When doing a prepared statement, use a ByteArrayInputStream for the parameter when setting the blob parameter.

Mike Thomsen
- 36,828
- 10
- 60
- 83