Java Code Examples for java.sql.PreparedStatement.setBlob()

The following are Jave code examples for showing how to use setBlob() of the java.sql.PreparedStatement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: neoscada   File: AbstractJdbcStorageDao.java   Source Code and License Vote up 7 votes
private static void setReplicationDataBlob ( final PreparedStatement stmt, final Event event ) throws SQLException, IOException
{
    final Blob blob = stmt.getConnection ().createBlob ();

    final ObjectOutputStream oos = new ObjectOutputStream ( blob.setBinaryStream ( 1 ) );
    oos.writeObject ( event );
    oos.close ();

    stmt.setBlob ( 4, blob );
}
 
Example 2
Project: taskana   File: MapTypeHandler.java   Source Code and License Vote up 7 votes
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Map parameter, JdbcType jdbcType) throws SQLException {
    if (parameter != null && parameter.size() > 0) {
        LOGGER.debug("Input-Map before serializing: ", parameter);
        // Convert Map to byte array
        try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream()) {
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(parameter);
            ps.setBlob(i, new ByteArrayInputStream(byteOut.toByteArray()));
            out.close();
        } catch (IOException e) {
            LOGGER.error("During serialization of 'customAttributes' an error occured: ", e);
        }
    } else {
        ps.setNull(i, Types.BLOB);
    }
}
 
Example 3
Project: lams   File: TemporaryLobCreator.java   Source Code and License Vote up 6 votes
@Override
public void setBlobAsBinaryStream(
		PreparedStatement ps, int paramIndex, InputStream binaryStream, int contentLength)
		throws SQLException {

	Blob blob = ps.getConnection().createBlob();
	try {
		FileCopyUtils.copy(binaryStream, blob.setBinaryStream(1));
	}
	catch (IOException ex) {
		throw new DataAccessResourceFailureException("Could not copy into LOB stream", ex);
	}

	this.temporaryBlobs.add(blob);
	ps.setBlob(paramIndex, blob);

	if (logger.isDebugEnabled()) {
		logger.debug(binaryStream != null ?
				"Copied binary stream into temporary BLOB with length " + contentLength :
				"Set BLOB to null");
	}
}
 
Example 4
Project: lams   File: OracleLobHandler.java   Source Code and License Vote up 6 votes
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, final byte[] content)
		throws SQLException {

	if (content != null) {
		Blob blob = (Blob) createLob(ps, false, new LobCallback() {
			@Override
			public void populateLob(Object lob) throws Exception {
				Method methodToInvoke = lob.getClass().getMethod("getBinaryOutputStream");
				OutputStream out = (OutputStream) methodToInvoke.invoke(lob);
				FileCopyUtils.copy(content, out);
			}
		});
		ps.setBlob(paramIndex, blob);
		if (logger.isDebugEnabled()) {
			logger.debug("Set bytes for Oracle BLOB with length " + blob.length());
		}
	}
	else {
		ps.setBlob(paramIndex, (Blob) null);
		logger.debug("Set Oracle BLOB to null");
	}
}
 
Example 5
Project: lams   File: OracleLobHandler.java   Source Code and License Vote up 6 votes
@Override
public void setBlobAsBinaryStream(
		PreparedStatement ps, int paramIndex, final InputStream binaryStream, int contentLength)
		throws SQLException {

	if (binaryStream != null) {
		Blob blob = (Blob) createLob(ps, false, new LobCallback() {
			@Override
			public void populateLob(Object lob) throws Exception {
				Method methodToInvoke = lob.getClass().getMethod("getBinaryOutputStream", (Class[]) null);
				OutputStream out = (OutputStream) methodToInvoke.invoke(lob, (Object[]) null);
				FileCopyUtils.copy(binaryStream, out);
			}
		});
		ps.setBlob(paramIndex, blob);
		if (logger.isDebugEnabled()) {
			logger.debug("Set binary stream for Oracle BLOB with length " + blob.length());
		}
	}
	else {
		ps.setBlob(paramIndex, (Blob) null);
		logger.debug("Set Oracle BLOB to null");
	}
}
 
