Example usage for java.sql Types BLOB

List of usage examples for java.sql Types BLOB

Introduction

In this page you can find the example usage for java.sql Types BLOB.

Prototype

int BLOB

To view the source code for java.sql Types BLOB.

Click Source Link

Document

The constant in the Java programming language, sometimes referred to as a type code, that identifies the generic SQL type BLOB.

Usage

From source file:ro.nextreports.designer.querybuilder.ResultSetTableModel.java

protected boolean isBlobColumn(int column) {
    return result.getColumnType(column) == Types.BLOB;
}

From source file:com.p5solutions.core.jpa.orm.oracle.ConversionUtilityImpl.java

/**
 * If oracle blob.// w w w  .jav a 2  s  .c o  m
 * 
 * @param pb
 *          the pb
 * @param value
 *          the value
 * @return the object
 */
protected Object ifOracleBLOB(ParameterBinder pb, Object value) {

    // if for some reason the object is already an Oracle oracle.sql.BLOB
    if (value instanceof BLOB) {
        BLOB blob = (BLOB) value;

        // simple, if the target and source are the same
        if (Types.BLOB == getSqlType(pb)) {
            return blob;
        }

        // if the destination is an array? not sure why this would happen
        // unless the data model changed during runtime?? return byte array.
        if (Types.ARRAY == getSqlType(pb)) {
            try {
                return blob.getBytes();
            } catch (Exception e) {
                logger.error("Unable to get bytes for when working with " + blob);
            }
        }

        // if the object is of type BLOB but the destination is something else,
        // such as a TIMESTAMP, then its incompatible.
        logger.warn("*** Incompatible value passed of type " + BLOB.class
                + ", when the target is expecting [sql-type: " + getSqlType(pb) + "]. Please check "
                + pb.getEntityClass() + " on paramater " + pb.getBindingPath() + " column name "
                + pb.getColumnNameUpper());
    }

    return null;
}

From source file:com.adaptris.core.services.jdbc.types.BlobColumnTranslatorTest.java

@Test
public void testBlobToStringWrongType() throws Exception {
    BlobColumnTranslator translator = new BlobColumnTranslator();
    JdbcResultRow row = new JdbcResultRow();
    row.setFieldValue("testField", "SomeData", Types.BLOB);

    try {//from www  .  ja  va 2s.  c  om
        translator.translate(row, "testField");
        fail();
    } catch (Exception ex) {
        // pass, expected
    }
}

From source file:org.codehaus.httpcache4j.storage.jdbc.JdbcCacheStorage.java

