Example usage for java.sql PreparedStatement setBlob

List of usage examples for java.sql PreparedStatement setBlob

Introduction

In this page you can find the example usage for java.sql PreparedStatement setBlob.

Prototype

void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException;

Source Link

Document

Sets the designated parameter to a InputStream object.

Usage

From source file:nz.co.gregs.dbvolution.actions.DBUpdateLargeObjects.java

private void setUsingBLOB(DBDefinition defn, DBRow row, String col, DBLargeObject largeObject, DBDatabase db,
        DBStatement statement) throws SQLException {
    String sqlString = defn.beginUpdateLine() + defn.formatTableName(row) + defn.beginSetClause()
            + defn.formatColumnName(col) + defn.getEqualsComparator() + defn.getPreparedVariableSymbol()
            + defn.beginWhereClause() + defn.formatColumnName(row.getPrimaryKeyColumnName())
            + defn.getEqualsComparator() + row.getPrimaryKey().toSQLString(db) + defn.endSQLStatement();
    //               db.printSQLIfRequested(sqlString);
    log.debug(sqlString);// w  w  w . java 2s  .  c  o  m
    PreparedStatement prep = statement.getConnection().prepareStatement(sqlString);
    try {
        prep.setBlob(1, largeObject.getInputStream(), largeObject.getSize());
        prep.execute();
    } finally {
        prep.close();
    }
}

From source file:org.apache.nifi.processors.standard.util.TestJdbcCommon.java

@Test
public void testBlob() throws Exception {
    try (final Statement stmt = con.createStatement()) {
        stmt.executeUpdate("CREATE TABLE blobtest (id INT, b BLOB(64 K))");
        stmt.execute("INSERT INTO blobtest VALUES (41, NULL)");
        PreparedStatement ps = con.prepareStatement("INSERT INTO blobtest VALUES (?, ?)");
        ps.setInt(1, 42);//www  .  j ava  2  s  .  c o m
        final byte[] buffer = new byte[4002];
        IntStream.range(0, 4002).forEach((i) -> buffer[i] = (byte) ((i % 10) + 65));
        ByteArrayInputStream bais = new ByteArrayInputStream(buffer);

        // - set the value of the input parameter to the input stream
        ps.setBlob(2, bais, 4002);
        ps.execute();
        bais.close();

        final ResultSet resultSet = stmt.executeQuery("select * from blobtest");

        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        JdbcCommon.convertToAvroStream(resultSet, outStream, false);

        final byte[] serializedBytes = outStream.toByteArray();
        assertNotNull(serializedBytes);

        // Deserialize bytes to records
        final InputStream instream = new ByteArrayInputStream(serializedBytes);

        final DatumReader<GenericRecord> datumReader = new GenericDatumReader<>();
        try (final DataFileStream<GenericRecord> dataFileReader = new DataFileStream<>(instream, datumReader)) {
            GenericRecord record = null;
            while (dataFileReader.hasNext()) {
                // Reuse record object by passing it to next(). This saves us from
                // allocating and garbage collecting many objects for files with
                // many items.
                record = dataFileReader.next(record);
                Integer id = (Integer) record.get("ID");
                Object o = record.get("B");
                if (id == 41) {
                    assertNull(o);
                } else {
                    assertNotNull(o);
                    assertTrue(o instanceof ByteBuffer);
                    assertEquals(4002, ((ByteBuffer) o).array().length);
                }
            }
        }
    }
}

From source file:org.apache.gora.sql.store.SqlStore.java

protected void setBytes(PreparedStatement statement, Column column, int index, byte[] value)
        throws SQLException {

    switch (column.getJdbcType()) {
    case BLOB:/*from ww  w .  j ava 2  s. c  o  m*/
        statement.setBlob(index, new ByteArrayInputStream(value), value.length);
        break;
    case BINARY:
    case VARBINARY:
        statement.setBytes(index, value);
        break;
    case LONGVARBINARY:
        statement.setBinaryStream(index, new ByteArrayInputStream(value));
        break;
    }
}

From source file:org.wso2.carbon.dataservices.core.description.query.SQLQuery.java

