Example usage for java.sql ResultSet getBinaryStream

List of usage examples for java.sql ResultSet getBinaryStream

Introduction

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

Prototype

java.io.InputStream getBinaryStream(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a stream of uninterpreted bytes.

Usage

From source file:org.apache.syncope.core.util.ContentExporter.java

private String getValues(final ResultSet rs, final String columnName, final Integer columnType)
        throws SQLException {

    String res = null;/*from w  w  w .j  av a 2 s .  co m*/

    try {
        switch (columnType) {
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            final InputStream is = rs.getBinaryStream(columnName);
            if (is != null) {
                res = new String(Hex.encode(IOUtils.toByteArray(is)));
            }
            break;

        case Types.BLOB:
            final Blob blob = rs.getBlob(columnName);
            if (blob != null) {
                res = new String(Hex.encode(IOUtils.toByteArray(blob.getBinaryStream())));
            }
            break;

        case Types.BIT:
        case Types.BOOLEAN:
            if (rs.getBoolean(columnName)) {
                res = "1";
            } else {
                res = "0";
            }
            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            final Timestamp timestamp = rs.getTimestamp(columnName);
            if (timestamp != null) {
                res = DataFormat.format(new Date(timestamp.getTime()));
            }
            break;

        default:
            res = rs.getString(columnName);
        }
    } catch (IOException e) {
        LOG.error("Error retrieving hexadecimal string", e);
    }

    return res;
}

From source file:nz.co.gregs.dbvolution.datatypes.DBByteArray.java

private byte[] getFromBinaryStream(ResultSet resultSet, String fullColumnName) throws SQLException {
    byte[] bytes = new byte[] {};
    InputStream inputStream;/*  ww  w. ja v a  2s. com*/
    inputStream = resultSet.getBinaryStream(fullColumnName);
    if (resultSet.wasNull()) {
        inputStream = null;
    }
    if (inputStream == null) {
        this.setToNull();
    } else {
        bytes = getBytesFromInputStream(inputStream);
    }
    return bytes;
}

From source file:com.nabla.wapp.report.server.handler.AddReportHandler.java

@Override
protected int add(AddReport record, IUserSessionContext ctx) throws DispatchException, SQLException {
    int id;/*from  ww  w . ja  v  a 2s.  c om*/
    final Connection conn = ctx.getWriteConnection();
    final PreparedStatement stmt = StatementFormat.prepare(ctx.getReadConnection(),
            "SELECT * FROM import_data WHERE id=?;", record.getFileId());
    try {
        final ResultSet rs = stmt.executeQuery();
        try {
            if (!rs.next())
                throw new ActionException(CommonServerErrors.NO_DATA);
            if (!ctx.getSessionId().equals(rs.getString("userSessionId")))
                throw new ActionException(CommonServerErrors.ACCESS_DENIED);

            final ConnectionTransactionGuard guard = new ConnectionTransactionGuard(conn);
            try {
                if (ReportZipFile.acceptContentType(rs.getString("content_type"))) {
                    final ReportZipFile zip = new ReportZipFile(rs.getBinaryStream("content"));
                    try {
                        final ZipArchiveEntry design = zip.getReportDesign();
                        if (design == null)
                            throw new DispatchException(ReportErrors.ADD_REPORT_NO_REPORT_DESIGN_FOUND);
                        id = reportManager.addReport(conn, design.getName(), zip.getInputStream(design),
                                zip.getInputStream(design));
                        for (Enumeration<ZipArchiveEntry> iter = zip
                                .getEntries(ReportManager.PROPERTIES_FILE_EXTENSION); iter.hasMoreElements();) {
                            final ZipArchiveEntry e = iter.nextElement();
                            reportManager.loadLocaleReportName(conn, id, e.getName(), zip.getInputStream(e));
                        }
                        for (Enumeration<ZipArchiveEntry> iter = zip
                                .getEntries(ReportManager.RESOURCE_FILE_EXTENSIONS); iter.hasMoreElements();) {
                            final ZipArchiveEntry e = iter.nextElement();
                            reportManager.loadReportResource(conn, id, e.getName(), zip.getInputStream(e));
                        }
                    } finally {
                        zip.close();
                    }
                } else {
                    id = reportManager.addReport(conn, FilenameUtils.getBaseName(rs.getString("file_name")),
                            rs.getBinaryStream("content"), rs.getBinaryStream("content"));
                }
                guard.setSuccess();
            } finally {
                guard.close();
            }
        } finally {
            rs.close();
        }
    } finally {
        stmt.close();
    }
    Database.executeUpdate(conn, "DELETE FROM import_data WHERE id=?;", record.getFileId());
    return id;
}

From source file:org.apache.ddlutils.platform.sybase.SybasePlatform.java

/**
 * {@inheritDoc}/*from w w  w. ja v  a2s  . c  om*/
 */
protected Object extractColumnValue(ResultSet resultSet, String columnName, int columnIdx, int jdbcType)
        throws DatabaseOperationException, SQLException {
    boolean useIdx = (columnName == null);

    if ((jdbcType == Types.LONGVARBINARY) || (jdbcType == Types.BLOB)) {
        InputStream stream = useIdx ? resultSet.getBinaryStream(columnIdx)
                : resultSet.getBinaryStream(columnName);

        if (stream == null) {
            return null;
        } else {
            byte[] buf = new byte[65536];
            byte[] result = new byte[0];
            int len;

            try {
                do {
                    len = stream.read(buf);
                    if (len > 0) {
                        byte[] newResult = new byte[result.length + len];

                        System.arraycopy(result, 0, newResult, 0, result.length);
                        System.arraycopy(buf, 0, newResult, result.length, len);
                        result = newResult;
                    }
                } while (len > 0);
                stream.close();
                return result;
            } catch (IOException ex) {
                throw new DatabaseOperationException("Error while extracting the value of column " + columnName
                        + " of type " + TypeMap.getJdbcTypeName(jdbcType) + " from a result set", ex);
            }
        }
    } else {
        return super.extractColumnValue(resultSet, columnName, columnIdx, jdbcType);
    }
}

From source file:org.apache.syncope.core.persistence.jpa.content.XMLContentExporter.java

private String getValues(final ResultSet rs, final String columnName, final Integer columnType)
        throws SQLException {

    String res = null;/*from w ww .j a v a2 s . co  m*/

    try {
        switch (columnType) {
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            final InputStream is = rs.getBinaryStream(columnName);
            if (is != null) {
                res = DatatypeConverter.printHexBinary(IOUtils.toByteArray(is));
            }
            break;

        case Types.BLOB:
            final Blob blob = rs.getBlob(columnName);
            if (blob != null) {
                res = DatatypeConverter.printHexBinary(IOUtils.toByteArray(blob.getBinaryStream()));
            }
            break;

        case Types.BIT:
        case Types.BOOLEAN:
            if (rs.getBoolean(columnName)) {
                res = "1";
            } else {
                res = "0";
            }
            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            final Timestamp timestamp = rs.getTimestamp(columnName);
            if (timestamp != null) {
                res = FormatUtils.format(new Date(timestamp.getTime()));
            }
            break;

        default:
            res = rs.getString(columnName);
        }
    } catch (IOException e) {
        LOG.error("Error retrieving hexadecimal string", e);
    }

    return res;
}

From source file:org.quartz.impl.jdbcjobstore.oracle.OracleDelegate.java

protected Object getObjectFromBlob(ResultSet rs, String colName)
        throws ClassNotFoundException, IOException, SQLException {

    Object obj = null;/*from www  .j ava 2  s  .c o  m*/
    InputStream binaryInput = rs.getBinaryStream(colName);
    if (binaryInput != null) {
        ObjectInputStream in = new ObjectInputStream(binaryInput);
        try {
            obj = in.readObject();
        } finally {
            in.close();
        }
    }

    return obj;
}

From source file:org.quartz.impl.jdbcjobstore.oracle.OracleDelegate.java

protected Object getJobDetailFromBlob(ResultSet rs, String colName)
        throws ClassNotFoundException, IOException, SQLException {

    if (canUseProperties()) {
        InputStream binaryInput = rs.getBinaryStream(colName);
        return binaryInput;
    }//from ww w  . j a va  2  s .co m

    return getObjectFromBlob(rs, colName);
}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static RowMapperFactory getScalarMapper(final Type itemType, final int idx, boolean req) {
    if (itemType == String.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getString(idx);
            }//ww w .  j  av a 2  s. co  m
        };
    }
    if (itemType == Integer.class || itemType == Integer.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getInt(idx);
            }
        };
    }
    if (itemType == URL.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getURL(idx);
            }
        };
    }
    if (itemType == BigInteger.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                final BigDecimal res = rs.getBigDecimal(idx);
                return res == null ? null : res.toBigInteger();
            }
        };
    }
    if (itemType == BigDecimal.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBigDecimal(idx);
            }
        };
    }
    if (itemType == InputStream.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBinaryStream(idx);
            }
        };
    }
    if (itemType == Boolean.class || itemType == Boolean.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBoolean(idx);
            }
        };
    }
    if (itemType == Blob.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBlob(idx);
            }
        };
    }
    if (itemType == java.sql.Date.class || itemType == java.util.Date.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getTimestamp(idx);
            }
        };
    }
    if (itemType instanceof Class) {
        final Class itemClass = (Class) itemType;
        final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass);
        if (columnMapper != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    return columnMapper.map(rs, idx);
                }
            };
        }
        final Converter converter = ConvertUtils.lookup(itemClass);
        if (converter != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(idx);
                    if (s == null) {
                        return null;
                    }
                    return converter.convert(itemClass, s);
                }
            };
        }
        if (Enum.class.isAssignableFrom((Class<?>) itemType)) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(idx);
                    if (s == null) {
                        return null;
                    }
                    //noinspection unchecked
                    return Enum.valueOf((Class<Enum>) itemType, s);
                }
            };
        }
    }
    if (req) {
        throw new IllegalArgumentException("no mapping defined for " + itemType);
    }
    return null;
}