@Override
public HTTPResponse insert(HTTPRequest request, HTTPResponse response) {
    Key key = Key.create(request, response);
    Connection connection = getConnection();

    String sql = "insert into response(uri, vary, status, headers, payload, mimeType, cachetime) values (?, ?, ?, ?, ?, ?, ?)";
    PreparedStatement statement = null;
    try {//from www  .j  a v  a  2 s  .  c  o  m
        JdbcUtil.startTransaction(connection);
        invalidate(key, connection);
        statement = connection.prepareStatement(sql);
        statement.setString(1, key.getURI().toString());
        statement.setString(2, key.getVary().toJSON());
        statement.setInt(3, response.getStatus().getCode());
        statement.setString(4, response.getHeaders().toJSON());
        InputStream inputStream = null;
        if (response.hasPayload() && response.getPayload().isAvailable()) {
            statement.setString(6, response.getPayload().getMimeType().toString());
            inputStream = response.getPayload().getInputStream();
            statement.setBinaryStream(5, inputStream);
        } else {
            statement.setNull(5, Types.BLOB);
            statement.setNull(6, Types.VARCHAR);
        }
        statement.setTimestamp(7, new Timestamp(DateTimeUtils.currentTimeMillis()));
        try {
            statement.executeUpdate();
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        connection.commit();
        return getImpl(connection, key);
    } catch (SQLException e) {
        JdbcUtil.rollback(connection);
        JdbcUtil.close(connection);
        throw new DataAccessException(e);
    } finally {
        JdbcUtil.endTransaction(connection);
        JdbcUtil.close(statement);
    }
}

From source file:org.jumpmind.db.platform.h2.H2DdlBuilder.java

public H2DdlBuilder() {

    super(DatabaseNamesConstants.H2);

    databaseInfo.setNonPKIdentityColumnsSupported(false);
    databaseInfo.setIdentityOverrideAllowed(false);
    databaseInfo.setSystemForeignKeyIndicesAlwaysNonUnique(true);
    databaseInfo.setNullAsDefaultValueRequired(false);
    databaseInfo.addNativeTypeMapping(Types.ARRAY, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.DISTINCT, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.NULL, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.REF, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.STRUCT, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.DATALINK, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.BIT, "BOOLEAN", Types.BIT);
    databaseInfo.addNativeTypeMapping(Types.NUMERIC, "DECIMAL", Types.DECIMAL);
    databaseInfo.addNativeTypeMapping(Types.BINARY, "BINARY", Types.BINARY);
    databaseInfo.addNativeTypeMapping(Types.BLOB, "BLOB", Types.BLOB);
    databaseInfo.addNativeTypeMapping(Types.CLOB, "CLOB", Types.CLOB);
    databaseInfo.addNativeTypeMapping(Types.LONGVARCHAR, "VARCHAR(" + Integer.MAX_VALUE + ")", Types.VARCHAR);
    databaseInfo.addNativeTypeMapping(Types.FLOAT, "DOUBLE", Types.DOUBLE);
    databaseInfo.addNativeTypeMapping(Types.JAVA_OBJECT, "OTHER");

    databaseInfo.setDefaultSize(Types.CHAR, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.VARCHAR, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.BINARY, Integer.MAX_VALUE);
    databaseInfo.setDefaultSize(Types.VARBINARY, Integer.MAX_VALUE);

    databaseInfo.setNonBlankCharColumnSpacePadded(false);
    databaseInfo.setBlankCharColumnSpacePadded(false);
    databaseInfo.setCharColumnSpaceTrimmed(true);
    databaseInfo.setEmptyStringNulled(false);
    databaseInfo.setNullAsDefaultValueRequired(true);

}

From source file:RowSetModel.java

public Class getColumnClass(int column) {
    String cname;//from w w  w  .  j av a2 s .  co m
    int type;

    try {
        ResultSetMetaData meta = rowSet.getMetaData();

        if (meta == null) {
            return null;
        }
        type = meta.getColumnType(column + 1);
    } catch (SQLException e) {
        e.printStackTrace();
        return super.getColumnClass(column);
    }
    switch (type) {
    case Types.BIT: {
        cname = "java.lang.Boolean";
        break;
    }
    case Types.TINYINT: {
        cname = "java.lang.Byte";
        break;
    }
    case Types.SMALLINT: {
        cname = "java.lang.Short";
        break;
    }
    case Types.INTEGER: {
        cname = "java.lang.Integer";
        break;
    }
    case Types.BIGINT: {
        cname = "java.lang.Long";
        break;
    }
    case Types.FLOAT:
    case Types.REAL: {
        cname = "java.lang.Float";
        break;
    }
    case Types.DOUBLE: {
        cname = "java.lang.Double";
        break;
    }
    case Types.NUMERIC: {
        cname = "java.lang.Number";
        break;
    }
    case Types.DECIMAL: {
        cname = "java.math.BigDecimal";
        break;
    }
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR: {
        cname = "java.lang.String";
        break;
    }
    case Types.DATE: {
        cname = "java.sql.Date";
        break;
    }
    case Types.TIME: {
        cname = "java.sql.Time";
        break;
    }
    case Types.TIMESTAMP: {
        cname = "java.sql.Timestamp";
        break;
    }
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY: {
        cname = "byte[]";
        break;
    }
    case Types.OTHER:
    case Types.JAVA_OBJECT: {
        cname = "java.lang.Object";
        break;
    }
    case Types.CLOB: {
        cname = "java.sql.Clob";
        break;
    }
    case Types.BLOB: {
        cname = "java.ssql.Blob";
        break;
    }
    case Types.REF: {
        cname = "java.sql.Ref";
        break;
    }
    case Types.STRUCT: {
        cname = "java.sql.Struct";
        break;
    }
    default: {
        return super.getColumnClass(column);
    }
    }
    try {
        return Class.forName(cname);
    } catch (Exception e) {
        e.printStackTrace();
        return super.getColumnClass(column);
    }
}

From source file:org.apache.ddlutils.platform.postgresql.PostgreSqlPlatform.java

/**
 * Creates a new platform instance./*from  w  w w .  j av  a  2 s  .  c  om*/
 */
public PostgreSqlPlatform() {
    PlatformInfo info = getPlatformInfo();

    info.setPrimaryKeyColumnAutomaticallyRequired(true);
    // this is the default length though it might be changed when building PostgreSQL
    // in file src/include/postgres_ext.h
    info.setMaxIdentifierLength(31);

    info.addNativeTypeMapping(Types.ARRAY, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.BINARY, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.BIT, "BOOLEAN");
    info.addNativeTypeMapping(Types.BLOB, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.BOOLEAN, "BOOLEAN", Types.BIT);
    info.addNativeTypeMapping(Types.CLOB, "TEXT", Types.LONGVARCHAR);
    info.addNativeTypeMapping(Types.DATALINK, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.DECIMAL, "NUMERIC", Types.NUMERIC);
    info.addNativeTypeMapping(Types.DISTINCT, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.DOUBLE, "DOUBLE PRECISION");
    info.addNativeTypeMapping(Types.FLOAT, "DOUBLE PRECISION", Types.DOUBLE);
    info.addNativeTypeMapping(Types.JAVA_OBJECT, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.LONGVARBINARY, "BYTEA");
    info.addNativeTypeMapping(Types.LONGVARCHAR, "TEXT", Types.LONGVARCHAR);
    info.addNativeTypeMapping(Types.NULL, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.OTHER, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.REF, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.STRUCT, "BYTEA", Types.LONGVARBINARY);
    info.addNativeTypeMapping(Types.TINYINT, "SMALLINT", Types.SMALLINT);
    info.addNativeTypeMapping(Types.VARBINARY, "BYTEA", Types.LONGVARBINARY);

    info.setDefaultSize(Types.CHAR, 254);
    info.setDefaultSize(Types.VARCHAR, 254);

    // no support for specifying the size for these types (because they are mapped
    // to BYTEA which back-maps to BLOB)
    info.setHasSize(Types.BINARY, false);
    info.setHasSize(Types.VARBINARY, false);

    setSqlBuilder(new PostgreSqlBuilder(this));
    setModelReader(new PostgreSqlModelReader(this));
}

From source file:org.jumpmind.symmetric.db.derby.DerbyFunctions.java

protected static void appendCsvString(String tableName, String[] columnNames, String[] pkColumnNames,
        ResultSet rs, StringBuilder builder) throws SQLException {
    ResultSetMetaData metaData = rs.getMetaData();
    for (String columnName : columnNames) {
        if (StringUtils.isNotBlank(columnName)) {
            int index = findColumnIndex(metaData, columnName);
            if (index >= 0) {
                int type = metaData.getColumnType(index);
                switch (type) {
                case Types.BLOB:
                    builder.append(//  w  w  w .  j  a  va  2s.  co m
                            blobToString(columnName, tableName, getPrimaryKeyWhereString(pkColumnNames, rs)));
                    builder.append(",");
                    break;
                case Types.CLOB:
                    builder.append(
                            clobToString(columnName, tableName, getPrimaryKeyWhereString(pkColumnNames, rs)));
                    builder.append(",");
                    break;
                default:
                    builder.append(escape(rs.getString(index)));
                    builder.append(",");
                    break;
                }
            } else {
                builder.append(",");
            }
        } else {
            builder.append(",");
        }
    }
}

From source file:org.sakuli.services.forwarder.database.dao.impl.DaoTestCaseImpl.java

@Override
public void saveTestCaseResult(final TestCase testCase) {
    LOGGER.info("Save results for test case \"" + testCase.getId() + "\"");

    //create a map for the sql parameters
    MapSqlParameterSource tcParameters = new MapSqlParameterSource();
    tcParameters.addValue("sakuli_suites_id", testSuite.getDbPrimaryKey());
    tcParameters.addValue("caseID", testCase.getId());
    tcParameters.addValue("result", testCase.getState().getErrorCode());
    tcParameters.addValue("result_desc", testCase.getState());
    tcParameters.addValue("name", testCase.getName());
    tcParameters.addValue("guid", testSuite.getGuid());
    tcParameters.addValue("start", testCase.getStartDateAsUnixTimestamp());
    tcParameters.addValue("stop", testCase.getStopDateAsUnixTimestamp());
    int warningTime = testCase.getWarningTime();
    tcParameters.addValue("warning", (warningTime != 0) ? warningTime : null);
    int criticalTime = testCase.getCriticalTime();
    tcParameters.addValue("critical", (criticalTime != 0) ? criticalTime : null);
    tcParameters.addValue("browser", testSuite.getBrowserInfo());
    tcParameters.addValue("lastpage", testCase.getLastURL());

    //try to save the screenshot
    tcParameters.addValue("screenshot", getScreenshotAsSqlLobValue(testCase), Types.BLOB);
    tcParameters.addValue("duration", testCase.getDuration());
    tcParameters.addValue("msg", testCase.getExceptionMessages(true));

    //generate the sql-statement
    SimpleJdbcInsert insertTCResults = new SimpleJdbcInsert(getDataSource()).withTableName("sakuli_cases")
            .usingGeneratedKeyColumns("id");

    LOGGER.debug("write the following values to 'sakuli_cases': " + tcParameters.getValues()
            + " => now execute ....");

    int dbPrimaryKey = insertTCResults.executeAndReturnKey(tcParameters).intValue();

    LOGGER.info("test case '" + testCase.getId() + "' has been written to 'sahi_cases' with  primaryKey="
            + dbPrimaryKey);/*w w  w .j a v a2 s.c o  m*/
    testCase.setDbPrimaryKey(dbPrimaryKey);
}

From source file:com.trackplus.ddl.GenericStringValueConverter.java

protected String extractColumnValue(ResultSet resultSet, int columnIdx, int jdbcType)
        throws SQLException, DDLException {
    String value = resultSet.getString(columnIdx);
    if (value != null) {
        switch (jdbcType) {
        case Types.NUMERIC:
        case Types.DECIMAL:
            break;
        case Types.BIT:
        case Types.BOOLEAN:
        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
        case Types.BIGINT:
        case Types.REAL:
        case Types.FLOAT:
        case Types.DOUBLE: {
            break;
        }//from  www  .j a v  a 2 s .c  o  m

        case Types.CHAR:
        case Types.VARCHAR:
        case Types.LONGVARCHAR:
        case Types.BINARY:
        case Types.VARBINARY:
        case Types.TIME:
        case Types.CLOB:
        case Types.ARRAY:
        case Types.REF: {
            value = "'" + value.replaceAll("'", "''") + "'";
            break;
        }
        case Types.DATE:
        case Types.TIMESTAMP: {
            Date d = resultSet.getDate(columnIdx);
            Calendar cal = Calendar.getInstance();
            cal.setTime(d);
            int year = cal.get(Calendar.YEAR);
            if (year < 1900) {
                throw new DDLException("Invalid date:" + d);
            } else {
                value = "'" + value + "'";
            }
            break;
        }
        case Types.BLOB:
        case Types.LONGVARBINARY: {
            Blob blobValue = resultSet.getBlob(columnIdx);
            String str = new String(Base64.encodeBase64(blobValue.getBytes(1l, (int) blobValue.length())));
            value = "'" + str + "'";
            break;
        }
        default:
            break;
        }
    }
    return value;
}