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

泄露秘密 提交于 2021-02-05 20:20:57

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/760781/getbytes-vs-getbinarystream-vs-getblob-for-getting-data-out-of-a-blob-column

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!