Example usage for java.sql Types SMALLINT

List of usage examples for java.sql Types SMALLINT

Introduction

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

Prototype

int SMALLINT

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

Click Source Link

Document

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

Usage

From source file:org.apache.ojb.broker.metadata.FieldTypeClasses.java

/**
 * Returns a {@link FieldType} instance for the given sql type
 * (see {@link java.sql.Types}) as specified in JDBC 3.0 specification
 * (see JDBC 3.0 specification <em>Appendix B, Data Type Conversion Tables</em>).
 *
 * @param jdbcType Specify the type to look for.
 * @return A new specific {@link FieldType} instance.
 *//*from   ww  w  . j ava2  s  . c  o m*/
static FieldType newFieldType(JdbcType jdbcType) {
    FieldType result = null;
    switch (jdbcType.getType()) {
    case Types.ARRAY:
        result = new ArrayFieldType();
        break;
    case Types.BIGINT:
        result = new LongFieldType();
        break;
    case Types.BINARY:
        result = new ByteArrayFieldType();
        break;
    case Types.BIT:
        result = new BooleanFieldType();
        break;
    case Types.BLOB:
        result = new BlobFieldType();
        break;
    case Types.CHAR:
        result = new StringFieldType();
        break;
    case Types.CLOB:
        result = new ClobFieldType();
        break;
    case Types.DATE:
        result = new DateFieldType();
        break;
    case Types.DECIMAL:
        result = new BigDecimalFieldType();
        break;
    // Not needed, user have to use the underlying sql datatype in OJB mapping files
    //            case Types.DISTINCT:
    //                result = new DistinctFieldType();
    //                break;
    case Types.DOUBLE:
        result = new DoubleFieldType();
        break;
    case Types.FLOAT:
        result = new FloatFieldType();
        break;
    case Types.INTEGER:
        result = new IntegerFieldType();
        break;
    case Types.JAVA_OBJECT:
        result = new JavaObjectFieldType();
        break;
    case Types.LONGVARBINARY:
        result = new ByteArrayFieldType();
        break;
    case Types.LONGVARCHAR:
        result = new StringFieldType();
        break;
    case Types.NUMERIC:
        result = new BigDecimalFieldType();
        break;
    case Types.REAL:
        result = new FloatFieldType();
        break;
    case Types.REF:
        result = new RefFieldType();
        break;
    case Types.SMALLINT:
        result = new ShortFieldType();
        break;
    case Types.STRUCT:
        result = new StructFieldType();
        break;
    case Types.TIME:
        result = new TimeFieldType();
        break;
    case Types.TIMESTAMP:
        result = new TimestampFieldType();
        break;
    case Types.TINYINT:
        result = new ByteFieldType();
        break;
    case Types.VARBINARY:
        result = new ByteArrayFieldType();
        break;
    case Types.VARCHAR:
        result = new StringFieldType();
        break;
    case Types.OTHER:
        result = new JavaObjectFieldType();
        break;
    //
    //            case Types.NULL:
    //                result = new NullFieldType();
    //                break;

    //#ifdef JDBC30
    case Types.BOOLEAN:
        result = new BooleanFieldType();
        break;
    case Types.DATALINK:
        result = new URLFieldType();
        break;
    //#endif
    default:
        throw new OJBRuntimeException("Unkown or not supported field type specified, specified jdbc type was '"
                + jdbcType + "', as string: " + JdbcTypesHelper.getSqlTypeAsString(jdbcType.getType()));
    }
    // make sure that the sql type was set
    result.setSqlType(jdbcType);
    return result;
}

From source file:org.apache.kylin.jdbc.KylinClient.java

