Search in sources :

Example 21 with Blob

use of java.sql.Blob in project hibernate-orm by hibernate.

the class ContextualLobCreator method createBlob.

@Override
public Blob createBlob(byte[] bytes) {
    try {
        final Blob blob = createBlob();
        blob.setBytes(1, bytes);
        return blob;
    } catch (SQLException e) {
        throw new JDBCException("Unable to set BLOB bytes after creation", e);
    }
}
Also used : Blob(java.sql.Blob) JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException)

Example 22 with Blob

use of java.sql.Blob in project jaybird by FirebirdSQL.

the class TestFBBlob method checkReadBlob.

protected void checkReadBlob(String name) throws Exception {
    PreparedStatement p = c.prepareStatement("select * from " + name + " where C1 = ?");
    p.setInt(1, 1);
    ResultSet rs = p.executeQuery();
    while (rs.next()) {
        if (log != null)
            log.info("C1: " + rs.getInt(1));
        Blob blobRead = rs.getBlob(2);
        InputStream is = blobRead.getBinaryStream();
        int count = 0;
        while (is.read() != -1) {
            count++;
        }
        if (log != null)
            log.info("C2 count: " + count);
        assertEquals("retrieved wrong length blob: expecting " + bloblength + ", retrieved: " + count, bloblength, count);
    }
    p.close();
}
Also used : Blob(java.sql.Blob) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 23 with Blob

use of java.sql.Blob in project hibernate-orm by hibernate.

the class AbstractExpectationsFactory method retrieveExpected.

protected <T> Map<Integer, T> retrieveExpected(NativeSQLStatement nativeSQLStatement, int type) throws SQLException {
    PreparedStatement preparedStatement = null;
    ResultSet results = null;
    Connection cn = null;
    Map<Integer, T> expected = new HashMap<Integer, T>();
    try {
        cn = createConnection();
        preparedStatement = nativeSQLStatement.prepare(cn);
        LOG.info("Native SQL is: " + nativeSQLStatement.toString());
        results = preparedStatement.executeQuery();
        while (results.next()) {
            int id = results.getInt(1);
            switch(type) {
                case GEOMETRY:
                    expected.put(id, (T) decode(results.getObject(2)));
                    break;
                case STRING:
                    expected.put(id, (T) results.getString(2));
                    break;
                case INTEGER:
                    {
                        Long value = Long.valueOf(results.getLong(2));
                        if (results.wasNull()) {
                            // This is required because the Hibernate BasicExtractor also checks ResultSet#wasNull which can lead to a mismatch between the expected and the actual results
                            value = null;
                        }
                        expected.put(id, (T) value);
                    }
                    break;
                case DOUBLE:
                    {
                        Double value = Double.valueOf(results.getDouble(2));
                        if (results.wasNull()) {
                            // this is required because SQL Server converts automatically null to 0.0
                            value = null;
                        }
                        expected.put(id, (T) value);
                    }
                    break;
                case BOOLEAN:
                    expected.put(id, (T) Boolean.valueOf(results.getBoolean(2)));
                    break;
                default:
                    T val = (T) results.getObject(2);
                    // TODO -- clean up
                    if (val instanceof Blob) {
                        val = (T) ((Blob) val).getBytes(1, MAX_BYTE_LEN);
                    }
                    expected.put(id, val);
            }
        }
        return expected;
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (SQLException e) {
            }
        }
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (SQLException e) {
            }
        }
        if (cn != null) {
            try {
                cn.close();
            } catch (SQLException e) {
            }
        }
    }
}
Also used : Blob(java.sql.Blob) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Point(com.vividsolutions.jts.geom.Point)

Example 24 with Blob

use of java.sql.Blob in project rxjava2-jdbc by davidmoten.

the class UtilTest method testToBytesThrowsSqlException.

@Test(expected = SQLRuntimeException.class)
public void testToBytesThrowsSqlException() throws SQLException {
    Blob blob = Mockito.mock(Blob.class);
    // 
    Mockito.when(blob.getBinaryStream()).thenThrow(new SQLException("boo"));
    Util.toBytes(blob);
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 25 with Blob

use of java.sql.Blob in project components by Talend.

the class DBTestUtils method loadAllTypesData.

/**
 * Load only one record
 */
public static void loadAllTypesData(Connection conn, String tablename) throws SQLException {
    try (PreparedStatement statement = conn.prepareStatement("insert into " + tablename + " values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")) {
        statement.setShort(1, (short) 32767);
        statement.setInt(2, 2147483647);
        statement.setLong(3, 9223372036854775807l);
        statement.setFloat(4, 1.11111111f);
        statement.setDouble(5, 2.222222222);
        statement.setBigDecimal(6, new BigDecimal("1234567890.1234567890"));
        statement.setString(7, "abcd");
        statement.setString(8, "abcdefg");
        Blob blob = conn.createBlob();
        byte[] bytes = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        blob.setBytes(1, bytes);
        statement.setBlob(9, blob);
        Clob clob = conn.createClob();
        clob.setString(1, "abcdefg");
        statement.setClob(10, clob);
        statement.setDate(11, Date.valueOf("2016-12-28"));
        statement.setTime(12, Time.valueOf("14:30:33"));
        statement.setTimestamp(13, Timestamp.valueOf("2016-12-28 14:31:56.12345"));
        statement.setBoolean(14, true);
        statement.executeUpdate();
    }
    if (!conn.getAutoCommit()) {
        conn.commit();
    }
}
Also used : Blob(java.sql.Blob) PreparedStatement(java.sql.PreparedStatement) Clob(java.sql.Clob) BigDecimal(java.math.BigDecimal)

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