From source file:com.npstrandberg.simplemq.MessageQueueImp.java

private List<Message> peekInternal(int limit) {
    if (limit < 1)
        limit = 1;//  ww  w  . ja va 2 s.c o  m

    List<Message> messages = new ArrayList<Message>(limit);

    try {

        // 'ORDER BY time' depends on that the host computer times is always right.
        // 'ORDER BY id' what happens with the 'id' when we hit Long.MAX_VALUE?
        PreparedStatement ps = conn.prepareStatement(
                "SELECT LIMIT 0 " + limit + " id, object, body FROM message WHERE read=false ORDER BY id");

        // The lock is making sure, that a SELECT and DELETE/UPDATE is only
        // done by one thread at a time.
        lock.lock();

        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            long id = rs.getLong(1);
            InputStream is = rs.getBinaryStream(2);
            String body = rs.getString(3);

            MessageWrapper mw = new MessageWrapper();
            mw.id = id;
            mw.body = body;
            if (is != null)
                mw.object = Utils.deserialize(is);

            messages.add(mw);
        }

        ps.close();
    } catch (SQLException e) {
        logger.error(e);
    } finally {
        lock.unlock();
    }

    return messages;
}

From source file:com.sf.ddao.orm.RSMapperFactoryRegistry.java

public static RowMapperFactory getScalarRowMapperFactory(final Type itemType, final String name, boolean req) {
    if (itemType == String.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getString(name);
            }//from  w  w w  .  j  a v  a 2s  . c  o  m
        };
    }
    if (itemType == Integer.class || itemType == Integer.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getInt(name);
            }
        };
    }
    if (itemType == URL.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getURL(name);
            }
        };
    }
    if (itemType == BigInteger.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                final BigDecimal res = rs.getBigDecimal(name);
                return res == null ? null : res.toBigInteger();
            }
        };
    }
    if (itemType == BigDecimal.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBigDecimal(name);
            }
        };
    }
    if (itemType == InputStream.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBinaryStream(name);
            }
        };
    }
    if (itemType == Boolean.class || itemType == Boolean.TYPE) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBoolean(name);
            }
        };
    }
    if (itemType == Blob.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getBlob(name);
            }
        };
    }
    if (itemType == java.sql.Date.class || itemType == java.util.Date.class) {
        return new ScalarRMF() {
            public Object map(ResultSet rs) throws SQLException {
                return rs.getTimestamp(name);
            }
        };
    }
    if (itemType instanceof Class) {
        final Class itemClass = (Class) itemType;
        final ColumnMapper columnMapper = ColumnMapperRegistry.lookup(itemClass);
        if (columnMapper != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    return columnMapper.map(rs, name);
                }
            };
        }
        final Converter converter = ConvertUtils.lookup(itemClass);
        if (converter != null) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(name);
                    if (s == null) {
                        return null;
                    }
                    return converter.convert(itemClass, s);
                }
            };
        }
        if (Enum.class.isAssignableFrom((Class<?>) itemType)) {
            return new ScalarRMF() {
                public Object map(ResultSet rs) throws SQLException {
                    String s = rs.getString(name);
                    if (s == null) {
                        return null;
                    }
                    //noinspection unchecked
                    return Enum.valueOf((Class<Enum>) itemType, s);
                }
            };
        }
    }
    if (req) {
        throw new IllegalArgumentException("no mapping defined for " + itemType);
    }
    return null;
}