@SuppressWarnings("rawtypes")
public static Class convertType(int sqlType) {
    Class result = Object.class;

    switch (sqlType) {
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        result = String.class;
        break;/*  w  ww  .j  a v a 2  s . c o m*/
    case Types.NUMERIC:
    case Types.DECIMAL:
        result = BigDecimal.class;
        break;
    case Types.BIT:
        result = Boolean.class;
        break;
    case Types.TINYINT:
        result = Byte.class;
        break;
    case Types.SMALLINT:
        result = Short.class;
        break;
    case Types.INTEGER:
        result = Integer.class;
        break;
    case Types.BIGINT:
        result = Long.class;
        break;
    case Types.REAL:
    case Types.FLOAT:
    case Types.DOUBLE:
        result = Double.class;
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
        result = Byte[].class;
        break;
    case Types.DATE:
        result = Date.class;
        break;
    case Types.TIME:
        result = Time.class;
        break;
    case Types.TIMESTAMP:
        result = Timestamp.class;
        break;
    default:
        //do nothing
        break;
    }

    return result;
}

From source file:com.tesora.dve.common.TestDataGenerator.java

protected Object getColumnValue(ColumnMetadata cm) {
    Object cv = null;// w  ww  . ja  va  2  s .  co  m
    Calendar cal = Calendar.getInstance();

    switch (cm.getDataType()) {
    case Types.BIT:
    case Types.BOOLEAN:
        cv = Boolean.TRUE;
        break;
    case Types.BIGINT:
        cv = Long.MAX_VALUE;
        break;
    case Types.CHAR:
    case Types.VARCHAR:
        cv = StringUtils.left(baseString, cm.getSize());
        break;
    case Types.SMALLINT:
        cv = Short.MAX_VALUE;
        break;
    case Types.TINYINT:
        cv = Byte.MAX_VALUE;
        break;
    case Types.INTEGER:
        cv = Integer.MAX_VALUE;
        break;
    case Types.DOUBLE:
        cv = new Double(1234.5678); // TODO need to handle s,p
        break;
    case Types.FLOAT:
        cv = new Float(123.56); // TODO need to handle s,p
        break;
    case Types.DECIMAL:
        cv = new BigDecimal("12345.6789"); // TODO need to handle s,p
        break;
    case Types.DATE:
        cal.setTimeInMillis(123456789);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cv = cal.getTime();
        break;
    case Types.TIMESTAMP:
        cal.setTimeInMillis(123456789);
        cv = cal.getTime();
        break;
    case Types.TIME:
        cv = new Time(123456789);
        break;
    default:
        break;
    }

    return cv;
}

From source file:org.castor.cpa.persistence.sql.keygen.TableKeyGenerator.java

private void assertNumericSqlType(final int sqlType) throws MappingException {
    switch (sqlType) {
    case Types.BIGINT:
    case Types.SMALLINT:
    case Types.INTEGER:
    case Types.DECIMAL:
    case Types.NUMERIC:
        break;// w  w w.ja  v  a 2s  . com
    default:
        String msg = Messages.format("mapping.keyGenSQLType", getClass().getName(), sqlType);
        throw new MappingException(msg);
    }
}

From source file:au.com.ish.derbydump.derbydump.metadata.Column.java

/**
 * Get a string value for the value in this column in the datarow
 * /*  w w w .java  2 s.c  o  m*/
 * @param dataRow The row which we are exporting
 * @return an SQL statement compliant string version of the value
 */
