Search in sources :

Example 11 with Blob

use of java.sql.Blob in project robovm by robovm.

the class OldPreparedStatementTest method testSetBlob.

public void testSetBlob() {
    ResultSet res = null;
    PreparedStatement ps = null;
    Blob mock = new MockBlob();
    try {
        String neverExecutedQuery = "select TBlob from type;";
        ps = conn.prepareStatement(neverExecutedQuery);
        ps.setBlob(1, mock);
        fail("Exception expected not supported");
    } catch (SQLException e) {
    //ok
    }
}
Also used : Blob(java.sql.Blob) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 12 with Blob

use of java.sql.Blob in project spring-framework by spring-projects.

the class JdbcUtils method getResultSetValue.

/**
	 * Retrieve a JDBC column value from a ResultSet, using the most appropriate
	 * value type. The returned value should be a detached value object, not having
	 * any ties to the active ResultSet: in particular, it should not be a Blob or
	 * Clob object but rather a byte array or String representation, respectively.
	 * <p>Uses the {@code getObject(index)} method, but includes additional "hacks"
	 * to get around Oracle 10g returning a non-standard object for its TIMESTAMP
	 * datatype and a {@code java.sql.Date} for DATE columns leaving out the
	 * time portion: These columns will explicitly be extracted as standard
	 * {@code java.sql.Timestamp} object.
	 * @param rs is the ResultSet holding the data
	 * @param index is the column index
	 * @return the value object
	 * @throws SQLException if thrown by the JDBC API
	 * @see java.sql.Blob
	 * @see java.sql.Clob
	 * @see java.sql.Timestamp
	 */
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
    Object obj = rs.getObject(index);
    String className = null;
    if (obj != null) {
        className = obj.getClass().getName();
    }
    if (obj instanceof Blob) {
        Blob blob = (Blob) obj;
        obj = blob.getBytes(1, (int) blob.length());
    } else if (obj instanceof Clob) {
        Clob clob = (Clob) obj;
        obj = clob.getSubString(1, (int) clob.length());
    } else if ("oracle.sql.TIMESTAMP".equals(className) || "oracle.sql.TIMESTAMPTZ".equals(className)) {
        obj = rs.getTimestamp(index);
    } else if (className != null && className.startsWith("oracle.sql.DATE")) {
        String metaDataClassName = rs.getMetaData().getColumnClassName(index);
        if ("java.sql.Timestamp".equals(metaDataClassName) || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
            obj = rs.getTimestamp(index);
        } else {
            obj = rs.getDate(index);
        }
    } else if (obj instanceof java.sql.Date) {
        if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
            obj = rs.getTimestamp(index);
        }
    }
    return obj;
}
Also used : Blob(java.sql.Blob) Clob(java.sql.Clob)

Example 13 with Blob

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

the class LobCreatorTest method testLobCreation.

private void testLobCreation(LobCreator lobCreator) throws SQLException {
    Blob blob = lobCreator.createBlob(new byte[] {});
    if (lobCreator == NonContextualLobCreator.INSTANCE) {
        assertTrue(blob instanceof BlobImplementer);
    } else {
        assertTrue(blob instanceof JdbcBlob);
    }
    blob = lobCreator.wrap(blob);
    assertTrue(blob instanceof WrappedBlob);
    Clob clob = lobCreator.createClob("Hi");
    if (lobCreator == NonContextualLobCreator.INSTANCE) {
        assertTrue(clob instanceof ClobImplementer);
    } else {
        assertTrue(clob instanceof JdbcClob);
    }
    clob = lobCreator.wrap(clob);
    assertTrue(clob instanceof WrappedClob);
    Clob nclob = lobCreator.createNClob("Hi");
    if (lobCreator == NonContextualLobCreator.INSTANCE) {
        assertTrue(nclob instanceof NClobImplementer);
    } else {
        assertTrue(nclob instanceof JdbcNClob);
    }
    assertTrue(NClob.class.isInstance(nclob));
    nclob = lobCreator.wrap(nclob);
    assertTrue(nclob instanceof WrappedClob);
    blob.free();
    clob.free();
    nclob.free();
}
Also used : WrappedBlob(org.hibernate.engine.jdbc.WrappedBlob) NClob(java.sql.NClob) WrappedBlob(org.hibernate.engine.jdbc.WrappedBlob) Blob(java.sql.Blob) WrappedClob(org.hibernate.engine.jdbc.WrappedClob) NClobImplementer(org.hibernate.engine.jdbc.NClobImplementer) BlobImplementer(org.hibernate.engine.jdbc.BlobImplementer) ClobImplementer(org.hibernate.engine.jdbc.ClobImplementer) NClobImplementer(org.hibernate.engine.jdbc.NClobImplementer) WrappedClob(org.hibernate.engine.jdbc.WrappedClob) NClob(java.sql.NClob) Clob(java.sql.Clob)

Example 14 with Blob

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

the class JpaLargeBlobTest method jpaBlobStream.

@Test
public void jpaBlobStream() throws Exception {
    Session session = openSession();
    LobEntity o = new LobEntity();
    LobHelper lh = session.getLobHelper();
    LobInputStream lis = new LobInputStream();
    session.getTransaction().begin();
    Blob blob = lh.createBlob(lis, LobEntity.BLOB_LENGTH);
    o.setBlob(blob);
    // Regardless if NON_CONTEXTUAL_LOB_CREATION is set to true,
    // ContextualLobCreator should use a NonContextualLobCreator to create
    // a blob Proxy.  If that's the case, the InputStream will not be read
    // until it's persisted with the JDBC driver.
    // Although HHH-7698 was about high memory consumption, this is the best
    // way to test that the high memory use is being prevented.
    assertFalse(lis.wasRead());
    session.persist(o);
    session.getTransaction().commit();
    assertTrue(lis.wasRead());
    session.close();
    lis.close();
}
Also used : Blob(java.sql.Blob) LobHelper(org.hibernate.LobHelper) Session(org.hibernate.Session) Test(org.junit.Test)

Example 15 with Blob

use of java.sql.Blob in project Lucee by lucee.

the class SQLUtil method toBlob.

/**
 * create a blog Object
 * @param conn
 * @param value
 * @return
 * @throws PageException
 * @throws SQLException
 */
public static Blob toBlob(Connection conn, Object value) throws PageException, SQLException {
    if (value instanceof Blob)
        return (Blob) value;
    // Java >= 1.6
    if (SystemUtil.JAVA_VERSION >= SystemUtil.JAVA_VERSION_1_6) {
        try {
            Blob blob = conn.createBlob();
            blob.setBytes(1, Caster.toBinary(value));
            return blob;
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            return BlobImpl.toBlob(value);
        }
    }
    // Java < 1.6
    if (isOracle(conn)) {
        Blob blob = OracleBlob.createBlob(conn, Caster.toBinary(value), null);
        if (blob != null)
            return blob;
    }
    return BlobImpl.toBlob(value);
}
Also used : Blob(java.sql.Blob)

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