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:at.ac.tuwien.dsg.dataassetloader.store.MySqlDataAssetStore.java

public String getDataPartitionRepo(DataPartitionRequest request) {

    String edaas = request.getEdaas();
    String customerID = request.getCustomerID();
    String dawID = request.getDataAssetID();
    String paritionID = request.getPartitionID();
    String daPartitionStr = "";

    //        Configuration cfg = new Configuration();
    //        String dataPath = cfg.getConfig("DATA.REPO.PATH");
    ///*from  w w  w.j  a v a 2  s . c o  m*/
    //        String fileName = "kmeans-" + request.getDataAssetID() + ".data";
    //
    //        at.ac.tuwien.dsg.depic.common.utils.IOUtils iou = new at.ac.tuwien.dsg.depic.common.utils.IOUtils(dataPath);
    //        daPartitionStr = iou.readData(fileName);

    System.out.println("GET DATA PARTITION ... ");

    InputStream inputStream = null;

    MySqlConnectionManager connectionManager = new MySqlConnectionManager(ip, port, db, user, pass);

    String sql = "Select * from ProcessingDataAsset where edaas='" + edaas + "' AND customerID='" + customerID
            + "'  AND partitionID='" + paritionID + "' ";

    System.out.println(sql);

    ResultSet rs = connectionManager.ExecuteQuery(sql);

    try {
        while (rs.next()) {
            inputStream = rs.getBinaryStream("da");
            StringWriter writer = new StringWriter();
            String encoding = StandardCharsets.UTF_8.name();

            IOUtils.copy(inputStream, writer, encoding);
            daPartitionStr = writer.toString();

        }

        rs.close();
    } catch (Exception ex) {

    }
    return daPartitionStr;
}

From source file:at.ac.tuwien.dsg.dataassetloader.store.MySqlDataAssetStore.java

public String getDataAssetXML(String dafName, String partitionID) {

    String dafStr = "";
    try {/*from  w w w . j  av  a 2 s  .c  om*/
        InputStream inputStream = null;

        MySqlConnectionManager connectionManager = new MySqlConnectionManager(ip, port, db, user, pass);

        String sql = "Select * from DataAsset where daw_name='" + dafName + "' and partitionID='" + partitionID
                + "'";

        ResultSet rs = connectionManager.ExecuteQuery(sql);

        try {
            while (rs.next()) {
                inputStream = rs.getBinaryStream("da");
            }

            rs.close();
        } catch (SQLException ex) {

        }

        StringWriter writer = new StringWriter();
        String encoding = StandardCharsets.UTF_8.name();

        IOUtils.copy(inputStream, writer, encoding);
        dafStr = writer.toString();

    } catch (IOException ex) {

    }

    return dafStr;
}

From source file:org.sonar.db.ce.CeTaskDataDao.java

public Optional<DataStream> selectData(DbSession dbSession, String taskUuid) {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    DataStream result = null;/*from   w w  w .j a  v  a  2  s. c  o  m*/
    try {
        stmt = dbSession.getConnection()
                .prepareStatement("SELECT data FROM ce_task_data WHERE task_uuid=? AND data IS NOT NULL");
        stmt.setString(1, taskUuid);
        rs = stmt.executeQuery();
        if (rs.next()) {
            result = new DataStream(stmt, rs, rs.getBinaryStream(1));
            return Optional.of(result);
        }
        return Optional.empty();
    } catch (SQLException e) {
        throw new IllegalStateException("Fail to select data of CE task " + taskUuid, e);
    } finally {
        if (result == null) {
            DatabaseUtils.closeQuietly(rs);
            DatabaseUtils.closeQuietly(stmt);
        }
    }
}

From source file:org.sonar.db.ce.CeTaskInputDao.java

public Optional<DataStream> selectData(DbSession dbSession, String taskUuid) {
    PreparedStatement stmt = null;
    ResultSet rs = null;
    DataStream result = null;//from ww  w .  j av  a  2  s.  c o m
    try {
        stmt = dbSession.getConnection().prepareStatement(
                "SELECT input_data FROM ce_task_input WHERE task_uuid=? AND input_data IS NOT NULL");
        stmt.setString(1, taskUuid);
        rs = stmt.executeQuery();
        if (rs.next()) {
            result = new DataStream(stmt, rs, rs.getBinaryStream(1));
            return Optional.of(result);
        }
        return Optional.empty();
    } catch (SQLException e) {
        throw new IllegalStateException("Fail to select data of CE task " + taskUuid, e);
    } finally {
        if (result == null) {
            DatabaseUtils.closeQuietly(rs);
            DatabaseUtils.closeQuietly(stmt);
        }
    }
}

