Here you can find the source of readBlob(ResultSet rs, int index)
Parameter | Description |
---|---|
rs | the result set |
index | the index of the column containing the blob |
Parameter | Description |
---|---|
SQLException | if a database error occurs |
public static byte[] readBlob(ResultSet rs, int index) throws SQLException
//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 } } } }