Example 6
Project: lams   File: ByteArrayBlobType.java   Source Code and License Vote up 6 votes
protected void set(PreparedStatement st, Object value, int index, SessionImplementor session) throws SQLException {
	if ( value == null ) {
		st.setNull( index, sqlTypes( null )[0] );
	}
	else {
		byte[] toSet = unWrap( value );
		final boolean useInputStream = session.getFactory().getDialect().useInputStreamToInsertBlob();

		if ( useInputStream ) {
			st.setBinaryStream( index, new ByteArrayInputStream( toSet ), toSet.length );
		}
		else {
			st.setBlob( index, Hibernate.getLobCreator( session ).createBlob( toSet ) );
		}
	}
}
 
Example 7
Project: jaffa-framework   File: TypeDefs.java   Source Code and License Vote up 6 votes
/** Sets a parameter in the PreparedStatement.
 * @param engineType The engine type as defined in init.xml
 * @param pstmt The PreparedStatement.
 * @param parameterIndex The index of the parameter that is to be set.
 * @param value The object to be assigned to the parameter.
 * @throws SQLException if a database access error occurs.
 */
public void setAppObject(PreparedStatement pstmt, int parameterIndex, Object value, String engineType)
throws SQLException {
    if (value != null) {
        if (!(value instanceof byte[]))
            value = DataTypeMapper.instance().map(value, byte[].class);
        if ("oracle".equalsIgnoreCase(engineType) && !supportsStdLob(pstmt)) {
            Blob blob = createBlob(pstmt.getConnection(), (byte[]) value);
            pstmt.setBlob(parameterIndex, blob);
        } else {
            byte[] bytes = (byte[]) value;
            InputStream stream = new BufferedInputStream(new ByteArrayInputStream(bytes));
            pstmt.setBinaryStream(parameterIndex, stream, bytes.length);
        }
    } else
        pstmt.setNull(parameterIndex, getSqlType(Defaults.BLOB, engineType));
}
 
Example 8
Project: lams   File: TemporaryLobCreator.java   Source Code and License Vote up 5 votes
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content)
		throws SQLException {

	Blob blob = ps.getConnection().createBlob();
	blob.setBytes(1, content);

	this.temporaryBlobs.add(blob);
	ps.setBlob(paramIndex, blob);

	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Copied bytes into temporary BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 9
Project: lams   File: DefaultLobHandler.java   Source Code and License Vote up 5 votes
@Override
public void setBlobAsBytes(PreparedStatement ps, int paramIndex, byte[] content)
		throws SQLException {

	if (streamAsLob) {
		if (content != null) {
			ps.setBlob(paramIndex, new ByteArrayInputStream(content), content.length);
		}
		else {
			ps.setBlob(paramIndex, (Blob) null);
		}
	}
	else if (wrapAsLob) {
		if (content != null) {
			ps.setBlob(paramIndex, new PassThroughBlob(content));
		}
		else {
			ps.setBlob(paramIndex, (Blob) null);
		}
	}
	else {
		ps.setBytes(paramIndex, content);
	}
	if (logger.isDebugEnabled()) {
		logger.debug(content != null ? "Set bytes for BLOB with length " + content.length :
				"Set BLOB to null");
	}
}
 
Example 10
Project: lams   File: DefaultLobHandler.java   Source Code and License Vote up 5 votes
@Override
public void setBlobAsBinaryStream(
		PreparedStatement ps, int paramIndex, InputStream binaryStream, int contentLength)
		throws SQLException {

	if (streamAsLob) {
		if (binaryStream != null) {
			ps.setBlob(paramIndex, binaryStream, contentLength);
		}
		else {
			ps.setBlob(paramIndex, (Blob) null);
		}
	}
	else if (wrapAsLob) {
		if (binaryStream != null) {
			ps.setBlob(paramIndex, new PassThroughBlob(binaryStream, contentLength));
		}
		else {
			ps.setBlob(paramIndex, (Blob) null);
		}
	}
	else {
		ps.setBinaryStream(paramIndex, binaryStream, contentLength);
	}
	if (logger.isDebugEnabled()) {
		logger.debug(binaryStream != null ? "Set binary stream for BLOB with length " + contentLength :
				"Set BLOB to null");
	}
}
 
Example 11
Project: lams   File: OracleDelegate.java   Source Code and License Vote up 5 votes
@Override
public int insertCalendar(Connection conn, String calendarName,
        Calendar calendar) throws IOException, SQLException {
    ByteArrayOutputStream baos = serializeObject(calendar);

    PreparedStatement ps = null;
    PreparedStatement ps2 = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(INSERT_ORACLE_CALENDAR));
        ps.setString(1, calendarName);

        ps.executeUpdate();
        ps.close();

        ps = conn.prepareStatement(rtp(SELECT_ORACLE_CALENDAR_BLOB));
        ps.setString(1, calendarName);

        rs = ps.executeQuery();

        if (rs.next()) {
            Blob dbBlob = writeDataToBlob(rs, 1, baos.toByteArray());
            ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_CALENDAR_BLOB));

            ps2.setBlob(1, dbBlob);
            ps2.setString(2, calendarName);

            return ps2.executeUpdate();
        }

        return 0;

    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeStatement(ps2);
    }
}
 
