Java SQL ResultSet Blob Read getBlobAsByteArray(final ResultSet rs, final int ind)

Here you can find the source of getBlobAsByteArray(final ResultSet rs, final int ind)

Description

get Blob As Byte Array

License

Open Source License

Declaration

@Nullable
    private static byte[] getBlobAsByteArray(final ResultSet rs, final int ind) throws SQLException 

Method Source Code

//package com.java2s;
/*//from   ww  w  . ja va  2 s . c o m
 * $Id: SqlJdbcUtil.java,v 1.2 2006/03/20 05:10:13 detkin Exp $
 *
 * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

import javax.annotation.Nullable;

import java.sql.Blob;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    @Nullable
    private static byte[] getBlobAsByteArray(final ResultSet rs, final int ind) throws SQLException {
        final Blob blob = rs.getBlob(ind);
        if (blob == null) {
            return null;
        }

        final long len = blob.length();
        if (len <= 0L) {
            return null;
        }

        if (blob.length() > Integer.MAX_VALUE) {
            throw new SQLException("BLOB exceeds Integer.MAX_VALUE in length; cannot be retrieved as byte array");
        }

        // Yes, the starting position really is 1L.  Because JDBC.  *sigh*
        return blob.getBytes(1L, (int) len);
    }
}

Related

  1. getBlob(ResultSet results, int parameterIndex)
  2. getBlobAsBytes(ResultSet rs, int col)
  3. getBlobBytes(ResultSet rs, int iColumn)
  4. getBlobValue(ResultSet result, String strField)
  5. readBlob(ResultSet rs, int index)