Search in sources :

Example 16 with Blob

use of java.sql.Blob in project microservices by pwillhan.

the class LazyProperties method storeLoadLocator.

@Test
public void storeLoadLocator() throws Exception {
    // TODO: This test fails on H2 standalone
    // http://groups.google.com/group/h2-database/browse_thread/thread/9c6f4893a62c9b1a
    UserTransaction tx = TM.getUserTransaction();
    try {
        tx.begin();
        EntityManager em = JPA.createEntityManager();
        byte[] bytes = new byte[131072];
        new Random().nextBytes(bytes);
        InputStream imageInputStream = new ByteArrayInputStream(bytes);
        int byteLength = bytes.length;
        Item someItem = new Item();
        someItem.setName("Some item");
        someItem.setDescription("This is some description.");
        // Need the native Hibernate API
        Session session = em.unwrap(Session.class);
        // You need to know the number of bytes you want to read from the stream!
        Blob blob = session.getLobHelper().createBlob(imageInputStream, byteLength);
        someItem.setImageBlob(blob);
        em.persist(someItem);
        tx.commit();
        em.close();
        Long ITEM_ID = someItem.getId();
        tx.begin();
        em = JPA.createEntityManager();
        Item item = em.find(Item.class, ITEM_ID);
        // You can stream the bytes directly...
        InputStream imageDataStream = item.getImageBlob().getBinaryStream();
        // ... or materialize them into memory:
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        StreamUtils.copy(imageDataStream, outStream);
        byte[] imageBytes = outStream.toByteArray();
        assertEquals(imageBytes.length, 131072);
        tx.commit();
        em.close();
    } finally {
        TM.rollback();
    }
}
Also used : UserTransaction(javax.transaction.UserTransaction) Item(org.jpwh.model.advanced.Item) EntityManager(javax.persistence.EntityManager) Blob(java.sql.Blob) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Session(org.hibernate.Session) JPATest(org.jpwh.env.JPATest) Test(org.testng.annotations.Test)

Example 17 with Blob

use of java.sql.Blob in project jaqy by Teradata.

the class ObjectTypeHandler method getString.

@Override
public String getString(JaqyResultSet rs, int columnIndex, JaqyInterpreter interpreter) throws SQLException {
    Object obj = rs.getObject(columnIndex);
    if (obj == null)
        return null;
    String value = null;
    if (obj instanceof Clob) {
        Clob clob = (Clob) obj;
        value = clob.getSubString(1, (int) clob.length());
        clob.free();
    } else if (obj instanceof byte[]) {
        value = StringUtils.getHexString((byte[]) obj);
    } else if (obj instanceof Blob) {
        Blob blob = ((Blob) obj);
        byte[] bytes = blob.getBytes(1, (int) blob.length());
        value = StringUtils.getHexString((byte[]) bytes);
        blob.free();
    } else {
        value = obj.toString();
    }
    return value;
}
Also used : Blob(java.sql.Blob) Clob(java.sql.Clob)

Example 18 with Blob

use of java.sql.Blob in project incubator-gobblin by apache.

the class MysqlStateStore method getAll.

protected List<T> getAll(String storeName, String tableName, boolean useLike) throws IOException {
    List<T> states = Lists.newArrayList();
    try (Connection connection = dataSource.getConnection();
        PreparedStatement queryStatement = connection.prepareStatement(useLike ? SELECT_JOB_STATE_WITH_LIKE_SQL : SELECT_JOB_STATE_SQL)) {
        queryStatement.setString(1, storeName);
        queryStatement.setString(2, tableName);
        try (ResultSet rs = queryStatement.executeQuery()) {
            while (rs.next()) {
                Blob blob = rs.getBlob(1);
                Text key = new Text();
                try (InputStream is = StreamUtils.isCompressed(blob.getBytes(1, 2)) ? new GZIPInputStream(blob.getBinaryStream()) : blob.getBinaryStream();
                    DataInputStream dis = new DataInputStream(is)) {
                    // keep deserializing while we have data
                    while (dis.available() > 0) {
                        T state = this.stateClass.newInstance();
                        key.readString(dis);
                        state.readFields(dis);
                        states.add(state);
                    }
                } catch (EOFException e) {
                // no more data. GZIPInputStream.available() doesn't return 0 until after EOF.
                }
            }
        }
    } catch (RuntimeException re) {
        throw re;
    } catch (Exception e) {
        throw new IOException("failure retrieving state from storeName " + storeName + " tableName " + tableName, e);
    }
    return states;
}
Also used : Blob(java.sql.Blob) DataInputStream(java.io.DataInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) SQLException(java.sql.SQLException) IOException(java.io.IOException) EOFException(java.io.EOFException) GZIPInputStream(java.util.zip.GZIPInputStream) ResultSet(java.sql.ResultSet) EOFException(java.io.EOFException)

