Example usage for java.sql Types NUMERIC

List of usage examples for java.sql Types NUMERIC

Introduction

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

Prototype

int NUMERIC

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

Click Source Link

Document

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

Usage

From source file:org.eclipse.ecr.core.storage.sql.extensions.H2Fulltext.java

protected static String asString(Object data, int type) throws SQLException {
    if (data == null) {
        return "";
    }/*  www . ja va 2 s  . c  om*/
    switch (type) {
    case Types.BIT:
    case DataType.TYPE_BOOLEAN:
    case Types.INTEGER:
    case Types.BIGINT:
    case Types.DECIMAL:
    case Types.DOUBLE:
    case Types.FLOAT:
    case Types.NUMERIC:
    case Types.REAL:
    case Types.SMALLINT:
    case Types.TINYINT:
    case Types.DATE:
    case Types.TIME:
    case Types.TIMESTAMP:
    case Types.LONGVARCHAR:
    case Types.CHAR:
    case Types.VARCHAR:
        return data.toString();
    case Types.CLOB:
        try {
            if (data instanceof Clob) {
                data = ((Clob) data).getCharacterStream();
            }
            return IOUtils.readStringAndClose((Reader) data, -1);
        } catch (IOException e) {
            throw Message.convert(e);
        }
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
    case Types.BINARY:
    case Types.JAVA_OBJECT:
    case Types.OTHER:
    case Types.BLOB:
    case Types.STRUCT:
    case Types.REF:
    case Types.NULL:
    case Types.ARRAY:
    case DataType.TYPE_DATALINK:
    case Types.DISTINCT:
        throw new SQLException("Unsupported column data type: " + type);
    default:
        return "";
    }
}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

private void deleteVersion(Page page, int version) {
    getExtendedJdbcTemplate().update(getBoundSql("ARCHITECTURE_COMMUNITY.DELETE_PAGE_BODY_VERSION").getSql(),
            new SqlParameterValue(Types.NUMERIC, page.getPageId()),
            new SqlParameterValue(Types.NUMERIC, version));
    getExtendedJdbcTemplate().update(getBoundSql("ARCHITECTURE_COMMUNITY.DELETE_PAGE_VERSION").getSql(),
            new SqlParameterValue(Types.NUMERIC, page.getPageId()),
            new SqlParameterValue(Types.NUMERIC, version));
}

From source file:ca.sqlpower.sqlobject.TestSQLColumn.java

public void testGetType() throws Exception {
    SQLColumn tmpCol = new SQLColumn();
    tmpCol.setType(Types.CHAR);//from   w w  w .j a  v  a 2s  . c om
    assertEquals(Types.CHAR, tmpCol.getType());
    SQLColumn cowCol = table1pk.getColumn(0);
    assertEquals(Types.NUMERIC, cowCol.getType());
    cowCol.setType(Types.CHAR);
    assertEquals(Types.CHAR, cowCol.getType());
}

From source file:org.apache.jmeter.protocol.jdbc.AbstractJDBCTestElement.java

private void setArgument(PreparedStatement pstmt, String argument, int targetSqlType, int index)
        throws SQLException {
    switch (targetSqlType) {
    case Types.INTEGER:
        pstmt.setInt(index, Integer.parseInt(argument));
        break;//  ww  w . j  a va 2s. c o  m
    case Types.DECIMAL:
    case Types.NUMERIC:
        pstmt.setBigDecimal(index, new BigDecimal(argument));
        break;
    case Types.DOUBLE:
    case Types.FLOAT:
        pstmt.setDouble(index, Double.parseDouble(argument));
        break;
    case Types.CHAR:
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
        pstmt.setString(index, argument);
        break;
    case Types.BIT:
    case Types.BOOLEAN:
        pstmt.setBoolean(index, Boolean.parseBoolean(argument));
        break;
    case Types.BIGINT:
        pstmt.setLong(index, Long.parseLong(argument));
        break;
    case Types.DATE:
        pstmt.setDate(index, Date.valueOf(argument));
        break;
    case Types.REAL:
        pstmt.setFloat(index, Float.parseFloat(argument));
        break;
    case Types.TINYINT:
        pstmt.setByte(index, Byte.parseByte(argument));
        break;
    case Types.SMALLINT:
        pstmt.setShort(index, Short.parseShort(argument));
        break;
    case Types.TIMESTAMP:
        pstmt.setTimestamp(index, Timestamp.valueOf(argument));
        break;
    case Types.TIME:
        pstmt.setTime(index, Time.valueOf(argument));
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        pstmt.setBytes(index, argument.getBytes());
        break;
    case Types.NULL:
        pstmt.setNull(index, targetSqlType);
        break;
    default:
        pstmt.setObject(index, argument, targetSqlType);
    }
}