Example 12
Project: lams   File: OracleDelegate.java   Source Code and License Vote up 5 votes
@Override
public int updateCalendar(Connection conn, String calendarName,
        Calendar calendar) throws IOException, SQLException {
    ByteArrayOutputStream baos = serializeObject(calendar);

    PreparedStatement ps = null;
    PreparedStatement ps2 = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(SELECT_ORACLE_CALENDAR_BLOB));
        ps.setString(1, calendarName);

        rs = ps.executeQuery();

        if (rs.next()) {
            Blob dbBlob = writeDataToBlob(rs, 1, baos.toByteArray());
            ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_CALENDAR_BLOB));

            ps2.setBlob(1, dbBlob);
            ps2.setString(2, calendarName);

            return ps2.executeUpdate();
        }

        return 0;

    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeStatement(ps2);
    }
}
 
Example 13
Project: lams   File: OracleDelegate.java   Source Code and License Vote up 5 votes
@Override
public int updateJobData(Connection conn, JobDetail job)
    throws IOException, SQLException {
    
    ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap());
    byte[] data = baos.toByteArray();

    PreparedStatement ps = null;
    PreparedStatement ps2 = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB));
        ps.setString(1, job.getKey().getName());
        ps.setString(2, job.getKey().getGroup());

        rs = ps.executeQuery();

        int res = 0;

        if (rs.next()) {
            Blob dbBlob = writeDataToBlob(rs, 1, data);
            ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB));

            ps2.setBlob(1, dbBlob);
            ps2.setString(2, job.getKey().getName());
            ps2.setString(3, job.getKey().getGroup());

            res = ps2.executeUpdate();
        }

        return res;
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeStatement(ps2);
    }
}
 
Example 14
Project: lams   File: BlobTypeDescriptor.java   Source Code and License Vote up 5 votes
@Override
public <X> BasicBinder<X> getBlobBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
	return new BasicBinder<X>( javaTypeDescriptor, this ) {
		@Override
		protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
				throws SQLException {
			st.setBlob( index, javaTypeDescriptor.unwrap( value, Blob.class, options ) );
		}
	};
}
 
