Example usage for java.sql Types DECIMAL

List of usage examples for java.sql Types DECIMAL

Introduction

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

Prototype

int DECIMAL

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

Click Source Link

Document

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

Usage

From source file:org.netflux.core.FieldMetadata.java

/**
 * Returns a string representation of the provided type.
 * /* w  w  w.j a  v a  2 s. c  o m*/
 * @param type the type from {@link Types} to transform to a string.
 * @return a string representation of the provided type.
 */
public static String typeToString(int type) {
    switch (type) {
    case Types.ARRAY:
        return "ARRAY";

    case Types.BIGINT:
        return "BIGINT";

    case Types.BINARY:
        return "BINARY";

    case Types.BIT:
        return "BIT";

    case Types.BLOB:
        return "BLOB";

    case Types.BOOLEAN:
        return "BOOLEAN";

    case Types.CHAR:
        return "CHAR";

    case Types.CLOB:
        return "CLOB";

    case Types.DATALINK:
        return "DATALINK";

    case Types.DATE:
        return "DATE";

    case Types.DECIMAL:
        return "DECIMAL";

    case Types.DISTINCT:
        return "DISTINCT";

    case Types.DOUBLE:
        return "DOUBLE";

    case Types.FLOAT:
        return "FLOAT";

    case Types.INTEGER:
        return "INTEGER";

    case Types.JAVA_OBJECT:
        return "JAVA_OBJECT";

    case Types.LONGVARBINARY:
        return "LONGVARBINARY";

    case Types.LONGVARCHAR:
        return "LONGVARCHAR";

    case Types.NULL:
        return "NULL";

    case Types.NUMERIC:
        return "NUMERIC";

    case Types.OTHER:
        return "OTHER";

    case Types.REAL:
        return "REAL";

    case Types.REF:
        return "REF";

    case Types.SMALLINT:
        return "SMALLINT";

    case Types.STRUCT:
        return "STRUCT";

    case Types.TIME:
        return "TIME";

    case Types.TIMESTAMP:
        return "TIMESTAMP";

    case Types.TINYINT:
        return "TINYINT";

    case Types.VARBINARY:
        return "VARBINARY";

    case Types.VARCHAR:
        return "VARCHAR";

    default:
        return "*unsupported type*";
    }
}

From source file:org.jumpmind.metl.core.runtime.component.Sorter.java

protected void createDatabase() {
    if (databasePlatform == null) {
        ResettableBasicDataSource ds = new ResettableBasicDataSource();
        ds.setDriverClassName(Driver.class.getName());
        ds.setMaxActive(1);//from  w  w w.java 2  s .  co  m
        ds.setInitialSize(1);
        ds.setMinIdle(1);
        ds.setMaxIdle(1);
        databaseName = UUID.randomUUID().toString();
        ds.setUrl("jdbc:h2:mem:" + databaseName);
        databasePlatform = JdbcDatabasePlatformFactory.createNewPlatformInstance(ds, new SqlTemplateSettings(),
                true, false);

        Model inputModel = context.getFlowStep().getComponent().getInputModel();
        List<ModelEntity> entities = inputModel.getModelEntities();
        for (ModelEntity entity : entities) {
            Table table = new Table();
            table.setName(entity.getName() + "_1");
            List<ModelAttribute> attributes = entity.getModelAttributes();
            for (ModelAttribute attribute : attributes) {
                DataType dataType = attribute.getDataType();
                Column column = new Column(attribute.getName());
                if (dataType.isNumeric()) {
                    column.setTypeCode(Types.DECIMAL);
                } else if (dataType.isBoolean()) {
                    column.setTypeCode(Types.BOOLEAN);
                } else if (dataType.isTimestamp()) {
                    column.setTypeCode(Types.TIMESTAMP);
                } else if (dataType.isBinary()) {
                    column.setTypeCode(Types.BLOB);
                } else {
                    column.setTypeCode(Types.LONGVARCHAR);
                }

                column.setPrimaryKey(attribute.isPk());
                table.addColumn(column);
            }

            alterCaseToMatchLogicalCase(table);
            databasePlatform.createTables(false, false, table);
        }

        log(LogLevel.INFO, "Creating databasePlatform with the following url: %s", ds.getUrl());
    }
}

From source file:org.apache.ddlutils.model.Column.java

/**
 * {@inheritDoc}/*ww w.ja  v  a2  s  .c  om*/
 */
