Java SQL ResultSet Blob Read readBlob(ResultSet rs, int index)

Here you can find the source of readBlob(ResultSet rs, int index)

Description

Read the blob associated with the column with the specified index from the specified result set.

License

Apache License

Parameter

Parameter Description
rs the result set
index the index of the column containing the blob

Exception

Parameter Description
SQLException if a database error occurs

Return

the binary data for the BLOB

Declaration

public static byte[] readBlob(ResultSet rs, int index) throws SQLException 

Method Source Code


//package com.java2s;
/*/*  w ww  .  j a va  2s.c  om*/
 * Copyright 2017 Marcus Portmann
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.*;
import java.sql.*;

public class Main {
    /**
     * Read the blob associated with the column with the specified index from the
     * specified result set.
     *
     * @param rs    the result set
     * @param index the index of the column containing the blob
     *
     * @return the binary data for the BLOB
     *
     * @throws SQLException if a database error occurs
     */
    public static byte[] readBlob(ResultSet rs, int index) throws SQLException {
        ByteArrayOutputStream bos = null;
        BufferedInputStream in = null;

        try {
            bos = new ByteArrayOutputStream();
            in = new BufferedInputStream(rs.getBinaryStream(index));

            int noBytes;
            byte[] tmpBuffer = new byte[1024];

            while ((noBytes = in.read(tmpBuffer)) != -1) {
                bos.write(tmpBuffer, 0, noBytes);
            }

            return bos.toByteArray();
        } catch (IOException e) {
            throw new SQLException(
                    "An IO error occurred while reading the BLOB from the database: " + e.getMessage());
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    // Do nothing
                }
            }

            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // Do nothing
                }
            }
        }
    }

    /**
     * Close the connection.
     *
     * @param connection the connection to close
     */
    public static void close(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                // Do nothing
            }
        }
    }

    /**
     * Close the result set.
     *
     * @param rs the result set to close
     */
    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                // Do nothing
            }
        }
    }

    /**
     * Close the statement.
     *
     * @param statement the statement to close
     */
    public static void close(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // Do nothing
            }
        }
    }
}

Related

  1. getBlob(ResultSet results, int parameterIndex)
  2. getBlobAsByteArray(final ResultSet rs, final int ind)
  3. getBlobAsBytes(ResultSet rs, int col)
  4. getBlobBytes(ResultSet rs, int iColumn)
  5. getBlobValue(ResultSet result, String strField)
  6. readBlobUTF16BinaryStream(ResultSet rs, String fieldName)
  7. readBlobUTF16BinaryStream(ResultSet rs, String fieldName)
  8. readFromBlob(ResultSet rs, String column)