Example 15
Project: lams   File: OracleDelegate.java   Source Code and License Vote up 4 votes
@Override
public int insertJobDetail(Connection conn, JobDetail job)
    throws IOException, SQLException {

    ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap());
    byte[] data = baos.toByteArray();
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(INSERT_ORACLE_JOB_DETAIL));
        ps.setString(1, job.getKey().getName());
        ps.setString(2, job.getKey().getGroup());
        ps.setString(3, job.getDescription());
        ps.setString(4, job.getJobClass().getName());
        setBoolean(ps, 5, job.isDurable());
        setBoolean(ps, 6, job.isConcurrentExectionDisallowed());
        setBoolean(ps, 7, job.isPersistJobDataAfterExecution());
        setBoolean(ps, 8, job.requestsRecovery());

        ps.executeUpdate();
        ps.close();

        ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB));
        ps.setString(1, job.getKey().getName());
        ps.setString(2, job.getKey().getGroup());

        rs = ps.executeQuery();

        int res = 0;

        Blob dbBlob = null;
        if (rs.next()) {
            dbBlob = writeDataToBlob(rs, 1, data);
        } else {
            return res;
        }

        rs.close();
        ps.close();

        ps = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB));
        ps.setBlob(1, dbBlob);
        ps.setString(2, job.getKey().getName());
        ps.setString(3, job.getKey().getGroup());

        res = ps.executeUpdate();

        return res;
    } finally {
        closeResultSet(rs);
        closeStatement(ps);
    }

}
 
Example 16
Project: lams   File: OracleDelegate.java   Source Code and License Vote up 4 votes
@Override
public int updateJobDetail(Connection conn, JobDetail job)
    throws IOException, SQLException {
    
    ByteArrayOutputStream baos = serializeJobData(job.getJobDataMap());
    byte[] data = baos.toByteArray();

    PreparedStatement ps = null;
    PreparedStatement ps2 = null;
    ResultSet rs = null;

    try {
        ps = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL));
        ps.setString(1, job.getDescription());
        ps.setString(2, job.getJobClass().getName());
        setBoolean(ps, 3, job.isDurable());
        setBoolean(ps, 4, job.isConcurrentExectionDisallowed());
        setBoolean(ps, 5, job.isPersistJobDataAfterExecution());
        setBoolean(ps, 6, job.requestsRecovery());
        ps.setString(7, job.getKey().getName());
        ps.setString(8, job.getKey().getGroup());

        ps.executeUpdate();
        ps.close();

        ps = conn.prepareStatement(rtp(SELECT_ORACLE_JOB_DETAIL_BLOB));
        ps.setString(1, job.getKey().getName());
        ps.setString(2, job.getKey().getGroup());

        rs = ps.executeQuery();

        int res = 0;

        if (rs.next()) {
            Blob dbBlob = writeDataToBlob(rs, 1, data);
            ps2 = conn.prepareStatement(rtp(UPDATE_ORACLE_JOB_DETAIL_BLOB));

            ps2.setBlob(1, dbBlob);
            ps2.setString(2, job.getKey().getName());
            ps2.setString(3, job.getKey().getGroup());

            res = ps2.executeUpdate();
        }

        return res;

    } finally {
        closeResultSet(rs);
        closeStatement(ps);
        closeStatement(ps2);
    }
}
 
Example 17
Project: Lagerta   File: BlobValueTransformer.java   Source Code and License Vote up 4 votes
@Override
public void set(PreparedStatement preparedStatement, int index, Object value) throws SQLException {
    preparedStatement.setBlob(index, new SerialBlob(serializer.serialize(value).array()));
}
 
Example 18
Project: iDataBaseConnection   File: SQLPreparedParamUtil.java   Source Code and License Vote up 4 votes
private static void setValue(PreparedStatement preStmt,int index,Value value)
{
	try 
	{
		if(value.isStringValue())
		{
			preStmt.setString(index, value.getString_value());
		}
		else if(value.isIntValue())
		{
			preStmt.setInt(index, value.getInt_value());
		}
		else if(value.isDoubleValue())
		{
			preStmt.setDouble(index, value.getDouble_value());
		}
		else if(value.isBooleanValue())
		{
			preStmt.setBoolean(index, value.getBoolean_value());
		}
		else if(value.isBlobValue())
		{
			preStmt.setBlob(index, value.getBlob_value());
		}
		else if(value.isBytesValue())
		{
			preStmt.setBytes(index, value.getBytes_value());
		}
		else if(value.isLongValue())
		{
			preStmt.setLong(index, value.getLong_value());
		}
		else if(value.isFloatValue())
		{
			preStmt.setFloat(index, value.getFloat_value());
		}
		else if(value.isBigdecimalValue())
		{
			preStmt.setBigDecimal(index, value.getBigdecimal_value());
		}
		else if(value.isByteValue())
		{
			preStmt.setByte(index, value.getByte_value());
		}
		else if(value.isDateValue())
		{
			preStmt.setDate(index, value.getDate_value());
		}
		else if(value.isTimeValue())
		{
			preStmt.setTime(index, value.getTime_value());
		}
		else
		{
			preStmt.setObject(index, value.getObject_value());
		}
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		DBCException.logException(DBCException.E_PreparedStatement, e);
	}
	
}
 
