Example usage for java.sql Blob setBytes

List of usage examples for java.sql Blob setBytes

Introduction

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

Prototype

int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException;

Source Link

Document

Writes all or part of the given byte array to the BLOB value that this Blob object represents and returns the number of bytes written.

Usage

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

/**
 * Sets the value of this blob as the specified bytes. This method works the
 * same as the Blob.setBytes(long pos, byte[], int offset, int length) as
 * specified in the JDBC 3.0 API. Because of this, the first element in the
 * bytes to write to is actually element 1 (as opposed to the standard array
 * treatment where the first element is at position 0).
 *
 * @param table//  w  w w  .j  av  a2 s.  co  m
 * @param columnName
 * @param id
 * @param pos
 * @param bytes
 * @param offset
 * @param length
 * @return
 */
public int setBlobAsBytes(String table, String columnName, String id, long pos, byte[] bytes, int offset,
        int length) {
    Connection conn = Database.getConnection();
    PreparedStatement prepared = null;
    Statement statement = null;
    ResultSet resultSet = null;
    int written = 0;
    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);
        resultSet.next();
        Blob blob = resultSet.getBlob(columnName);

        // null check
        if (blob == null) {
            // because this method is used to place byte in specific positions, it
            // wouldn't
            // make sense to insert the bytes into a null field as it defeats the
            // purpose of
            // this method. Just return a write count of 0 and don't do anything
            // else.
            return written;
        } else {
            // modify the blob
            written = blob.setBytes(pos, bytes, offset, length);
            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();
            this.closeConnection(conn);
        } catch (SQLException e) {
            this.throwDatabaseException(e);
        }
    }
    return written;
}