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:Main.java

public static void main(String[] argv) throws Exception {
    ResultSet rset = null;
    InputStream stream = rset.getBinaryStream(1);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int a1 = stream.read();
    while (a1 >= 0) {
        output.write((char) a1);
        a1 = stream.read();// www  . j  a v  a  2  s  . c  o m
    }
    Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
    output.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table images (Id int, b BLOB);");

    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1, "10");
    ps.setBinaryStream(2, fis);//from w  ww.  j  a  v  a2  s .  co  m
    ps.executeUpdate();

    ResultSet rset = st.executeQuery("select b from images");
    InputStream stream = rset.getBinaryStream(1);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int a1 = stream.read();
    while (a1 >= 0) {
        output.write((char) a1);
        a1 = stream.read();
    }
    Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
    output.close();

    ps.close();

    fis.close();
    st.close();
    conn.close();
}

From source file:com.javacreed.examples.sql.Example3.java

public static void main(final String[] args) throws Exception {
    try (BasicDataSource dataSource = DatabaseUtils.createDataSource();
            Connection connection = dataSource.getConnection()) {
        final ExampleTest test = new ExampleTest(connection, "compressed_table", "compressed") {
            @Override/* www.  ja v  a 2s .  co  m*/
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (InputStream in = new LZ4BlockInputStream(resultSet.getBinaryStream("compressed"))) {
                    return IOUtils.toString(in, "UTF-8");
                }
            }

            @Override
            protected void setPreparedStatement(final String data, final PreparedStatement statement)
                    throws Exception {
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length());
                try (OutputStream out = new LZ4BlockOutputStream(baos)) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }
        };
        test.runTest();
    }
    Example3.LOGGER.debug("Done");
}

From source file:com.javacreed.examples.sql.Example2.java

public static void main(final String[] args) throws Exception {
    try (BasicDataSource dataSource = DatabaseUtils.createDataSource();
            Connection connection = dataSource.getConnection()) {
        final ExampleTest test = new ExampleTest(connection, "compressed_table", "compressed") {
            @Override//from  w  w  w. ja va  2  s  .c o m
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (GZIPInputStream in = new GZIPInputStream(resultSet.getBinaryStream("compressed"))) {
                    return IOUtils.toString(in, "UTF-8");
                }
            }

            @Override
            protected void setPreparedStatement(final String data, final PreparedStatement statement)
                    throws Exception {
                // Compress the data before inserting it. We need to compress before inserting the data to make this process
                // as realistic as possible.
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length());
                try (OutputStream out = new GZIPOutputStream(baos, data.length())) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }
        };
        test.runTest();
    }
    Example2.LOGGER.debug("Done");
}

From source file:com.javacreed.examples.sql.Example4.java

public static void main(final String[] args) throws Exception {
    try (BasicDataSource dataSource = DatabaseUtils.createDataSource();
            Connection connection = dataSource.getConnection()) {
        final ExampleTest test = new ExampleTest(connection, "compressed_table", "compressed") {

            @Override//from   w ww  . jav a  2  s.c  o m
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (InputStream in = new LZ4BlockInputStream(
                        CryptoUtils.wrapInToCipheredInputStream(resultSet.getBinaryStream("compressed")))) {
                    return IOUtils.toString(in, "UTF-8");
                }
            }

            @Override
            protected void setPreparedStatement(final String data, final PreparedStatement statement)
                    throws Exception {
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length());
                try (OutputStream out = new LZ4BlockOutputStream(
                        CryptoUtils.wrapInToCipheredOutputStream(baos))) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }

        };
        test.runTest();
    }
    Example4.LOGGER.debug("Done");
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);

    String sql = "SELECT name, description, image FROM pictures ";
    PreparedStatement stmt = conn.prepareStatement(sql);
    ResultSet resultSet = stmt.executeQuery();
    while (resultSet.next()) {
        String name = resultSet.getString(1);
        String description = resultSet.getString(2);
        File image = new File("D:\\java.gif");
        FileOutputStream fos = new FileOutputStream(image);

        byte[] buffer = new byte[1];
        InputStream is = resultSet.getBinaryStream(3);
        while (is.read(buffer) > 0) {
            fos.write(buffer);//from   ww w .  j a v a 2s  .  c  o m
        }
        fos.close();
    }
    conn.close();
}