Example 19
Project: MineDonate   File: ModShopManager.java   Source Code and License Vote up 3 votes
public static void addEntityToShop ( Account acc, Shop s, int catId, int limit, int cost, String name ) {

		EntityInfo info = new EntityInfo(s.sid, catId, s.cats[catId].getNextMerchId(), 0, Integer.valueOf(cost), acc.manageSession.currentMob, name, limit);
        
        s.cats[catId].addMerch(info);
       
        ModNetworkRegistry . sendToAllAddMerchPacket ( info ) ;

        PreparedStatement pstat = null ;

        try {
          
            ByteBuf buf = Unpooled.buffer();
            
            ByteBufUtils.writeTag(buf, info.entity_data);
            
            InputStream stream = new ByteArrayInputStream(buf.array());
        
            pstat = ModDataBase . getPreparedStatement ( "main", "INSERT INTO " + s.cats[catId].getDatabaseTable() + " (name, data, cost, lim) VALUES(?,?,?,?)");
            
            pstat.setString(1, name);
            pstat.setBlob(2, stream);
            pstat.setInt(3, cost);
            pstat.setInt(4, limit);

            pstat.execute();
            
        } catch ( Exception ex ) {
            
        	ex . printStackTrace ( ) ;
            
        }
        
        ModDataBase . closePreparedStatementAndConnection ( pstat ) ;

	}
 
Example 20
Project: MineDonate   File: ModShopManager.java   Source Code and License Vote up 3 votes
public static void addItemToShop ( Account acc, Shop s, int catId, int limit, int cost, String name ) {
	
   	ItemInfo info = new ItemInfo(s.sid, catId, s.cats[catId].getNextMerchId(), 0, Integer.valueOf(cost), name, Integer.valueOf(limit), acc.manageSession.currentItemStack);

       PreparedStatement pstat = null ;

       try {
         
       	ByteBuf buf = Unpooled . buffer ( ) ;
        
           NBTTagCompound nbt = new NBTTagCompound ( ) ;
           
           acc . manageSession . currentItemStack . writeToNBT ( nbt ) ;
		acc . manageSession . setItemStack ( null ) ;
           
           s . cats [ catId ] . addMerch ( info ) ;
           
           ModNetworkRegistry . sendToAllAddMerchPacket ( info ) ;
           
           ByteBufUtils.writeTag(buf, nbt);

           InputStream stream = new ByteArrayInputStream(buf.array());
          
           
           if ( s . sid == 0 ) {
           
           	pstat = ModDataBase . getPreparedStatement ( "main", "INSERT INTO " + s.cats[catId].getDatabaseTable() + " (name, cost, lim, stack_data) VALUES(?,?,?,?)");
           
           } else {
        
           	pstat = ModDataBase . getPreparedStatement ( "main", "INSERT INTO " + s.cats[catId].getDatabaseTable() + " (name, cost, lim, stack_data, shopId) VALUES(?,?,?,?,?)");
           	pstat.setInt(5, s.sid);

           }
           
           pstat.setString(1, name);
           pstat.setInt(2, Integer.valueOf(cost));
           pstat.setInt(3, Integer.valueOf(limit));
           pstat.setBlob(4, stream);
           pstat.execute();
           
       } catch ( Exception ex ) {
           
       	ex . printStackTrace ( ) ;
           
       }
       
       ModDataBase . closePreparedStatementAndConnection ( pstat ) ;

}