1

In our product,when we run a project (Say 'ABC') that inserts blob data into the database(can be MySQL/Oracle/MS-SQL) , but when we execute a select query and retrieve a blob data from database to convert it to original form using java (Here we are writing this output to a file) then the following block of code fails for MySQL but works perfectly fine for MS-SQL.Can we build a code snippet in generic way ?

int length = blobData.length();  
    byte[] data = new byte[length / 2];

for (int i = 0; i < length ; i += 2)
data[i / 2] = (byte) ((Character.digit(blobData.charAt(i), 16) << 4) + Character.digit(blobData.charAt(i + 1), 16));//Note : blobData is of String type
FullStack
  • 665
  • 11
  • 26
  • http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php This may help you :) – Shivam Oct 27 '15 at 11:40
  • @Shivam It is good but we can not use it in that way. – FullStack Oct 27 '15 at 11:49
  • I do not think you can have generic solution. For example BLOB in Oracle is BinaryStream (or CharStream for CLOB). LOBs can be HUGE and converting them into arrays in not safe. – ibre5041 Oct 27 '15 at 11:52
  • @ibre5041 What do you suggest instead of arrays ? – FullStack Oct 27 '15 at 11:56
  • Get the BLOB through [`ResultSet.getBlob()`](http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getBlob%28int%29). Then, convert it to a `String` with `String blobAsString = new String(blob.getBytes(0, blob.length()), Charset.forName("ENCODING_OF_DATA"))`, where `ENCODING_OF_DATA` is the charset encoding used for the character data. – Mick Mnemonic Oct 27 '15 at 13:11
  • @SudeepMasare use `ResultSet.getBlob().getBinaryStream()`. And then read BLOB data piece by piece. For String data use CLOBs instead of BLOBs. The max size of BLOB is 176TB, you probably do not have so much RAM for your JVM heap. – ibre5041 Oct 27 '15 at 14:37

0 Answers0