Java Utililty Methods SQL Blob

List of utility methods to do SQL Blob

Description

The list of methods to do SQL Blob are organized into topic(s).

Method

booleanisBlob(Object object)
is Blob
return object instanceof Blob;
byte[]readBlob(Blob blob)
read Blob
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    copy(new BufferedInputStream(blob.getBinaryStream()), baos);
    return baos.toByteArray();
} catch (SQLException e) {
    throw new RuntimeException(e.getMessage(), e);
byte[]readBytes(Blob blob, byte[] defaultValue)
read Bytes
if (blob == null) {
    return defaultValue;
InputStream is = null;
try {
    int length = (int) blob.length();
    if (length == 0) {
        return defaultValue;
...
voidsafeFree(Blob blob)
Safely free a blob.
if (blob != null) {
    try {
        blob.free();
    } catch (Exception ignore) {
voidsafeFree(Blob blob)
safe Free
if (blob != null) {
    try {
        blob.free();
    } catch (Exception ignore) {
    catch (AbstractMethodError ignore) {
byte[]toByteArray(Blob blob)
to Byte Array
try {
    return blob.getBytes(1, (int) blob.length());
} catch (SQLException e) {
    throw new RuntimeException("Error on transforming blob into array.", e);
voidwriteStringToBlob(String value, PreparedStatement preparedStatement, int index)
write String To Blob
try {
    final byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
    preparedStatement.setBinaryStream(index, new ByteArrayInputStream(bytes), bytes.length);
} catch (SQLException e) {
    throw new RuntimeException(e);
voidwriteToBlob(int index, PreparedStatement ps, byte[] data)
write To Blob
ByteArrayInputStream bais = null;
if (data != null) {
    bais = new ByteArrayInputStream(data);
ps.setBinaryStream(index, bais);