private void setBlobValue(int queryType, String paramName, String value, String paramType,
        PreparedStatement sqlQuery, int i) throws SQLException, DataServiceFault {
    if ("IN".equals(paramType)) {
        if (value == null) {
            sqlQuery.setNull(i + 1, java.sql.Types.BLOB);
        } else {//w  ww  .  j  ava2 s  .  co m
            byte[] data = this.getBytesFromBase64String(value);
            sqlQuery.setBlob(i + 1, new ByteArrayInputStream(data), data.length);
        }
    } else if ("INOUT".equals(paramType)) {
        if (value == null) {
            ((CallableStatement) sqlQuery).setNull(i + 1, java.sql.Types.BLOB);
        } else {
            byte[] data = this.getBytesFromBase64String(value);
            ((CallableStatement) sqlQuery).setBlob(i + 1, new ByteArrayInputStream(data), data.length);
        }
        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.BLOB);
    } else {
        ((CallableStatement) sqlQuery).registerOutParameter(i + 1, java.sql.Types.BLOB);
    }
}

From source file:org.wso2.carbon.dataservices.core.odata.RDBMSDataHandler.java

/**
 * This method bind values to prepared statement.
 *
 * @param type            data Type/*from ww  w  . j  a v a2 s  .  c om*/
 * @param value           String value
 * @param ordinalPosition Ordinal Position
 * @param sqlStatement    Statement
 * @throws SQLException
 * @throws ParseException
 * @throws ODataServiceFault
 */
private void bindValuesToPreparedStatement(int type, String value, int ordinalPosition,
        PreparedStatement sqlStatement) throws SQLException, ParseException, ODataServiceFault {
    byte[] data;
    try {
        switch (type) {
        case Types.INTEGER:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setInt(ordinalPosition, ConverterUtil.convertToInt(value));
            }
            break;
        case Types.TINYINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setByte(ordinalPosition, ConverterUtil.convertToByte(value));
            }
            break;
        case Types.SMALLINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setShort(ordinalPosition, ConverterUtil.convertToShort(value));
            }
            break;
        case Types.DOUBLE:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setDouble(ordinalPosition, ConverterUtil.convertToDouble(value));
            }
            break;
        case Types.VARCHAR:
            /* fall through */
        case Types.CHAR:
            /* fall through */
        case Types.LONGVARCHAR:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setString(ordinalPosition, value);
            }
            break;
        case Types.CLOB:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setClob(ordinalPosition, new BufferedReader(new StringReader(value)),
                        value.length());
            }
            break;
        case Types.BOOLEAN:
            /* fall through */
        case Types.BIT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setBoolean(ordinalPosition, ConverterUtil.convertToBoolean(value));
            }
            break;
        case Types.BLOB:
            /* fall through */
        case Types.LONGVARBINARY:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                data = this.getBytesFromBase64String(value);
                sqlStatement.setBlob(ordinalPosition, new ByteArrayInputStream(data), data.length);
            }
            break;
        case Types.BINARY:
            /* fall through */
        case Types.VARBINARY:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                data = this.getBytesFromBase64String(value);
                sqlStatement.setBinaryStream(ordinalPosition, new ByteArrayInputStream(data), data.length);
            }
            break;
        case Types.DATE:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setDate(ordinalPosition, DBUtils.getDate(value));
            }
            break;
        case Types.DECIMAL:
            /* fall through */
        case Types.NUMERIC:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setBigDecimal(ordinalPosition, ConverterUtil.convertToBigDecimal(value));
            }
            break;
        case Types.FLOAT:
            /* fall through */
        case Types.REAL:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setFloat(ordinalPosition, ConverterUtil.convertToFloat(value));
            }
            break;
        case Types.TIME:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setTime(ordinalPosition, DBUtils.getTime(value));
            }
            break;
        case Types.LONGNVARCHAR:
            /* fall through */
        case Types.NCHAR:
            /* fall through */
        case Types.NVARCHAR:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setNString(ordinalPosition, value);
            }
            break;
        case Types.NCLOB:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setNClob(ordinalPosition, new BufferedReader(new StringReader(value)),
                        value.length());
            }
            break;
        case Types.BIGINT:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setLong(ordinalPosition, ConverterUtil.convertToLong(value));
            }
            break;
        case Types.TIMESTAMP:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setTimestamp(ordinalPosition, DBUtils.getTimestamp(value));
            }
            break;
        default:
            if (value == null) {
                sqlStatement.setNull(ordinalPosition, type);
            } else {
                sqlStatement.setString(ordinalPosition, value);
            }
            break;
        }
    } catch (DataServiceFault e) {
        throw new ODataServiceFault(e, "Error occurred while binding values. :" + e.getMessage());
    }
}