public boolean equals(Object obj) {
    if (obj instanceof Column) {
        Column other = (Column) obj;
        EqualsBuilder comparator = new EqualsBuilder();

        // Note that this compares case sensitive
        comparator.append(_name, other._name);
        comparator.append(_primaryKey, other._primaryKey);
        comparator.append(_required, other._required);
        comparator.append(_autoIncrement, other._autoIncrement);
        comparator.append(_typeCode, other._typeCode);
        comparator.append(getParsedDefaultValue(), other.getParsedDefaultValue());

        // comparing the size makes only sense for types where it is relevant
        if ((_typeCode == Types.NUMERIC) || (_typeCode == Types.DECIMAL)) {
            comparator.append(_size, other._size);
            comparator.append(_scale, other._scale);
        } else if ((_typeCode == Types.CHAR) || (_typeCode == Types.VARCHAR) || (_typeCode == Types.BINARY)
                || (_typeCode == Types.VARBINARY)) {
            comparator.append(_size, other._size);
        }

        return comparator.isEquals();
    } else {
        return false;
    }
}

From source file:com.tesora.dve.db.NativeType.java

public boolean isNumericType() {
    return dataType == Types.BIGINT || dataType == Types.DECIMAL || dataType == Types.DOUBLE
            || dataType == Types.FLOAT || dataType == Types.INTEGER || dataType == Types.NUMERIC
            || dataType == Types.REAL || dataType == Types.TINYINT || dataType == Types.SMALLINT;
}

From source file:org.springframework.jdbc.core.StatementCreatorUtils.java

private static void setValue(PreparedStatement ps, int paramIndex, int sqlType, @Nullable String typeName,
        @Nullable Integer scale, Object inValue) throws SQLException {

    if (inValue instanceof SqlTypeValue) {
        ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName);
    } else if (inValue instanceof SqlValue) {
        ((SqlValue) inValue).setValue(ps, paramIndex);
    } else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR) {
        ps.setString(paramIndex, inValue.toString());
    } else if (sqlType == Types.NVARCHAR || sqlType == Types.LONGNVARCHAR) {
        ps.setNString(paramIndex, inValue.toString());
    } else if ((sqlType == Types.CLOB || sqlType == Types.NCLOB) && isStringValue(inValue.getClass())) {
        String strVal = inValue.toString();
        if (strVal.length() > 4000) {
            // Necessary for older Oracle drivers, in particular when running against an Oracle 10 database.
            // Should also work fine against other drivers/databases since it uses standard JDBC 4.0 API.
            if (sqlType == Types.NCLOB) {
                ps.setNClob(paramIndex, new StringReader(strVal), strVal.length());
            } else {
                ps.setClob(paramIndex, new StringReader(strVal), strVal.length());
            }/*from w w w  .  j ava 2s .c  om*/
            return;
        } else {
            // Fallback: setString or setNString binding
            if (sqlType == Types.NCLOB) {
                ps.setNString(paramIndex, strVal);
            } else {
                ps.setString(paramIndex, strVal);
            }
        }
    } else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
        if (inValue instanceof BigDecimal) {
            ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
        } else if (scale != null) {
            ps.setObject(paramIndex, inValue, sqlType, scale);
        } else {
            ps.setObject(paramIndex, inValue, sqlType);
        }
    } else if (sqlType == Types.BOOLEAN) {
        if (inValue instanceof Boolean) {
            ps.setBoolean(paramIndex, (Boolean) inValue);
        } else {
            ps.setObject(paramIndex, inValue, Types.BOOLEAN);
        }
    } else if (sqlType == Types.DATE) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Date) {
                ps.setDate(paramIndex, (java.sql.Date) inValue);
            } else {
                ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.DATE);
        }
    } else if (sqlType == Types.TIME) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Time) {
                ps.setTime(paramIndex, (java.sql.Time) inValue);
            } else {
                ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIME);
        }
    } else if (sqlType == Types.TIMESTAMP) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Timestamp) {
                ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue);
            } else {
                ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
        }
    } else if (sqlType == SqlTypeValue.TYPE_UNKNOWN || (sqlType == Types.OTHER
            && "Oracle".equals(ps.getConnection().getMetaData().getDatabaseProductName()))) {
        if (isStringValue(inValue.getClass())) {
            ps.setString(paramIndex, inValue.toString());
        } else if (isDateValue(inValue.getClass())) {
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            // Fall back to generic setObject call without SQL type specified.
            ps.setObject(paramIndex, inValue);
        }
    } else {
        // Fall back to generic setObject call with SQL type specified.
        ps.setObject(paramIndex, inValue, sqlType);
    }
}

From source file:jeeves.resources.dbms.Dbms.java

