@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()); } } }
rset.next(); Blob bobj = rset.getBlob(1); BLOB object = (BLOB) bobj; int chunkSize = object.getChunkSize(); byte[] binaryBuffer = new byte[chunkSize]; int position = 1; int bytesRead = 0; int bytesWritten = 0, totbytesRead = 0, totbytesWritten = 0; InputStream is = fileItem.getInputStream(); while ((bytesRead = is.read(binaryBuffer)) != -1) { bytesWritten = object.putBytes(position, binaryBuffer, bytesRead); position += bytesRead; totbytesRead += bytesRead; totbytesWritten += bytesWritten; is.close();
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; }
/** * INTERNAL: * Write LOB value - only on Oracle8 and up */ @SuppressWarnings("deprecation") public void writeLOB(DatabaseField field, Object value, ResultSet resultSet, AbstractSession session) throws SQLException { if (isBlob(field.getType())) { //change for 338585 to use getName instead of getNameDelimited oracle.sql.BLOB blob = (oracle.sql.BLOB)resultSet.getObject(field.getName()); //we could use the jdk 1.4 java.nio package and use channel/buffer for the writing //for the time being, simply use Oracle api. blob.putBytes(1, (byte[])value); //impose the locallization session.log(SessionLog.FINEST, SessionLog.SQL, "write_BLOB", Long.valueOf(blob.length()), field.getName()); } else if (isClob(field.getType())) { //change for 338585 to use getName instead of getNameDelimited oracle.sql.CLOB clob = (oracle.sql.CLOB)resultSet.getObject(field.getName()); //we could use the jdk 1.4 java.nio package and use channel/buffer for the writing //for the time being, simply use Oracle api. clob.putString(1, (String)value); //impose the locallization session.log(SessionLog.FINEST, SessionLog.SQL, "write_CLOB", Long.valueOf(clob.length()), field.getName()); } else { //do nothing for now, open to BFILE or NCLOB types } }
outstream = blob.getBinaryOutputStream(); int bufferSize = blob.getChunkSize(); byte[] buffer = new byte[bufferSize];
if (blob != null) blob.putBytes(1, bytes);
/** * Returns the bytes from a result set * * @param res * The ResultSet to read from * @param columnName * The name of the column to read from * * @return The byte value from the column */ public byte[] getBytesFromResultset(ResultSet res, String columnName) throws SQLException { // read the bytes from an oracle blob oracle.sql.BLOB blob = ((OracleResultSet) res).getBLOB(columnName); byte[] content = new byte[(int) blob.length()]; content = blob.getBytes(1, (int) blob.length()); return content; }
public byte[] get() { if (!isFormField) return blob.getBytes(); else return bos.toByteArray(); }
/** * 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(); } }
outstream = blob.getBinaryOutputStream(); instream = new java.io.BufferedInputStream(new java.io.FileInputStream(file)); int bufferSize = blob.getChunkSize(); byte[] buffer = new byte[bufferSize];
blob.putBytes(1, bytes); // Deprecated but what can you do
public String getString(String charset) { String value; try { if (!isFormField) value= new String(blob.getBytes(),charset); else value= new String(bos.toByteArray(), charset); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } logger.debug(fieldName+": "+value); return value; }
@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()); } } }
outstream = blob.getBinaryOutputStream(); int bufferSize = blob.getChunkSize(); byte[] buffer = new byte[bufferSize];
@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()); } } }
outstream = blob.getBinaryOutputStream(); int bufferSize = blob.getChunkSize(); byte[] buffer = new byte[bufferSize];