Example 19 with Blob

use of java.sql.Blob in project incubator-gobblin by apache.

the class MysqlStateStore method get.

@Override
public T get(String storeName, String tableName, String stateId) throws IOException {
    try (Connection connection = dataSource.getConnection();
        PreparedStatement queryStatement = connection.prepareStatement(SELECT_JOB_STATE_SQL)) {
        int index = 0;
        queryStatement.setString(++index, storeName);
        queryStatement.setString(++index, tableName);
        try (ResultSet rs = queryStatement.executeQuery()) {
            if (rs.next()) {
                Blob blob = rs.getBlob(1);
                Text key = new Text();
                try (InputStream is = StreamUtils.isCompressed(blob.getBytes(1, 2)) ? new GZIPInputStream(blob.getBinaryStream()) : blob.getBinaryStream();
                    DataInputStream dis = new DataInputStream(is)) {
                    // keep deserializing while we have data
                    while (dis.available() > 0) {
                        T state = this.stateClass.newInstance();
                        key.readFields(dis);
                        state.readFields(dis);
                        if (key.toString().equals(stateId)) {
                            return state;
                        }
                    }
                } catch (EOFException e) {
                // no more data. GZIPInputStream.available() doesn't return 0 until after EOF.
                }
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IOException("failure retrieving state from storeName " + storeName + " tableName " + tableName, e);
    }
    return null;
}
Also used : Blob(java.sql.Blob) DataInputStream(java.io.DataInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) SQLException(java.sql.SQLException) IOException(java.io.IOException) EOFException(java.io.EOFException) GZIPInputStream(java.util.zip.GZIPInputStream) ResultSet(java.sql.ResultSet) EOFException(java.io.EOFException)

Example 20 with Blob

use of java.sql.Blob in project wicket by apache.

the class BlobImageResource method getImageData.

@Override
protected byte[] getImageData(Attributes attributes) {
    try {
        Blob blob = getBlob(attributes);
        if (blob != null) {
            InputStream in = blob.getBinaryStream();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            Streams.copy(in, out);
            return out.toByteArray();
        }
        return new byte[0];
    } catch (SQLException e) {
        throw new WicketRuntimeException("Error while reading image data", e);
    } catch (IOException e) {
        throw new WicketRuntimeException("Error while reading image data", e);
    }
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

Blob (java.sql.Blob)337 ResultSet (java.sql.ResultSet)130 SQLException (java.sql.SQLException)109 PreparedStatement (java.sql.PreparedStatement)106 InputStream (java.io.InputStream)97 Clob (java.sql.Clob)81 ByteArrayInputStream (java.io.ByteArrayInputStream)59 IOException (java.io.IOException)54 Statement (java.sql.Statement)52 Connection (java.sql.Connection)36 Test (org.junit.Test)34 BigDecimal (java.math.BigDecimal)25 Timestamp (java.sql.Timestamp)25 SQLXML (java.sql.SQLXML)22 OutputStream (java.io.OutputStream)21 NClob (java.sql.NClob)21 Reader (java.io.Reader)19 Date (java.sql.Date)18 ArrayList (java.util.ArrayList)17 List (java.util.List)17