private Element buildElement(ResultSet rs, int col, String name, int type, Hashtable<String, String> formats)
        throws SQLException {
    String value = null;//from   w w w . ja v  a  2 s.  com

    switch (type) {
    case Types.DATE:
        Date date = rs.getDate(col + 1);
        if (date == null)
            value = null;
        else {
            String format = formats.get(name);
            SimpleDateFormat df = (format == null) ? new SimpleDateFormat(DEFAULT_DATE_FORMAT)
                    : new SimpleDateFormat(format);
            value = df.format(date);
        }
        break;

    case Types.TIME:
        Time time = rs.getTime(col + 1);
        if (time == null)
            value = null;
        else {
            String format = formats.get(name);
            SimpleDateFormat df = (format == null) ? new SimpleDateFormat(DEFAULT_TIME_FORMAT)
                    : new SimpleDateFormat(format);
            value = df.format(time);
        }
        break;

    case Types.TIMESTAMP:
        Timestamp timestamp = rs.getTimestamp(col + 1);
        if (timestamp == null)
            value = null;
        else {
            String format = formats.get(name);
            SimpleDateFormat df = (format == null) ? new SimpleDateFormat(DEFAULT_TIMESTAMP_FORMAT)
                    : new SimpleDateFormat(format);
            value = df.format(timestamp);
        }
        break;

    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.INTEGER:
    case Types.BIGINT:
        long l = rs.getLong(col + 1);
        if (rs.wasNull())
            value = null;
        else {
            String format = formats.get(name);
            if (format == null)
                value = l + "";
            else {
                DecimalFormat df = new DecimalFormat(format);
                value = df.format(l);
            }
        }
        break;

    case Types.DECIMAL:
    case Types.FLOAT:
    case Types.DOUBLE:
    case Types.REAL:
    case Types.NUMERIC:
        double n = rs.getDouble(col + 1);

        if (rs.wasNull())
            value = null;
        else {
            String format = formats.get(name);

            if (format == null) {
                value = n + "";

                // --- this fix is mandatory for oracle
                // --- that shit returns integers like xxx.0

                if (value.endsWith(".0"))
                    value = value.substring(0, value.length() - 2);
            } else {
                DecimalFormat df = new DecimalFormat(format);
                value = df.format(n);
            }
        }
        break;

    default:
        value = rs.getString(col + 1);
        if (value != null) {
            value = stripIllegalChars(value);
        }

        break;
    }
    return new Element(name).setText(value);
}

From source file:org.springframework.jdbc.core.metadata.GenericTableMetaDataProvider.java

/**
 * Method supporting the metadata processing for a table's columns
 *///from   w  ww.  j  a  va 2s.  c om
private void processTableColumns(DatabaseMetaData databaseMetaData, TableMetaData tmd) {
    ResultSet tableColumns = null;
    String metaDataCatalogName = metaDataCatalogNameToUse(tmd.getCatalogName());
    String metaDataSchemaName = metaDataSchemaNameToUse(tmd.getSchemaName());
    String metaDataTableName = tableNameToUse(tmd.getTableName());
    if (logger.isDebugEnabled()) {
        logger.debug("Retrieving metadata for " + metaDataCatalogName + '/' + metaDataSchemaName + '/'
                + metaDataTableName);
    }
    try {
        tableColumns = databaseMetaData.getColumns(metaDataCatalogName, metaDataSchemaName, metaDataTableName,
                null);
        while (tableColumns.next()) {
            String columnName = tableColumns.getString("COLUMN_NAME");
            int dataType = tableColumns.getInt("DATA_TYPE");
            if (dataType == Types.DECIMAL) {
                String typeName = tableColumns.getString("TYPE_NAME");
                int decimalDigits = tableColumns.getInt("DECIMAL_DIGITS");
                // Override a DECIMAL data type for no-decimal numerics
                // (this is for better Oracle support where there have been issues
                // using DECIMAL for certain inserts (see SPR-6912))
                if ("NUMBER".equals(typeName) && decimalDigits == 0) {
                    dataType = Types.NUMERIC;
                    if (logger.isDebugEnabled()) {
                        logger.debug("Overriding metadata: " + columnName + " now NUMERIC instead of DECIMAL");
                    }
                }
            }
            boolean nullable = tableColumns.getBoolean("NULLABLE");
            TableParameterMetaData meta = new TableParameterMetaData(columnName, dataType, nullable);
            this.tableParameterMetaData.add(meta);
            if (logger.isDebugEnabled()) {
                logger.debug("Retrieved metadata: " + meta.getParameterName() + " " + meta.getSqlType() + " "
                        + meta.isNullable());
            }
        }
    } catch (SQLException ex) {
        if (logger.isWarnEnabled()) {
            logger.warn("Error while retrieving metadata for table columns: " + ex.getMessage());
        }
    } finally {
        JdbcUtils.closeResultSet(tableColumns);
    }
}

From source file:org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplateTests.java