From source file:org.sakaiproject.webservices.SakaiReport.java

private String getColumnValue(ResultSet rs, int colType, int colIndex) throws SQLException, IOException {
    String value = "";
    switch (colType) {
    case Types.BIT:
    case Types.JAVA_OBJECT:
        value = handleObject(rs.getObject(colIndex));
        break;//from  ww w  .j a  va 2  s. c  o  m
    case Types.BOOLEAN:
        boolean b = rs.getBoolean(colIndex);
        value = Boolean.valueOf(b).toString();
        break;
    case NCLOB: // todo : use rs.getNClob
    case Types.CLOB:
        Clob c = rs.getClob(colIndex);
        if (c != null) {
            value = read(c);
        }
        break;
    case Types.BIGINT:
        value = handleLong(rs, colIndex);
        break;
    case Types.DECIMAL:
    case Types.DOUBLE:
    case Types.FLOAT:
    case Types.REAL:
    case Types.NUMERIC:
        value = handleBigDecimal(rs.getBigDecimal(colIndex));
        break;
    case Types.INTEGER:
    case Types.TINYINT:
    case Types.SMALLINT:
        value = handleInteger(rs, colIndex);
        break;
    case Types.DATE:
        value = handleDate(rs, colIndex);
        break;
    case Types.TIME:
        value = handleTime(rs.getTime(colIndex));
        break;
    case Types.TIMESTAMP:
        value = handleTimestamp(rs.getTimestamp(colIndex));
        break;
    case NVARCHAR: // todo : use rs.getNString
    case NCHAR: // todo : use rs.getNString
    case LONGNVARCHAR: // todo : use rs.getNString
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
    case Types.CHAR:
        value = rs.getString(colIndex);
        break;
    case Types.VARBINARY:
    case Types.BINARY:
        value = handleRaw(rs.getBytes(colIndex));
        break;
    default:
        value = "";
    }

    if (value == null) {
        value = "";
    }

    return value;

}

From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java

public void setColumnSpecificHeaders(Record record, Set<String> knownTableNames, ResultSetMetaData metaData,
        String jdbcNameSpacePrefix) throws SQLException {
    Record.Header header = record.getHeader();
    Set<String> tableNames = new HashSet<>();
    for (int i = 1; i <= metaData.getColumnCount(); i++) {
        header.setAttribute(jdbcNameSpacePrefix + metaData.getColumnLabel(i) + ".jdbcType",
                String.valueOf(metaData.getColumnType(i)));

        // Additional headers per various types
        switch (metaData.getColumnType(i)) {
        case Types.DECIMAL:
        case Types.NUMERIC:
            header.setAttribute(jdbcNameSpacePrefix + metaData.getColumnLabel(i) + ".scale",
                    String.valueOf(metaData.getScale(i)));
            header.setAttribute(jdbcNameSpacePrefix + metaData.getColumnLabel(i) + ".precision",
                    String.valueOf(metaData.getPrecision(i)));
            break;
        }//www.  j  a v  a  2s.  co m

        String tableName = metaData.getTableName(i);

        // Store the column's table name (if not empty)
        if (StringUtils.isNotEmpty(tableName)) {
            tableNames.add(tableName);
        }
    }

    if (tableNames.isEmpty()) {
        tableNames.addAll(knownTableNames);
    }

    header.setAttribute(jdbcNameSpacePrefix + "tables", Joiner.on(",").join(tableNames));
}

From source file:org.apache.cassandra.cql.jdbc.HandleObjects.java

