10

What is the easiest way to convert byte array into Blob data type in MYSQL with java programming language?

2
  • Have you tried anything yourself? Googling "java convert byte[] to sql.blob" gives a lot of hits .. Commented Jul 12, 2011 at 10:39
  • well i have been googling for some time now,thats y i finally thought of posting it here.I did convert byte array to a serialblob bt had a problem converting it back to a bytearray since my table column was of type blob.I am here to get a proper way of converting a byte array into a blob,thanks:) Commented Jul 12, 2011 at 10:45

3 Answers 3

8
Blob blob = connection.createBlob(); blob.setBytes(1, bytes); 
Sign up to request clarification or add additional context in comments.

2 Comments

well, you need the connection object, obviously :)
setBytes() returns number of bytes written. Should we write it inside loop for partial write? or single line of code will suffice? I have a 50KB byte[] all in memory which I want to save.
3

Blob fileBlob = new javax.sql.rowset.serial.SerialBlob(byteArray);

Comments

1

You may try this one, if you are using hibernate.. Possibly the easiest way! :)

Blob blob = Hibernate.createBlob(bytes); 

1 Comment

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.