Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manipulate a BLOB in Java?

Tags:

java

blob

I have a DB procedure which returns a BLOB. Can anyone tell me how to manipulate the BLOB? Is there any specific API for this?

like image 931
Parth Trivedi Avatar asked Oct 17 '11 07:10

Parth Trivedi


People also ask

How do you represent a BLOB in Java?

To represent a BLOB data type, we use K, M, and G characters that represent the multiples of 1024, 1024*1024, 1024*1024*1024, respectively. The lifespan of a BLOB ends when a transaction commits. We should use the BLOB data type if we want to store very large binary values.

What is the datatype for BLOB in Java?

A BLOB (binary large object) is a varying-length binary string that can be up to 2,147,483,647 characters long. Like other binary types, BLOB strings are not associated with a code page. In addition, BLOB strings do not hold character data.

What is Longblob in Java?

A longblob is just an array of bytes up to a length of 2^32 - 1.


1 Answers

Is there any specific API for this ?

Sure, the JDBC API.

  • Trail: JDBC™ Database Access

You get hold of the Blob instance just as you get hold of any value from a result set. You should then use the get...- and set... methods on this Blob.

Here you basically have two options:

  • Work with a byte-array:

    1. Get hold of a byte[] containing the data through Blob.getBytes
    2. Manipulate this byte-array
    3. Set it back using Blob.setBytes.


  • Work with InputStream / OutputStream:

    1. Get hold a an InputStream through Blob.getBinaryStream
    2. Manipulate this stream as you see fit
    3. Use Blob.setBinaryStream.

An alternative approach is to skip messing with Blob in the first place, and instead use the second approach (with streams) directly through the ResultSet-interface.

like image 199
aioobe Avatar answered Sep 27 '22 23:09

aioobe