InterviewSolution
| 1. |
How Do I Extract A Blob From A Database? |
|
Answer» A BLOB (Binary Large OBject) is essentially an array of bytes (byte[]), stored in the database. You EXTRACT the data in two steps:
Note that a Blob is essentially a pointer to a byte array (called LOCATOR in database-talk), so the java.sql.Blob object essentially wraps a byte pointer. THUS, you must extract all data from the database blob before calling commit or <div align="center">private void runGetBLOB() { try { // Prepare a Statement: PreparedStatement stmnt = conn.prepareStatement("select aBlob from BlobTable"); // Execute ResultSet rs = stmnt.executeQuery(); while(rs.next()) { try { // Get as a BLOB Blob aBlob = rs.getBlob(1); byte[] allBytesInBlob = aBlob.getBytes(1, (int) aBlob.length()); } catch(Exception EX) { // The driver could not HANDLE this as a BLOB... // Fallback to default (and slower) byte[] handling byte[] bytes = rs.getBytes(1); } } // Close resources rs.close(); stmnt.close(); } catch(Exception ex) { this.log("ERROR when trying to read BLOB: " + ex); } }</div> A BLOB (Binary Large OBject) is essentially an array of bytes (byte[]), stored in the database. You extract the data in two steps: Note that a Blob is essentially a pointer to a byte array (called LOCATOR in database-talk), so the java.sql.Blob object essentially wraps a byte pointer. Thus, you must extract all data from the database blob before calling commit or |
|