Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to convert a Blob into a byte array

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

like image 817
androidGuy Avatar asked Jul 12 '11 10:07

androidGuy


People also ask

Is BLOB a byte array?

Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes. This byte array contains up to length consecutive bytes starting at position pos .

How do you convert a float to a byte?

As we know, the size of a float in Java is 32 bit which is similar to an int. So we can use floatToIntBits or floatToRawIntBits functions available in the Float class of Java. And then shift the bits to return a byte array.

Can we convert BLOB to string?

Use SQL override to convert BLOB data type to string.


1 Answers

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(); 
like image 57
Timothy Groote Avatar answered Sep 19 '22 20:09

Timothy Groote