public static final ByteBuffer makeBytes(Object object, int baseType, int scaleOrLength) throws SQLException {
    Class<? extends Object> objectClass = object.getClass();
    boolean isCollection = (Collection.class.isAssignableFrom(objectClass));
    int targetSqlType = isCollection ? Types.OTHER : baseType;
    // Type check first
    switch (targetSqlType) {
    case Types.TINYINT:
        // Only Numeric classes, Strings and Booleans are supported for transformation to TINYINT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "TINYINT");
        break;// w w  w. j  ava 2 s. c om

    case Types.SMALLINT:
        // Only Numeric classes, Strings and Booleans are supported for transformation to SMALLINT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "SMALLINT");
        break;

    case Types.INTEGER:
        // Only Numeric classes, Strings and Booleans are supported for transformation to INTEGER
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "INTEGER");
        break;

    case Types.BIGINT:
        // Only Numeric classes, Strings and Booleans are supported for transformation to BIGINT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "BIGINT");
        break;

    case Types.REAL:
    case Types.FLOAT:
    case Types.DOUBLE:
    case Types.DECIMAL:
        // Only Numeric classes Strings and Booleans are supported for transformation to REAL,FLOAT,DOUBLE,DECIMAL
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "the floating point types");
        break;

    case Types.NUMERIC:
        //NB This as a special case variation for Cassandra!! NUMERIC is transformed to java BigInteger (varint CQL type)
        //
        // Only Numeric classes Strings and Booleans are supported for transformation to NUMERIC
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "NUMERIC");
        break;

    case Types.BIT:
        // Only Numeric classes Strings and Booleans are supported for transformation to BIT
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "BIT");
        break;

    case Types.BOOLEAN:
        // Only Numeric classes Strings and Booleans are supported for transformation to BOOLEAN
        if (!(objectClass == String.class || objectClass == Boolean.class
                || Number.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "BOOLEAN");
        break;

    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
    case Types.NVARCHAR:
    case Types.LONGNVARCHAR:
        if (!objectClass.isAssignableFrom(String.class))
            throw makeBadMapping(objectClass, "String", "the various VARCHAR types");
        break;

    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        if (!(objectClass.isAssignableFrom(ByteBuffer.class) || objectClass.getSimpleName().equals("byte[]")))
            throw makeBadMapping(objectClass, "ByteBuffer or byte[]", "the BINARY Types");
        break;

    case Types.DATE:
        if (!(objectClass == String.class || objectClass == java.util.Date.class || objectClass == Date.class
                || objectClass == Timestamp.class))
            throw makeBadMapping(objectClass, "String, Date(java and sql) or Timestamp types", "DATE");
        break;

    case Types.TIME:
        if (!(objectClass == String.class || objectClass == java.util.Date.class || objectClass == Time.class
                || objectClass == Timestamp.class))
            throw makeBadMapping(objectClass, "String, Date (java), Time or Timestamp types", "TIME");
        break;

    case Types.TIMESTAMP:
        if (!(objectClass == String.class || objectClass == java.util.Date.class || objectClass == Date.class
                || objectClass == Timestamp.class))
            throw makeBadMapping(objectClass, "String, Date(java and sql) or Timestamp types", "TIMESTAMP");
        break;

    case Types.DATALINK:
        if (objectClass != URL.class)
            throw makeBadMapping(objectClass, "a URL type", "DATALINK");
        break;

    case Types.JAVA_OBJECT:
        break;

    case Types.OTHER:
        // Only Collection classes for transformation to OTHER
        if (!(List.class.isAssignableFrom(object.getClass()) || Set.class.isAssignableFrom(object.getClass())
                || Map.class.isAssignableFrom(object.getClass())))
            throw makeBadMapping(objectClass, STR_BOOL_NUMERIC, "OTHER");
        break;

    case Types.ROWID:
        if (objectClass != RowId.class)
            throw makeBadMapping(objectClass, "a RowId type", "ROWID");
        break;

    default:
        throw new SQLNonTransientException("Unsupported transformation to Jdbc Type: " + targetSqlType);
    }

    // see if we can map to an supported Type
    switch (targetSqlType) {
    case Types.BIT:
        BigInteger bitvalue = objectToBITorTINYINTorSMALLINTorNUMERIC(objectClass, object);
        assert bitvalue != null;
        return JdbcInteger.instance.decompose((bitvalue == BigInteger.ZERO) ? BigInteger.ZERO : BigInteger.ONE);

    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.NUMERIC:
        BigInteger varint = objectToBITorTINYINTorSMALLINTorNUMERIC(objectClass, object);
        assert varint != null;
        return JdbcInteger.instance.decompose(varint);

    case Types.INTEGER:
        Integer value = objectToINTEGER(objectClass, object);
        assert value != null;
        return JdbcInt32.instance.decompose(value);

    case Types.BIGINT:
        Long longvalue = objectToBIGINT(objectClass, object);
        assert longvalue != null;
        return JdbcLong.instance.decompose(longvalue);

    case Types.BOOLEAN:
        Boolean bool = objectToBOOLEAN(objectClass, object);
        assert bool != null;
        return JdbcBoolean.instance.decompose(bool);

    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
    case Types.NVARCHAR:
    case Types.LONGNVARCHAR:
        return ByteBufferUtil.bytes((String) object);

    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        if (objectClass.isAssignableFrom(ByteBuffer.class)) {
            return ((ByteBuffer) object);
        } else if (objectClass.getSimpleName().equals("byte[]")) {
            return ByteBuffer.wrap((byte[]) object);
        } else
            return null; // this should not happen

    case Types.DATE:
    case Types.TIME:
    case Types.TIMESTAMP:
        Long millis = objectToDATEorTIMEorTIMESTAMP(objectClass, object);
        assert millis != null;
        return JdbcLong.instance.decompose(millis);

    case Types.DATALINK:
        String urlAsString = ((URL) object).toExternalForm();
        return JdbcUTF8.instance.decompose(urlAsString);

    case Types.JAVA_OBJECT:
        return javaObject(object);

    case Types.OTHER:
        if (List.class.isAssignableFrom(objectClass)) {
            return handleAsList(objectClass, object);
        } else if (Set.class.isAssignableFrom(objectClass)) {
            return handleAsSet(objectClass, object);
        } else if (Map.class.isAssignableFrom(objectClass)) {
            return handleAsMap(objectClass, object);
        } else
            return null;

    case Types.ROWID:
        byte[] bytes = ((RowId) object).getBytes();
        return ByteBuffer.wrap(bytes);

    default:
        LOG.warn("Unhandled JDBC type: " + targetSqlType);
        return null;
    }
}

