Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getBytes vs getBinaryStream vs getBlob for getting data out of a BLOB column

Tags:

There are 3 different ways to get data out of a BLOB column from a ResultSet

  • getBytes
  • getBinaryStream
  • getBlob

Also, the Blob object returned by getBlob also has getBytes and getBinaryStream methods available on it.

Are there any particular reasons (performance, memory, database specific problems) that I should pick one over the other?

The Blob object also has a free() call that has been introduced since JDBC 4.0. Does that make a difference?

like image 932
Kapsh Avatar asked Apr 17 '09 15:04

Kapsh


People also ask

What is BLOB data Type 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.

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 .

What is BLOB SQL?

An SQL BLOB is a built-in type that stores a Binary Large Object as a column value in a row of a database table. By default drivers implement Blob using an SQL locator(BLOB) , which means that a Blob object contains a logical pointer to the SQL BLOB data rather than the data itself.

How do you convert bytes to blobs?

At first, I have created an object of Scanner class and read my file using FileReader() method. Then created a BLOB object initialized with null. Next, I have Converted the file into a Byte array ( byte[] ), stored it into my_byte_array. Finally converted the byte array into BLOB using SerialBlob() method.


2 Answers

If you're going to be pulling a lot of data (i.e. enough data to cause memory problems), then getBinaryStream will give you most flexibility to process and discard the data as you read it in.

On the other hand, this could be quite slow, depending on your JDBC driver, since each read from the stream could entail a lot of network chatter with the database. If you call getBytes, then the driver knows to fetch the whole lot in one go, which is likely to be more efficient.

getBlob() returns a "pointer" to the data, which you can manipulate using the methods on the Blob interface. If you need to modify or otherwise get fancy with the data in-situ, then this might be best for you.

like image 130
skaffman Avatar answered Sep 20 '22 18:09

skaffman


Generally you want to pick the stream-based methods (i.e. getBlob().getBinaryStream() or getBinaryStream()) rather than the byte-array method.

  1. Performance. The driver has a chance to incrementally pull bytes from the database.
  2. Memory. You don't have to load all bytes at once, and in one contiguous block.

Worst-case is the database (or JDBC driver) doesn't truly support streaming binary data, but then there's still no appreciable penalty for using the streaming methods.

like image 37
Jason Cohen Avatar answered Sep 21 '22 18:09

Jason Cohen