Easiest way to convert a Blob into a byte array

JavaMysqlBytearrayBlob

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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();

Solution 2 - Java

The easiest way is this.

byte[] bytes = resultSet.getBytes("my_field");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandroidGuyView Question on Stackoverflow
Solution 1 - JavaTimothy GrooteView Answer on Stackoverflow
Solution 2 - JavaLukas EderView Answer on Stackoverflow