Example usage for java.sql ResultSet getBlob

List of usage examples for java.sql ResultSet getBlob

Introduction

In this page you can find the example usage for java.sql ResultSet getBlob.

Prototype

Blob getBlob(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a Blob object in the Java programming language.

Usage

From source file:org.wso2.carbon.identity.sts.store.dao.DBStsDAO.java

/**
 * This is to get Token from token id/*w w w. j a  v a  2  s .com*/
 *
 * @param tokenId tokenId
 * @return Token
 */
public Token getToken(String tokenId) throws TrustException {
    Connection connection = IdentityDatabaseUtil.getDBConnection();
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    Token token = null;

    String query = DBQueries.GET_TOKEN;
    try {
        prepStmt = connection.prepareStatement(query);
        prepStmt.setString(1, tokenId);
        rs = prepStmt.executeQuery();

        if (rs != null) {
            while (rs.next()) {
                Blob tokenContent = rs.getBlob(STSMgtConstants.TOKEN_CONTENT);
                byte[] tokenContentBytes = tokenContent.getBytes(1, (int) tokenContent.length());
                token = getToken(tokenContentBytes);
            }
        }
    } catch (Exception e) {
        String msg = "Failed to get token";
        throw new TrustException(msg, e);
    } finally {
        IdentityDatabaseUtil.closeAllConnections(connection, rs, prepStmt);
    }
    return token;
}

From source file:com.wabacus.config.database.type.Oracle.java

public byte[] getBlobValue(ResultSet rs, int iindex) throws SQLException {
    BLOB blob = (BLOB) rs.getBlob(iindex);
    if (blob == null)
        return null;
    BufferedInputStream bin = null;
    try {//  w w w  .  j a v  a2s.c  om
        bin = new BufferedInputStream(blob.getBinaryStream());
        return Tools.getBytesArrayFromInputStream(bin);
    } catch (Exception e) {
        log.error("?", e);
        return null;
    } finally {
        if (bin != null) {
            try {
                bin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.wabacus.config.database.type.Oracle.java

public byte[] getBlobValue(ResultSet rs, String column) throws SQLException {
    BLOB blob = (BLOB) rs.getBlob(column);
    if (blob == null)
        return null;
    BufferedInputStream bin = null;
    try {//from   w  ww. j  a  v a2 s.c  om
        bin = new BufferedInputStream(blob.getBinaryStream());
        return Tools.getBytesArrayFromInputStream(bin);
    } catch (Exception e) {
        log.error("?" + column + "", e);
        return null;
    } finally {
        if (bin != null) {
            try {
                bin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:DisplayBlobServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    Blob photo = null;/*from  ww w  .  ja  v a 2  s.c o m*/
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;

    String query = "select photo from MyPictures where  id = '001'";
    ServletOutputStream out = response.getOutputStream();

    try {
        conn = getHSQLConnection();
    } catch (Exception e) {
        response.setContentType("text/html");
        out.println("<html><head><title>Person Photo</title></head>");
        out.println("<body><h1>Database Connection Problem.</h1></body></html>");
        return;
    }

    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery(query);
        if (rs.next()) {
            photo = rs.getBlob(1);
        } else {
            response.setContentType("text/html");
            out.println("<html><head><title>Person Photo</title></head>");
            out.println("<body><h1>No photo found for id= 001 </h1></body></html>");
            return;
        }

        response.setContentType("image/gif");
        InputStream in = photo.getBinaryStream();
        int length = (int) photo.length();

        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        while ((length = in.read(buffer)) != -1) {
            System.out.println("writing " + length + " bytes");
            out.write(buffer, 0, length);
        }

        in.close();
        out.flush();
    } catch (SQLException e) {
        response.setContentType("text/html");
        out.println("<html><head><title>Error: Person Photo</title></head>");
        out.println("<body><h1>Error=" + e.getMessage() + "</h1></body></html>");
        return;
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.xenei.bloomgraph.bloom.sql.DBIO.java

/**
 * Get the page index for the specified page.
 * /*from  www .ja  v a 2s  . co m*/
 * @param idx
 *            the page id to retrieve the index for.
 * @return The requiested PageIndex
 * @throws SQLException
 *             on SQL error
 * @throws IOException
 *             on IO error
 */
public PageIndex getPageIndex(final int idx) throws SQLException, IOException {
    final PreparedStatement stmt = getConnection().prepareStatement(sqlCommands.pageIndexById());
    ResultSet rs = null;
    Blob blob = null;
    try {
        stmt.setInt(1, idx);
        rs = stmt.executeQuery();
        if (!rs.next()) {
            return new PageIndex(idx);
        }
        blob = rs.getBlob(1);
        if (blob == null) {
            return new PageIndex(idx);
        }
        return new SQLPageIndex(blob, idx);
    } finally {
        DBIO.freeQuietly(blob);
        DbUtils.closeQuietly(rs);
        DbUtils.closeQuietly(stmt);
    }
}

From source file:org.apache.kylin.common.persistence.JDBCResourceDAO.java

private InputStream getInputStream(String resPath, ResultSet rs) throws SQLException, IOException {
    if (rs == null) {
        return null;
    }//w  w w . j av  a  2  s . c  o m
    InputStream inputStream = rs.getBlob(META_TABLE_CONTENT) == null ? null
            : rs.getBlob(META_TABLE_CONTENT).getBinaryStream();
    if (inputStream != null) {
        return inputStream;
    } else {
        Path redirectPath = bigCellHDFSPath(resPath);
        return redirectFileSystem.open(redirectPath);
    }
}

From source file:org.castor.jdo.engine.SQLTypeInfos.java

/**
 * Get value from given ResultSet at given index with given SQL type.
 * //from   w w  w .  j ava  2 s  . c  o  m
 * @param rs The ResultSet to get the value from.
 * @param index The index of the value in the ResultSet.
 * @param sqlType The SQL type of the value.
 * @return The value.
 * @throws SQLException If a database access error occurs.
 */
public static Object getValue(final ResultSet rs, final int index, final int sqlType) throws SQLException {
    switch (sqlType) {
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        return rs.getString(index);
    case Types.DECIMAL:
    case Types.NUMERIC:
        return rs.getBigDecimal(index);
    case Types.INTEGER:
        int intVal = rs.getInt(index);
        return (rs.wasNull() ? null : new Integer(intVal));
    case Types.TIME:
        return rs.getTime(index, getCalendar());
    case Types.DATE:
        return rs.getDate(index);
    case Types.TIMESTAMP:
        return rs.getTimestamp(index, getCalendar());
    case Types.FLOAT:
    case Types.DOUBLE:
        double doubleVal = rs.getDouble(index);
        return (rs.wasNull() ? null : new Double(doubleVal));
    case Types.REAL:
        float floatVal = rs.getFloat(index);
        return (rs.wasNull() ? null : new Float(floatVal));
    case Types.SMALLINT:
        short shortVal = rs.getShort(index);
        return (rs.wasNull() ? null : new Short(shortVal));
    case Types.TINYINT:
        byte byteVal = rs.getByte(index);
        return (rs.wasNull() ? null : new Byte(byteVal));
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
    case Types.BINARY:
        return rs.getBytes(index);
    case Types.BLOB:
        Blob blob = rs.getBlob(index);
        return (blob == null ? null : blob.getBinaryStream());
    case Types.CLOB:
        return rs.getClob(index);
    case Types.BIGINT:
        long longVal = rs.getLong(index);
        return (rs.wasNull() ? null : new Long(longVal));
    case Types.BIT:
        boolean boolVal = rs.getBoolean(index);
        return (rs.wasNull() ? null : new Boolean(boolVal));
    default:
        Object value = rs.getObject(index);
        return (rs.wasNull() ? null : value);
    }
}

From source file:org.apache.ambari.server.orm.DBAccessorImpl.java

private void convertUpdateData(ResultSet rs, DBColumnInfo columnNameFrom, int typeFrom,
        DBColumnInfo columnNameTo, int typeTo) throws SQLException {
    if (typeFrom == Types.BLOB && typeTo == Types.CLOB) {
        //BLOB-->CLOB
        Blob data = rs.getBlob(columnNameFrom.getName());
        if (data != null) {
            rs.updateClob(columnNameTo.getName(),
                    new BufferedReader(new InputStreamReader(data.getBinaryStream())));
        }/*from   w ww .j ava2  s  .c  o  m*/
    } else {
        Object data = rs.getObject(columnNameFrom.getName());
        rs.updateObject(columnNameTo.getName(), data);
    }

}

From source file:modelo.ProcesoVertimientosManagers.Visitas.java

public ArchivoExtension getAnexoVisita(int codigoArchivo) throws SQLException {

    SeleccionarAnexoVisita select = new SeleccionarAnexoVisita();
    ResultSet rset = select.getAnexoVisita(codigoArchivo);

    String nombreArchivo = null;// ww w. j av a2  s  .co m
    Blob dataArchivo = null;

    while (rset.next()) {
        nombreArchivo = rset.getString("NOMBRE_ARCHIVO");
        dataArchivo = rset.getBlob("ARCHIVO");
    }

    return new ArchivoExtension(dataArchivo, nombreArchivo);
}

From source file:edu.clemson.cs.nestbed.server.adaptation.sql.TestbedSqlAdapter.java

public Map<Integer, Testbed> readTestbeds() throws AdaptationException {
    Map<Integer, Testbed> testbeds = new HashMap<Integer, Testbed>();
    Connection connection = null;
    Statement statement = null;//from   w w  w  .jav  a  2 s .  c  o m
    ResultSet resultSet = null;

    ProjectAdapter projectAdapter = AdapterFactory.createProjectAdapter(AdapterType.SQL);

    try {
        String query = "SELECT * FROM Testbeds";

        connection = DriverManager.getConnection(CONN_STR);
        statement = connection.createStatement();
        resultSet = statement.executeQuery(query);

        while (resultSet.next()) {
            int id;
            String name;
            String description;
            Blob imageBlob;
            Date timestamp;

            id = resultSet.getInt(Index.ID.index());
            name = resultSet.getString(Index.NAME.index());
            description = resultSet.getString(Index.DESCRIPTION.index());
            imageBlob = resultSet.getBlob(Index.IMAGE.index());
            timestamp = resultSet.getDate(Index.TIMESTAMP.index());

            byte[] image = (imageBlob != null && imageBlob.length() > 0)
                    ? imageBlob.getBytes(0, (int) imageBlob.length())
                    : null;

            Testbed testbed = new Testbed(id, name, description, image, timestamp);
            testbeds.put(id, testbed);
        }
    } catch (SQLException ex) {
        String msg = "SQLException in readTestbeds";
        log.error(msg, ex);
        throw new AdaptationException(msg, ex);
    } finally {
        try {
            resultSet.close();
        } catch (Exception ex) {
        }
        try {
            statement.close();
        } catch (Exception ex) {
        }
        try {
            connection.close();
        } catch (Exception ex) {
        }
    }

    return testbeds;
}