public String toString(ResultSet dataRow) throws SQLException {

    switch (getColumnDataType()) {
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.BLOB: {
        Blob obj = dataRow.getBlob(columnName);
        return (obj == null) ? "NULL" : processBinaryData(obj);
    }

    case Types.CLOB: {
        Clob obj = dataRow.getClob(columnName);
        return (obj == null) ? "NULL" : processClobData(obj);
    }

    case Types.CHAR:
    case Types.LONGNVARCHAR:
    case Types.VARCHAR: {
        String obj = dataRow.getString(columnName);
        return (obj == null) ? "NULL" : processStringData(obj);
    }

    case Types.TIME: {
        Time obj = dataRow.getTime(columnName);
        return (obj == null) ? "NULL" : processStringData(obj.toString());
    }

    case Types.DATE: {
        Date obj = dataRow.getDate(columnName);
        return (obj == null) ? "NULL" : processStringData(obj.toString());
    }

    case Types.TIMESTAMP: {
        Timestamp obj = dataRow.getTimestamp(columnName);
        return (obj == null) ? "NULL" : processStringData(obj.toString());
    }

    case Types.SMALLINT: {
        Object obj = dataRow.getObject(columnName);
        return (obj == null) ? "NULL" : obj.toString();
    }

    case Types.BIGINT: {
        Object obj = dataRow.getObject(columnName);
        return (obj == null) ? "NULL" : obj.toString();
    }

    case Types.INTEGER: {
        Object obj = dataRow.getObject(columnName);
        return (obj == null) ? "NULL" : obj.toString();
    }

    case Types.NUMERIC:
    case Types.DECIMAL: {
        BigDecimal obj = dataRow.getBigDecimal(columnName);
        return (obj == null) ? "NULL" : String.valueOf(obj);
    }

    case Types.REAL:
    case Types.FLOAT: {
        Float obj = dataRow.getFloat(columnName);
        // dataRow.getFloat() always returns a value. only way to check the null is wasNull() method
        return (dataRow.wasNull()) ? "NULL" : String.valueOf(obj);
    }

    case Types.DOUBLE: {
        Double obj = dataRow.getDouble(columnName);
        return (dataRow.wasNull()) ? "NULL" : String.valueOf(obj);
    }

    default: {
        Object obj = dataRow.getObject(columnName);
        return (obj == null) ? "NULL" : obj.toString();
    }
    }
}

From source file:com.squid.core.jdbc.formatter.DefaultJDBCDataFormatter.java

public String formatJDBCObject(final Object jdbcObject, final int colType) throws SQLException {
    String value = "";

    switch (colType) {
    case Types.BIT:
        if (jdbcObject != null) {
            value = String.valueOf(jdbcObject);
        }// w  w  w . j  av a 2 s .  c  om
        break;
    case Types.BOOLEAN:
        if (jdbcObject != null) {
            value = new Boolean(jdbcObject.toString()).toString();
        }
        break;
    case Types.BIGINT:
    case Types.DECIMAL:
    case Types.DOUBLE:
    case Types.FLOAT:
    case Types.REAL:
    case Types.NUMERIC:
        if (jdbcObject != null) {
            value = "" + formatter.formatDecimal(jdbcObject);
        }
        break;
    case Types.INTEGER:
    case Types.TINYINT:
    case Types.SMALLINT:
        if (jdbcObject != null) {
            value = "" + formatter.formatDecimal(jdbcObject);
        }
        break;
    case Types.CLOB:
        if (jdbcObject != null) {
            try {
                value = read((Clob) jdbcObject);
            } catch (SQLException sqle) {
                value = "";
            } catch (IOException ioe) {
                value = "";
            }
        }
        break;
    case Types.ARRAY:
        try {
            ResultSet rs = ((Array) jdbcObject).getResultSet();
            int arrayType = ((Array) jdbcObject).getBaseType();
            boolean isNotNew = false;
            while (rs.next()) {
                String current = formatJDBCObject(rs.getObject(2), arrayType);
                if ("".equals(current.trim()) == false) {
                    if (isNotNew) {
                        value = value + ",";
                    } else {
                        isNotNew = !isNotNew;
                    }
                    value = value + current;
                }
            }
            if ("".equals(value) == false) {
                value = "{" + value + "}";
            }
        } catch (SQLException sqle) {
            value = "";
        }
        break;
    case Types.JAVA_OBJECT:
        if (jdbcObject != null) {
            value = String.valueOf(jdbcObject);
        }
        break;
    case Types.DATE:
        if (jdbcObject != null) {
            value = formatter.formatDate(jdbcObject);
        }
        break;
    case Types.TIME:
        if (jdbcObject != null) {
            value = ((Time) jdbcObject).toString();
        }
        break;
    case Types.TIMESTAMP:
        if (jdbcObject != null) {
            value = formatter.formatTimestamp(jdbcObject);
        }
        break;
    case Types.LONGVARCHAR:
    case Types.VARCHAR:
    case Types.CHAR:
        if (jdbcObject != null) {
            value = jdbcObject.toString();
        } else {
            value = "NULL";
        }
        break;
    case Types.BINARY:
        value = new String((byte[]) jdbcObject);
        break;
    default:
        value = "";
    }
    if (value == null) {
        value = "";
    }
    return value;
}

