Search in sources :

Example 1 with BLOB

use of oracle.sql.BLOB in project jforum2 by rafaelsteil.

the class OracleUtils method writeBlobUTF16BinaryStream.

/**
	 * The query should look like:
	 * 
	 * 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 String
	 * @param idForQuery int
	 * @param value String
	 * @throws SQLException
	 */
public static void writeBlobUTF16BinaryStream(String query, int idForQuery, String value) throws Exception {
    PreparedStatement p = null;
    ResultSet rs = null;
    OutputStream blobWriter = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(query);
        p.setInt(1, idForQuery);
        rs = p.executeQuery();
        rs.next();
        Blob text = rs.getBlob(1);
        if (text instanceof BLOB) {
            blobWriter = ((BLOB) text).getBinaryOutputStream();
        } else {
            blobWriter = text.setBinaryStream(0);
        }
        blobWriter.write(value.getBytes("UTF-16"));
        blobWriter.close();
    } catch (IOException e) {
        throw new DatabaseException(e);
    } finally {
        if (blobWriter != null) {
            blobWriter.close();
        }
        DbUtils.close(rs, p);
    }
}
Also used : Blob(java.sql.Blob) BLOB(oracle.sql.BLOB) OutputStream(java.io.OutputStream) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) DatabaseException(net.jforum.exceptions.DatabaseException)

Aggregations

IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Blob (java.sql.Blob)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 DatabaseException (net.jforum.exceptions.DatabaseException)1 BLOB (oracle.sql.BLOB)1