From source file:org.jiemamy.utils.sql.ResultSetUtil.java

/**
 * {@link ResultSet#getBinaryStream(String)}????????
 * ??????? {@link SQLException}??????{@code defaultValue}?
 * /*from  ww  w  .  j a  v a  2 s  .  c  o m*/
 * <p>{@link #getValue(Class, ResultSet, int, Object)}??{@link ResultSet#getAsciiStream(int)}?
 * ???????????</p>
 * 
 * @param rs {@link ResultSet}
 * @param columnIndex 
 * @param defaultValue {@link SQLException}??????getter????????
 * @return ???????
 * @throws IllegalArgumentException {@code rs}?{@code null}???
 */
public static InputStream getBinaryStream(ResultSet rs, int columnIndex, InputStream defaultValue) {
    Validate.notNull(rs);
    try {
        return rs.getBinaryStream(columnIndex);
    } catch (SQLException e) {
        // ignore
    }
    return defaultValue;
}

From source file:org.jiemamy.utils.sql.ResultSetUtil.java

/**
 * {@link ResultSet#getBinaryStream(String)}??????????
 * ??????? {@link SQLException}??????{@code defaultValue}?
 * // w w  w  .j a  v a  2 s .  c om
 * <p>{@link #getValue(Class, ResultSet, String, Object)}??{@link ResultSet#getAsciiStream(String)}?
 * ???????????</p>
 * 
 * @param rs {@link ResultSet}
 * @param columnName ??
 * @param defaultValue {@link SQLException}??????getter????????
 * @return ???????
 * @throws IllegalArgumentException {@code rs}?{@code null}???
 */
public static InputStream getBinaryStream(ResultSet rs, String columnName, InputStream defaultValue) {
    Validate.notNull(rs);
    try {
        return rs.getBinaryStream(columnName);
    } catch (SQLException e) {
        // ignore
    }
    return defaultValue;
}

From source file:ucd.denver.com.diskussioner.entityParser.parseToJson.java

public static JSONObject parseRSToJson(ResultSet rs) {
    try {/*from ww w.ja  v  a  2s.com*/
        JSONObject jo = new JSONObject();
        //            Field[] cols = User.class.getDeclaredFields();
        while (rs.next()) {
            jo.put("email", rs.getString(1));
            jo.put("password", rs.getString(2));
            jo.put("firstName", rs.getString(3));
            jo.put("lastName", rs.getString(4));
            //        byte[] buffer = new byte[1];
            InputStream is = rs.getBinaryStream(5);
            jo.put("photo", is);
            System.out.println("parseRSToJson++" + jo.toString());
        }
        return jo;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:org.intermine.modelproduction.MetadataManager.java

/**
 * Retrieve the BLOB value for a given key from the metadata table of the database
 * @param database the database//from   w w w.  j  av  a 2 s .  c o  m
 * @param key the key
 * @return the InputStream of the value
 * @throws SQLException if an error occurs
 */
public static InputStream retrieveBLOBInputStream(Database database, String key) throws SQLException {
    InputStream value = null;
    Connection connection = database.getConnection();
    try {
        String sql = "SELECT blob_value FROM " + METADATA_TABLE + " WHERE key ='" + key + "'";
        Statement st = connection.createStatement();
        ResultSet rs = st.executeQuery(sql);

        if (rs.next()) {
            value = rs.getBinaryStream("blob_value");

            return value;
        } else {
            return null;
        }

    } finally {
        connection.close();
    }
}