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);
}
}
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();
}
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) {
}
}
}
}
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);
}
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();
}
}
Aggregations