From source file:org.mayocat.attachment.store.jdbi.mapper.LoadedAttachmentMapper.java

@Override
public LoadedAttachment map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException {
    LoadedAttachment attachment = new LoadedAttachment();
    attachment.setId((UUID) resultSet.getObject("id"));
    attachment.setTitle(resultSet.getString("title"));
    attachment.setDescription(resultSet.getString("description"));
    attachment.setSlug(resultSet.getString("slug"));
    attachment.setData(new AttachmentData(resultSet.getBinaryStream("data")));
    attachment.setExtension(resultSet.getString("extension"));
    attachment.setParentId((UUID) resultSet.getObject("parent_id"));

    ObjectMapper mapper = new ObjectMapper();

    if (!Strings.isNullOrEmpty(resultSet.getString("metadata"))) {
        try {//from  w w w .j  a v a 2  s  .  co  m

            Map<String, Map<String, Object>> metadata = mapper.readValue(resultSet.getString("metadata"),
                    new TypeReference<Map<String, Map<String, Object>>>() {
                    });
            attachment.setMetadata(metadata);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    if (MapperUtils.hasColumn("localization_data", resultSet)
            && !Strings.isNullOrEmpty(resultSet.getString("localization_data"))) {
        try {
            Map<Locale, Map<String, Object>> localizedVersions = Maps.newHashMap();
            Map[] data = mapper.readValue(resultSet.getString("localization_data"), Map[].class);
            for (Map map : data) {
                localizedVersions.put(LocaleUtils.toLocale((String) map.get("locale")),
                        (Map) map.get("entity"));
            }
            attachment.setLocalizedVersions(localizedVersions);
        } catch (IOException e) {
            throw new SQLException("Failed to de-serialize localization JSON data", e);
        }
    }

    return attachment;
}

From source file:no.sintef.jarfter.PostgresqlInteractor.java

public InputStream getFileAsStream_havahol(String transform_uri) throws SQLException {
    checkConnection();/*  ww  w  .  ja  v a 2s .  c  om*/

    PreparedStatement pst = conn.prepareStatement("SELECT * FROM havahol WHERE h_id = ?");
    pst.setString(1, transform_uri);
    ResultSet rs = pst.executeQuery();
    while (rs.next()) {
        InputStream jarStream = rs.getBinaryStream("file");
        return jarStream;
    }
    throw new SQLException("Found no stream...");
}

From source file:de.whs.poodle.repositories.ImageRepository.java

public void writeImageToHttpResponse(int imageId, HttpServletResponse response) {
    jdbc.query("SELECT mimetype,data FROM uploaded_image WHERE id = ?", new Object[] { imageId },

            // use ResultSetExtractor so we can check whether a row even existed (NotFoundException)
            new ResultSetExtractor<Void>() {

                @Override/*  w  ww  .j  a va2 s.  c  o m*/
                public Void extractData(ResultSet rs) throws SQLException {
                    if (!rs.next()) // image doesn't exist
                        throw new NotFoundException();

                    String mimeType = rs.getString("mimetype");
                    response.setContentType(mimeType);

                    // write input stream from DB into http response
                    try (InputStream in = rs.getBinaryStream("data");
                            OutputStream out = response.getOutputStream();) {
                        StreamUtils.copy(in, out);
                        response.flushBuffer();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }

                    return null;
                }

            });
}

From source file:at.ac.tuwien.dsg.orchestrator.elasticityprocessesstore.ElasticityProcessesStore.java

public ElasticDataAsset getElasticDataAsset(String edaasName) {

    String elasticityProcessesXML = "";
    String elasticStateSetXML = "";
    DBType eDaaSType = null;//from   w ww. j a  v  a2s. c o  m
    try {

        InputStream elProcessStream = null;
        InputStream eStateSetStream = null;
        InputStream typeStream = null;

        //            String sql = "SELECT * FROM ElasticDaaS, DataAssetFunction "
        //                    + "WHERE ElasticDaaS.name = DataAssetFunction.edaas "
        //                    + "AND DataAssetFunction.dataAssetID='"+dataAssetID+"'";

        String sql = "SELECT * FROM ElasticDaaS " + "WHERE ElasticDaaS.name='" + edaasName + "' ";

        ResultSet rs = connectionManager.ExecuteQuery(sql);

        try {
            while (rs.next()) {
                elProcessStream = rs.getBinaryStream("elasticity_processes");
                eStateSetStream = rs.getBinaryStream("elasticStateSet");
                typeStream = rs.getBinaryStream("type");
            }
        } catch (SQLException ex) {
            Logger.getLogger(ElasticityProcessesStore.class.getName()).log(Level.SEVERE, null, ex);
        }

        StringWriter writer1 = new StringWriter();
        String encoding = StandardCharsets.UTF_8.name();
        IOUtils.copy(elProcessStream, writer1, encoding);
        elasticityProcessesXML = writer1.toString();

        StringWriter writer2 = new StringWriter();
        IOUtils.copy(eStateSetStream, writer2, encoding);
        elasticStateSetXML = writer2.toString();

        StringWriter writer3 = new StringWriter();
        IOUtils.copy(typeStream, writer3, encoding);
        String typeStr = writer3.toString();
        eDaaSType = DBType.valueOf(typeStr);

    } catch (IOException ex) {
        Logger.getLogger(ElasticityProcessesStore.class.getName()).log(Level.SEVERE, null, ex);
    }

    DataElasticityManagementProcess elasticityProcess = null;
    try {
        elasticityProcess = JAXBUtils.unmarshal(elasticityProcessesXML, DataElasticityManagementProcess.class);
    } catch (JAXBException ex) {
        Logger.getLogger(ElasticityProcessesStore.class.getName()).log(Level.SEVERE, null, ex);
    }

    ElasticStateSet elasticStateSet = null;

    try {
        elasticStateSet = JAXBUtils.unmarshal(elasticStateSetXML, ElasticStateSet.class);
    } catch (JAXBException ex) {
        Logger.getLogger(ElasticityProcessesStore.class.getName()).log(Level.SEVERE, null, ex);
    }

    ElasticDataAsset eda = new ElasticDataAsset(edaasName, elasticityProcess,
            elasticStateSet.getFinalElasticStateSet());

    return eda;

}

From source file:no.sintef.jarfter.PostgresqlInteractor.java

public InputStream getTransformationJar(String transform_uri) throws JarfterException {
    checkConnection();//from  ww w.  jav a2  s.  c  om

    try {
        PreparedStatement pst = conn.prepareStatement("SELECT * FROM transformations WHERE uri = ?");
        pst.setString(1, transform_uri);
        ResultSet rs = pst.executeQuery();
        while (rs.next()) {
            InputStream jarStream = rs.getBinaryStream("executable");
            return jarStream;
        }
    } catch (SQLException sqle) {
        error(sqle);
        throw new JarfterException(JarfterException.Error.SQL_UNKNOWN_ERROR, sqle.getMessage());
    }
    throw new JarfterException(JarfterException.Error.SQL_NO_DATABASE_ENTRY,
            "No transformations entry with uri " + transform_uri);
}

From source file:no.sintef.jarfter.PostgresqlInteractor.java

public InputStream getFilesFile(String fileid) throws JarfterException {
    checkConnection();//from  ww  w  .j a v  a  2 s . c o  m

    try {
        PreparedStatement st = conn.prepareStatement("SELECT file FROM files WHERE fileid = ?");
        st.setString(1, fileid);
        ResultSet rs = st.executeQuery();
        while (rs.next()) {
            InputStream fileStream = rs.getBinaryStream("file");
            return fileStream;
        }
        throw new JarfterException(JarfterException.Error.SQL_NO_DATABASE_ENTRY);
    } catch (SQLException sqle) {
        log("getFileFiles - got SQLException...");
        error(sqle);
        throw new JarfterException(JarfterException.Error.SQL_UNKNOWN_ERROR, sqle.getLocalizedMessage());
    }
}