From source file:org.jumpmind.db.platform.AbstractDatabasePlatform.java

protected Object getObjectValue(String value, Column column, BinaryEncoding encoding, boolean useVariableDates,
        boolean fitToColumn) throws DecoderException {
    Object objectValue = value;/*www  .j  av a2 s.com*/
    int type = column.getMappedTypeCode();
    if ((value == null || (getDdlBuilder().getDatabaseInfo().isEmptyStringNulled() && value.equals("")))
            && column.isRequired() && column.isOfTextType()) {
        objectValue = REQUIRED_FIELD_NULL_SUBSTITUTE;
    }
    if (value != null) {
        if (type == Types.DATE || type == Types.TIMESTAMP || type == Types.TIME) {
            objectValue = parseDate(type, value, useVariableDates);
        } else if (type == Types.CHAR) {
            String charValue = value.toString();
            if ((StringUtils.isBlank(charValue)
                    && getDdlBuilder().getDatabaseInfo().isBlankCharColumnSpacePadded())
                    || (StringUtils.isNotBlank(charValue)
                            && getDdlBuilder().getDatabaseInfo().isNonBlankCharColumnSpacePadded())) {
                objectValue = StringUtils.rightPad(value.toString(), column.getSizeAsInt(), ' ');
            }
        } else if (type == Types.BIGINT) {
            objectValue = parseBigInteger(value);
        } else if (type == Types.INTEGER || type == Types.SMALLINT || type == Types.BIT
                || type == Types.TINYINT) {
            objectValue = parseInteger(value);
        } else if (type == Types.NUMERIC || type == Types.DECIMAL || type == Types.FLOAT || type == Types.DOUBLE
                || type == Types.REAL) {
            objectValue = parseBigDecimal(value);
        } else if (type == Types.BOOLEAN) {
            objectValue = value.equals("1") ? Boolean.TRUE : Boolean.FALSE;
        } else if (!(column.getJdbcTypeName() != null
                && column.getJdbcTypeName().toUpperCase().contains(TypeMap.GEOMETRY))
                && !(column.getJdbcTypeName() != null
                        && column.getJdbcTypeName().toUpperCase().contains(TypeMap.GEOGRAPHY))
                && (type == Types.BLOB || type == Types.LONGVARBINARY || type == Types.BINARY
                        || type == Types.VARBINARY ||
                        // SQLServer ntext type
                        type == -10)) {
            if (encoding == BinaryEncoding.NONE) {
                objectValue = value.getBytes();
            } else if (encoding == BinaryEncoding.BASE64) {
                objectValue = Base64.decodeBase64(value.getBytes());
            } else if (encoding == BinaryEncoding.HEX) {
                objectValue = Hex.decodeHex(value.toCharArray());
            }
        } else if (type == Types.ARRAY) {
            objectValue = createArray(column, value);
        }
    }
    if (objectValue instanceof String) {
        String stringValue = cleanTextForTextBasedColumns((String) objectValue);
        int size = column.getSizeAsInt();
        if (fitToColumn && size > 0 && stringValue.length() > size) {
            stringValue = stringValue.substring(0, size);
        }
        objectValue = stringValue;
    }

    return objectValue;

}

