Example usage for java.sql Blob truncate

List of usage examples for java.sql Blob truncate

Introduction

In this page you can find the example usage for java.sql Blob truncate.

Prototype

void truncate(long len) throws SQLException;

Source Link

Document

Truncates the BLOB value that this Blob object represents to be len bytes in length.

Usage

From source file:com.runwaysdk.dataaccess.database.general.Oracle.java

/**
 * Truncates a blob by the specified length.
 *
 * @param table//  w ww  .j  av a 2  s  .  c  o m
 * @param columnName
 * @param id
 * @param length
 */
public void truncateBlob(String table, String columnName, String id, long length, Connection conn) {
    PreparedStatement prepared = null;
    Statement statement = null;
    ResultSet resultSet = null;
    try {
        // get the blob
        statement = conn.createStatement();
        String select = "SELECT " + columnName + " FROM " + table + " WHERE " + EntityDAOIF.ID_COLUMN + " = '"
                + id + "' FOR UPDATE";
        String update = "UPDATE " + table + " SET " + columnName + " = " + "? WHERE " + EntityDAOIF.ID_COLUMN
                + " = '" + id + "'";
        resultSet = statement.executeQuery(select);
        boolean resultSetFound = resultSet.next();
        if (!resultSetFound) {
            return;
        }

        Blob blob = resultSet.getBlob(columnName);

        // null check
        if (blob != null) {
            blob.truncate(length);

            // modify the blob
            if (conn.getMetaData().locatorsUpdateCopy()) {
                // The current database needs to be manually updated (it doesn't
                // support auto blob updates)
                prepared = conn.prepareStatement(update);
                prepared.setBlob(1, blob);
                prepared.executeUpdate();
            }
        }
    } catch (SQLException e) {
        this.throwDatabaseException(e);
    } finally {
        try {
            if (resultSet != null)
                resultSet.close();
            if (statement != null)
                statement.close();
            if (prepared != null)
                prepared.close();
        } catch (SQLException e) {
            this.throwDatabaseException(e);
        }
    }
}

From source file:org.etudes.jforum.dao.oracle.OracleUtils.java

/**
 * The query should look like://from   w ww.  j  a  v  a  2  s  .c  o  m
 * 
 * SELECT blob_field from any_table WHERE id = ? FOR UPDATE
 * 
 * BUT KEEP IN MIND:
 * 
 * When you insert record in previous step, it should go with empty_blob() like:
 * 
 * INSERT INTO jforum_posts_text ( post_text ) VALUES (EMPTY_BLOB())
 * 
 * @param query
 * @param idForQuery
 * @param value
 * @throws IOException
 * @throws SQLException
 */
public static void writeBlobUTF16BinaryStream(String query, int idForQuery, String value)
        throws IOException, SQLException {
    PreparedStatement p = JForum.getConnection().prepareStatement(query);
    p.setInt(1, idForQuery);

    ResultSet rs = p.executeQuery();
    rs.next();
    Blob postText = rs.getBlob(1);

    //wipe out the Blob contents 
    postText.truncate(0);

    if (logger.isDebugEnabled())
        logger.debug("post test is a " + postText.getClass().getName());

    // OutputStream blobWriter = ((oracle.sql.BLOB)postText).setBinaryStream(0L);
    OutputStream blobWriter = postText.setBinaryStream(0L);
    blobWriter.write(value.getBytes("UTF-16"));

    blobWriter.flush();
    blobWriter.close();
    rs.close();
    p.close();
}

From source file:org.opencms.db.oracle.CmsUserDriver.java

/**
 * Generates an Output stream that writes to a blob, also truncating the existing blob if required.<p>
 * /*from ww  w.j  a  v  a 2 s .c  o m*/
 * 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
 */
@SuppressWarnings("deprecation")
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();
    }
}

From source file:org.openehealth.ipf.commons.flow.hibernate.HibernateUtils.java

/**
 * Writes a byte array to a {@link Blob}.
 * //from w  w w. ja va2 s  . c om
 * @param bytes
 *            byte array.
 * @param blob
 *            an initialized {@link Blob}.
 * @return the {@link Blob} instance passed as <code>blob</code> argument.
 * @throws SQLException
 */
public static Blob writeToBlob(byte[] bytes, Blob blob) throws SQLException {
    if (/*blob != null*/ false) { // not supported
        blob.setBytes(0, bytes);
        blob.truncate(bytes.length);
    } else {
        blob = Hibernate.createBlob(bytes);
    }
    return blob;
}