public void testQueryWithRowMapper() throws SQLException {
    mockResultSet.next();// w  w w  .j a va 2s .co m
    ctrlResultSet.setReturnValue(true);
    mockResultSet.getInt("id");
    ctrlResultSet.setReturnValue(1);
    mockResultSet.getString("forename");
    ctrlResultSet.setReturnValue("rod");
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    mockPreparedStatement.setObject(1, new Integer(1), Types.DECIMAL);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.setString(2, "UK");
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeQuery();
    ctrlPreparedStatement.setReturnValue(mockResultSet);
    if (debugEnabled) {
        mockPreparedStatement.getWarnings();
        ctrlPreparedStatement.setReturnValue(null);
    }
    mockPreparedStatement.close();
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(SELECT_NAMED_PARAMETERS_PARSED);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    replay();

    NamedParameterJdbcTemplate jt = new NamedParameterJdbcTemplate(mockDataSource);
    Map params = new HashMap();
    params.put("id", new SqlParameterValue(Types.DECIMAL, new Integer(1)));
    params.put("country", "UK");
    List customers = jt.query(SELECT_NAMED_PARAMETERS, params, new RowMapper() {
        public Object mapRow(ResultSet rs, int rownum) throws SQLException {
            Customer cust = new Customer();
            cust.setId(rs.getInt(COLUMN_NAMES[0]));
            cust.setForename(rs.getString(COLUMN_NAMES[1]));
            return cust;
        }
    });
    assertEquals(1, customers.size());
    Customer cust = (Customer) customers.get(0);
    assertTrue("Customer id was assigned correctly", cust.getId() == 1);
    assertTrue("Customer forename was assigned correctly", cust.getForename().equals("rod"));
}

From source file:org.hxzon.util.db.springjdbc.StatementCreatorUtils.java

private static void setValue(PreparedStatement ps, int paramIndex, int sqlType, String typeName, Integer scale,
        Object inValue) throws SQLException {

    if (inValue instanceof SqlTypeValue) {
        ((SqlTypeValue) inValue).setTypeValue(ps, paramIndex, sqlType, typeName);
    } else if (inValue instanceof SqlValue) {
        ((SqlValue) inValue).setValue(ps, paramIndex);
    } else if (sqlType == Types.VARCHAR || sqlType == Types.LONGVARCHAR
            || (sqlType == Types.CLOB && isStringValue(inValue.getClass()))) {
        ps.setString(paramIndex, inValue.toString());
    } else if (sqlType == Types.DECIMAL || sqlType == Types.NUMERIC) {
        if (inValue instanceof BigDecimal) {
            ps.setBigDecimal(paramIndex, (BigDecimal) inValue);
        } else if (scale != null) {
            ps.setObject(paramIndex, inValue, sqlType, scale);
        } else {/*  w w w .  j  av a2s  . c  o  m*/
            ps.setObject(paramIndex, inValue, sqlType);
        }
    } else if (sqlType == Types.DATE) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Date) {
                ps.setDate(paramIndex, (java.sql.Date) inValue);
            } else {
                ps.setDate(paramIndex, new java.sql.Date(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setDate(paramIndex, new java.sql.Date(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.DATE);
        }
    } else if (sqlType == Types.TIME) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Time) {
                ps.setTime(paramIndex, (java.sql.Time) inValue);
            } else {
                ps.setTime(paramIndex, new java.sql.Time(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTime(paramIndex, new java.sql.Time(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIME);
        }
    } else if (sqlType == Types.TIMESTAMP) {
        if (inValue instanceof java.util.Date) {
            if (inValue instanceof java.sql.Timestamp) {
                ps.setTimestamp(paramIndex, (java.sql.Timestamp) inValue);
            } else {
                ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
            }
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            ps.setObject(paramIndex, inValue, Types.TIMESTAMP);
        }
    } else if (sqlType == SqlTypeValue.TYPE_UNKNOWN) {
        if (isStringValue(inValue.getClass())) {
            ps.setString(paramIndex, inValue.toString());
        } else if (isDateValue(inValue.getClass())) {
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(((java.util.Date) inValue).getTime()));
        } else if (inValue instanceof Calendar) {
            Calendar cal = (Calendar) inValue;
            ps.setTimestamp(paramIndex, new java.sql.Timestamp(cal.getTime().getTime()), cal);
        } else {
            // Fall back to generic setObject call without SQL type specified.
            ps.setObject(paramIndex, inValue);
        }
    } else {
        // Fall back to generic setObject call with SQL type specified.
        ps.setObject(paramIndex, inValue, sqlType);
    }
}

From source file:com.tesora.dve.db.NativeType.java

public boolean isDecimalType() {
    return dataType == Types.DECIMAL || dataType == Types.NUMERIC;
}