From source file:org.jumpmind.symmetric.service.impl.RegistrationService.java

public void saveRegistrationRequest(RegistrationRequest request) {
    /**/*  w w  w. jav a 2 s  .c o m*/
     * Lookup existing registration requests to update the attempt count.  We previously
     * did this in SQL on the update, but as400 v5 didn't like that
     */
    boolean foundOne = false;
    List<RegistrationRequest> requests = getRegistrationRequests(true);
    for (RegistrationRequest registrationRequest : requests) {
        if (registrationRequest.getNodeGroupId().equals(request.getNodeGroupId())
                && registrationRequest.getExternalId().equals(request.getExternalId())) {
            request.setAttemptCount(registrationRequest.getAttemptCount() + 1);
            foundOne = true;
            break;
        }
    }
    String externalId = request.getExternalId() == null ? "" : request.getExternalId();
    String nodeGroupId = request.getNodeGroupId() == null ? "" : request.getNodeGroupId();
    int count = 0;
    if (foundOne) {
        count = sqlTemplate.update(getSql("updateRegistrationRequestSql"),
                new Object[] { request.getAttemptCount(), request.getLastUpdateBy(),
                        request.getLastUpdateTime(), request.getRegisteredNodeId(), request.getStatus().name(),
                        request.getErrorMessage(), nodeGroupId, externalId, request.getIpAddress(),
                        request.getHostName() },
                new int[] { Types.NUMERIC, Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR,
                        Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
    }

    if (count == 0) {
        sqlTemplate.update(getSql("insertRegistrationRequestSql"),
                new Object[] { request.getLastUpdateBy(), request.getLastUpdateTime(),
                        request.getRegisteredNodeId(), request.getStatus().name(), nodeGroupId, externalId,
                        request.getIpAddress(), request.getHostName(), request.getErrorMessage() },
                new int[] { Types.VARCHAR, Types.DATE, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
                        Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR });
    }

}

From source file:architecture.ee.web.community.page.dao.jdbc.JdbcPageDao.java

private void insertPageBody(final Page page) {
    long bodyId = -1L;
    if (page.getBodyText() != null) {
        boolean newBodyRequired = false;
        if (page.getVersionId() == 1) {
            newBodyRequired = true;/*  w w  w  . j a v  a  2  s  . c om*/
        } else {
            // load body from database ...
            List<BodyContent> results = getExtendedJdbcTemplate().query(
                    getBoundSql("ARCHITECTURE_COMMUNITY.SELECT_PAGE_BODY").getSql(), bodyContentMapper,
                    new SqlParameterValue(Types.NUMERIC, page.getPageId()),
                    new SqlParameterValue(Types.NUMERIC, Integer.valueOf(page.getVersionId() - 1)));
            String preTextBody = null;
            for (BodyContent bodyContent : results) {
                bodyId = bodyContent.getBodyId();
                preTextBody = bodyContent.getBodyText();
            }
            String textBody = page.getBodyText();
            if (preTextBody == null || !preTextBody.equals(textBody))
                newBodyRequired = true;
        }

        if (newBodyRequired) {
            bodyId = getNextId("PAGE_BODY");
            getExtendedJdbcTemplate().update(getBoundSql("ARCHITECTURE_COMMUNITY.INSERT_PAGE_BODY").getSql(),
                    new SqlParameterValue(Types.NUMERIC, bodyId),
                    new SqlParameterValue(Types.NUMERIC, page.getPageId()),
                    new SqlParameterValue(Types.NUMERIC, page.getBodyContent().getBodyType().getId()),
                    new SqlParameterValue(Types.CLOB, new SqlLobValue(page.getBodyText(), getLobHandler())));
            getExtendedJdbcTemplate().update(
                    getBoundSql("ARCHITECTURE_COMMUNITY.INSERT_PAGE_BODY_VERSION").getSql(),
                    new SqlParameterValue(Types.NUMERIC, bodyId),
                    new SqlParameterValue(Types.NUMERIC, page.getPageId()),
                    new SqlParameterValue(Types.NUMERIC, page.getVersionId()));
        }
    }
}