55

what is the easiest way to convert a Blob into a byte array?I am using MYSQL and i want to convert a Blob datatype into a byte array.

Iam using java programming language:)

mu is too short
  • 426,620
  • 70
  • 833
  • 800
androidGuy
  • 5,553
  • 12
  • 39
  • 56

3 Answers3

89

the mySql blob class has the following function :

blob.getBytes

use it like this:

//(assuming you have a ResultSet named RS)
Blob blob = rs.getBlob("SomeDatabaseField");

int blobLength = (int) blob.length();  
byte[] blobAsBytes = blob.getBytes(1, blobLength);

//release the blob and free up memory. (since JDBC 4.0)
blob.free();
Timothy Groote
  • 8,614
  • 26
  • 52
60

The easiest way is this.

byte[] bytes = resultSet.getBytes("my_field");
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
0

You can also get bytes array from Blob instance by this way:

Blob myBlob = null;
byte[] bytes = myBlob.getBinaryStream().readAllBytes();
Muhammed_G
  • 375
  • 3
  • 7