From source file:org.apache.syncope.core.persistence.jpa.content.ContentLoaderHandler.java

private Object[] getParameters(final String tableName, final Attributes attrs) {
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

    Map<String, Integer> colTypes = jdbcTemplate.query("SELECT * FROM " + tableName + " WHERE 0=1",
            new ResultSetExtractor<Map<String, Integer>>() {

                @Override/*from  ww  w. ja v a2s  . c  o  m*/
                public Map<String, Integer> extractData(final ResultSet rs) throws SQLException {
                    Map<String, Integer> colTypes = new HashMap<>();
                    for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
                        colTypes.put(rs.getMetaData().getColumnName(i).toUpperCase(),
                                rs.getMetaData().getColumnType(i));
                    }
                    return colTypes;
                }
            });

    Object[] parameters = new Object[attrs.getLength()];
    for (int i = 0; i < attrs.getLength(); i++) {
        Integer colType = colTypes.get(attrs.getQName(i).toUpperCase());
        if (colType == null) {
            LOG.warn("No column type found for {}", attrs.getQName(i).toUpperCase());
            colType = Types.VARCHAR;
        }

        switch (colType) {
        case Types.INTEGER:
        case Types.TINYINT:
        case Types.SMALLINT:
            try {
                parameters[i] = Integer.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Integer '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.NUMERIC:
        case Types.DECIMAL:
        case Types.BIGINT:
            try {
                parameters[i] = Long.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Long '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.DOUBLE:
            try {
                parameters[i] = Double.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Double '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.REAL:
        case Types.FLOAT:
            try {
                parameters[i] = Float.valueOf(attrs.getValue(i));
            } catch (NumberFormatException e) {
                LOG.error("Unparsable Float '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.DATE:
        case Types.TIME:
        case Types.TIMESTAMP:
            try {
                parameters[i] = FormatUtils.parseDate(attrs.getValue(i));
            } catch (ParseException e) {
                LOG.error("Unparsable Date '{}'", attrs.getValue(i));
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.BIT:
        case Types.BOOLEAN:
            parameters[i] = "1".equals(attrs.getValue(i)) ? Boolean.TRUE : Boolean.FALSE;
            break;

        case Types.BINARY:
        case Types.VARBINARY:
        case Types.LONGVARBINARY:
            try {
                parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray());
            } catch (DecoderException | IllegalArgumentException e) {
                parameters[i] = attrs.getValue(i);
            }
            break;

        case Types.BLOB:
            try {
                parameters[i] = Hex.decodeHex(attrs.getValue(i).toCharArray());
            } catch (DecoderException | IllegalArgumentException e) {
                LOG.warn("Error decoding hex string to specify a blob parameter", e);
                parameters[i] = attrs.getValue(i);
            } catch (Exception e) {
                LOG.warn("Error creating a new blob parameter", e);
            }
            break;

        default:
            parameters[i] = attrs.getValue(i);
        }
    }

    return parameters;
}

From source file:org.apache.sqoop.mapreduce.db.DataDrivenDBInputFormat.java

/**
 * @return the DBSplitter implementation to use to divide the table/query
 * into InputSplits./*from w w  w.  j  av  a 2s  .co  m*/
 */
protected DBSplitter getSplitter(int sqlDataType) {
    switch (sqlDataType) {
    case Types.NUMERIC:
    case Types.DECIMAL:
        return new BigDecimalSplitter();

    case Types.BIT:
    case Types.BOOLEAN:
        return new BooleanSplitter();

    case Types.INTEGER:
    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.BIGINT:
        return new IntegerSplitter();

    case Types.REAL:
    case Types.FLOAT:
    case Types.DOUBLE:
        return new FloatSplitter();

    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
    case Types.LONGNVARCHAR:
    case Types.NVARCHAR:
    case Types.NCHAR:
        return new TextSplitter();

    case Types.DATE:
    case Types.TIME:
    case Types.TIMESTAMP:
        return new DateSplitter();

    default:
        // TODO: Support BINARY, VARBINARY, LONGVARBINARY, DISTINCT, CLOB,
        // BLOB, ARRAY, STRUCT, REF, DATALINK, and JAVA_OBJECT.
        return null;
    }
}

From source file:com.hangum.tadpole.engine.sql.util.RDBTypeToJavaTypeUtils.java

/**
 * ? ?/*from  www  . ja  v  a2 s .c o  m*/
 * 
 * @param sqlType
 * @return
 */
public static boolean isNumberType(int sqlType) {
    switch (sqlType) {
    case Types.BIGINT:
    case Types.DECIMAL:
    case Types.DOUBLE:
    case Types.FLOAT:
    case Types.INTEGER:
    case Types.NUMERIC:
    case Types.BIT:
    case Types.SMALLINT:
    case Types.TINYINT:
        return true;
    }

    return false;
}

From source file:com.sangupta.fileanalysis.db.DBResultViewer.java

/**
 * View resutls of a {@link ResultSet}./*from   ww w  .jav  a2s .c o  m*/
 * 
 * @param resultSet
 * @throws SQLException 
 */
public void viewResult(ResultSet resultSet) throws SQLException {
    if (resultSet == null) {
        // nothing to do
        return;
    }

    // collect the meta
    ResultSetMetaData meta = resultSet.getMetaData();

    final int numColumns = meta.getColumnCount();
    final int[] displaySizes = new int[numColumns + 1];
    final int[] colType = new int[numColumns + 1];

    for (int index = 1; index <= numColumns; index++) {
        colType[index] = meta.getColumnType(index);
        displaySizes[index] = getColumnSize(meta.getTableName(index), meta.getColumnName(index),
                colType[index]);
    }

    // display the header row
    for (int index = 1; index <= numColumns; index++) {
        center(meta.getColumnLabel(index), displaySizes[index]);
    }
    System.out.println("|");
    for (int index = 1; index <= numColumns; index++) {
        System.out.print("+" + StringUtils.repeat('-', displaySizes[index] + 2));
    }
    System.out.println("+");

    // start iterating over the result set
    int rowsDisplayed = 0;
    int numRecords = 0;
    while (resultSet.next()) {
        // read and display the value
        rowsDisplayed++;
        numRecords++;

        for (int index = 1; index <= numColumns; index++) {
            switch (colType[index]) {
            case Types.DECIMAL:
            case Types.DOUBLE:
            case Types.REAL:
                format(resultSet.getDouble(index), displaySizes[index]);
                continue;

            case Types.INTEGER:
            case Types.SMALLINT:
                format(resultSet.getInt(index), displaySizes[index]);
                continue;

            case Types.VARCHAR:
                format(resultSet.getString(index), displaySizes[index], false);
                continue;

            case Types.TIMESTAMP:
                format(resultSet.getTimestamp(index), displaySizes[index]);
                continue;

            case Types.BIGINT:
                format(resultSet.getBigDecimal(index), displaySizes[index]);
                continue;
            }
        }

        // terminator for row and new line
        System.out.println("|");

        // check for rows displayed
        if (rowsDisplayed == 20) {
            // ask the user if more data needs to be displayed
            String cont = ConsoleUtils.readLine("Type \"it\" for more: ", true);
            if (!"it".equalsIgnoreCase(cont)) {
                break;
            }

            // continue;
            rowsDisplayed = 0;
            continue;
        }
    }

    System.out.println("\nTotal number of records found: " + numRecords);
}