@SuppressWarnings("deprecation") protected Blob writeDataToBlob(ResultSet rs, int column, byte[] data) throws SQLException { Blob blob = rs.getBlob(column); // get blob if (blob == null) { throw new SQLException("Driver's Blob representation is null!"); } if (blob instanceof oracle.sql.BLOB) { // is it an oracle blob? ((oracle.sql.BLOB) blob).putBytes(1, data); ((oracle.sql.BLOB) blob).trim(data.length); return blob; } else { throw new SQLException( "Driver's Blob representation is of an unsupported type: " + blob.getClass().getName()); } } }
@SuppressWarnings("deprecation") protected Blob writeDataToBlob(ResultSet rs, int column, byte[] data) throws SQLException { Blob blob = rs.getBlob(column); // get blob if (blob == null) { throw new SQLException("Driver's Blob representation is null!"); } if (blob instanceof oracle.sql.BLOB) { // is it an oracle blob? ((oracle.sql.BLOB) blob).putBytes(1, data); ((oracle.sql.BLOB) blob).trim(data.length); return blob; } else { throw new SQLException( "Driver's Blob representation is of an unsupported type: " + blob.getClass().getName()); } } }
@SuppressWarnings("deprecation") protected Blob writeDataToBlob(ResultSet rs, int column, byte[] data) throws SQLException { Blob blob = rs.getBlob(column); // get blob if (blob == null) { throw new SQLException("Driver's Blob representation is null!"); } if (blob instanceof oracle.sql.BLOB) { // is it an oracle blob? ((oracle.sql.BLOB) blob).putBytes(1, data); ((oracle.sql.BLOB) blob).trim(data.length); return blob; } else { throw new SQLException( "Driver's Blob representation is of an unsupported type: " + blob.getClass().getName()); } } }
int primaryKeyId = getNextPrimaryKeyId(); PreparedStatement stmt1 = conn.prepareStatement(" insert into docTable values (?, ?, empty_blob()) "); stmt1.setInt(1, primaryKeyId ); stmt1.setString(2, getDocumentTitle()); stmt1.executeUpdate(); PreparedStatement stmt2 = conn.prepareStatement(" select doc from docTable where id = ? for update "); stmt2.setInt(1, primaryKeyId); stmt2.execute(); OracleResultSet rset = (OracleResultSet)stmt2.getResultSet(); if (rset.next()) { BLOB document = rset.getBLOB("doc"); document.trim(0); OutputStream os = document.getBinaryOutputStream(); os.write(getDocumentToBeWrittenToDb()); os.flush; os.close; }
/** * Generates an Output stream that writes to a blob, also truncating the existing blob if required.<p> * * Apparently Oracle requires some non-standard handling here.<p> * * @param res the result set where the blob is located in * @param name the name of the database column where the blob is located * @return an Output stream from a blob * @throws SQLException if something goes wring */ public static OutputStream getOutputStreamFromBlob(ResultSet res, String name) throws SQLException { // TODO: perform blob check only once and store Oracle version in a static private member // TODO: best do this during system startup / db init phase once Blob blob = res.getBlob(name); try { // jdbc standard blob.truncate(0); return blob.setBinaryStream(0L); } catch (SQLException e) { // oracle 9 & 8 (if using the same jdbc driver as provided by oracle9: ojdbc14.jar) ((oracle.sql.BLOB)blob).trim(0); return ((oracle.sql.BLOB)blob).getBinaryOutputStream(); } }