Example usage for java.sql Types TINYINT

List of usage examples for java.sql Types TINYINT

Introduction

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

Prototype

int TINYINT

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

Click Source Link

Document

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

Usage

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;//from  w  ww  . jav a  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.eclipse.ecr.core.storage.sql.extensions.H2Fulltext.java

private static Object asObject(String string, int type) throws SQLException {
    switch (type) {
    case Types.BIGINT:
        return Long.valueOf(string);
    case Types.INTEGER:
    case Types.SMALLINT:
    case Types.TINYINT:
        return Integer.valueOf(string);
    case Types.LONGVARCHAR:
    case Types.CHAR:
    case Types.VARCHAR:
        return string;
    default://from  w  ww. j  ava 2s . c om
        throw new SQLException("Unsupport data type for primary key: " + type);
    }
}

From source file:org.springframework.jdbc.support.JdbcUtils.java

/**
 * Check whether the given SQL type is numeric.
 * @param sqlType the SQL type to be checked
 * @return whether the type is numeric/*  ww w . ja  v  a  2 s .co m*/
 */
public static boolean isNumeric(int sqlType) {
    return Types.BIT == sqlType || Types.BIGINT == sqlType || Types.DECIMAL == sqlType
            || Types.DOUBLE == sqlType || Types.FLOAT == sqlType || Types.INTEGER == sqlType
            || Types.NUMERIC == sqlType || Types.REAL == sqlType || Types.SMALLINT == sqlType
            || Types.TINYINT == sqlType;
}

From source file:com.antsdb.saltedfish.server.mysql.PacketEncoder.java

private boolean writeValueFast(ByteBuf buffer, FieldMeta meta, long pValue) {
    DataType type = meta.getType();// w  w w  .j a va  2  s .co m
    byte format = Value.getFormat(null, pValue);
    if (type.getSqlType() == Types.TINYINT) {
        if (format == Value.FORMAT_INT4) {
            buffer.writeByte(Int4.get(pValue));
            return true;
        } else if (format == Value.FORMAT_INT8) {
            buffer.writeByte((int) Int8.get(null, pValue));
            return true;
        }
    } else if (type.getJavaType() == Boolean.class) {
        boolean b = FishBool.get(null, pValue);
        buffer.writeByte(b ? 1 : 0);
        return true;
    } else if (type.getJavaType() == Integer.class) {
        if (format == Value.FORMAT_INT4) {
            BufferUtils.writeUB4(buffer, Int4.get(pValue));
            return true;
        } else if (format == Value.FORMAT_INT8) {
            BufferUtils.writeUB4(buffer, (int) Int8.get(null, pValue));
            return true;
        }
    } else if (type.getJavaType() == Long.class) {
        if (format == Value.FORMAT_INT4) {
            BufferUtils.writeLongLong(buffer, Int4.get(pValue));
            return true;
        } else if (format == Value.FORMAT_INT8) {
            BufferUtils.writeLongLong(buffer, Int8.get(null, pValue));
            return true;
        }
    } else if (type.getJavaType() == Float.class) {
        if (format == Value.FORMAT_FLOAT4) {
            BufferUtils.writeUB4(buffer, Float.floatToIntBits(Float4.get(null, pValue)));
            return true;
        }
    } else if (type.getJavaType() == Double.class) {
        if (format == Value.FORMAT_FLOAT4) {
            BufferUtils.writeLongLong(buffer, Double.doubleToLongBits(Float8.get(null, pValue)));
            return true;
        }
    }
    return false;
}

From source file:com.alibaba.otter.shared.common.utils.meta.DdlUtils.java

private static Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values)
        throws SQLException {
    Column column = new Column();

    column.setName((String) values.get("COLUMN_NAME"));
    column.setDefaultValue((String) values.get("COLUMN_DEF"));
    column.setTypeCode(((Integer) values.get("DATA_TYPE")).intValue());

    String typeName = (String) values.get("TYPE_NAME");
    // column.setType(typeName);

    if ((typeName != null) && typeName.startsWith("TIMESTAMP")) {
        column.setTypeCode(Types.TIMESTAMP);
    }//from  w w w  . j  av  a 2s.  co m
    // modify 2013-09-25?unsigned
    if ((typeName != null) && StringUtils.containsIgnoreCase(typeName, "UNSIGNED")) {
        // unsigned????
        switch (column.getTypeCode()) {
        case Types.TINYINT:
            column.setTypeCode(Types.SMALLINT);
            break;
        case Types.SMALLINT:
            column.setTypeCode(Types.INTEGER);
            break;
        case Types.INTEGER:
            column.setTypeCode(Types.BIGINT);
            break;
        case Types.BIGINT:
            column.setTypeCode(Types.DECIMAL);
            break;
        default:
            break;
        }
    }

    Integer precision = (Integer) values.get("NUM_PREC_RADIX");

    if (precision != null) {
        column.setPrecisionRadix(precision.intValue());
    }

    String size = (String) values.get("COLUMN_SIZE");

    if (size == null) {
        size = (String) _defaultSizes.get(new Integer(column.getTypeCode()));
    }

    // we're setting the size after the precision and radix in case
    // the database prefers to return them in the size value
    column.setSize(size);

    int scale = 0;
    Object dec_digits = values.get("DECIMAL_DIGITS");

    if (dec_digits instanceof String) {
        scale = (dec_digits == null) ? 0 : NumberUtils.toInt(dec_digits.toString());
    } else if (dec_digits instanceof Integer) {
        scale = (dec_digits == null) ? 0 : (Integer) dec_digits;
    }

    if (scale != 0) {
        column.setScale(scale);
    }

    column.setRequired("NO".equalsIgnoreCase(((String) values.get("IS_NULLABLE")).trim()));
    column.setDescription((String) values.get("REMARKS"));
    return column;
}

From source file:org.jumpmind.symmetric.io.data.DbFill.java

private Object generateRandomValueForColumn(Column column) {
    Object objectValue = null;//w w w.  j a  v a  2 s .  c o  m
    int type = column.getMappedTypeCode();
    if (column.isEnum()) {
        objectValue = column.getEnumValues()[new Random().nextInt(column.getEnumValues().length)];
    } else if (column.isTimestampWithTimezone()) {
        objectValue = String.format("%s %s", FormatUtils.TIMESTAMP_FORMATTER.format(randomDate()),
                AppUtils.getTimezoneOffset());
    } else if (type == Types.DATE) {
        objectValue = DateUtils.truncate(randomDate(), Calendar.DATE);
    } else if (type == Types.TIMESTAMP || type == Types.TIME) {
        objectValue = randomTimestamp();
    } else if (type == Types.INTEGER || type == Types.BIGINT) {
        objectValue = randomInt();
    } else if (type == Types.SMALLINT) {
        objectValue = randomSmallInt(column.getJdbcTypeName().toLowerCase().contains("unsigned"));
    } else if (type == Types.FLOAT) {
        objectValue = randomFloat();
    } else if (type == Types.DOUBLE) {
        objectValue = randomDouble();
    } else if (type == Types.TINYINT) {
        objectValue = randomTinyInt();
    } else if (type == Types.NUMERIC || type == Types.DECIMAL || type == Types.REAL) {
        objectValue = randomBigDecimal(column.getSizeAsInt(), column.getScale());
    } else if (type == Types.BOOLEAN || type == Types.BIT) {
        objectValue = randomBoolean();
    } else if (type == Types.BLOB || type == Types.LONGVARBINARY || type == Types.BINARY
            || type == Types.VARBINARY ||
            // SQLServer text type
            type == -10) {
        objectValue = randomBytes();
    } else if (type == Types.ARRAY) {
        objectValue = null;
    } else if (type == Types.VARCHAR || type == Types.LONGVARCHAR || type == Types.CHAR || type == Types.CLOB) {
        int size = 0;
        // Assume if the size is 0 there is no max size configured.
        if (column.getSizeAsInt() != 0) {
            size = column.getSizeAsInt() > 50 ? 50 : column.getSizeAsInt();
        } else {
            // No max length so default to 50
            size = 50;
        }
        objectValue = randomString(size);
    } else if (type == Types.OTHER) {
        if ("UUID".equalsIgnoreCase(column.getJdbcTypeName())) {
            objectValue = randomUUID();
        }
    }
    return objectValue;
}

From source file:com.cloudera.sqoop.manager.SqlManager.java

/**
 * Resolve a database-specific type to the Java type that should contain it.
 * @param sqlType//from  w w  w.  j  ava2 s.c o m
 * @return the name of a Java type to hold the sql datatype, or null if none.
 */
public String toJavaType(int sqlType) {
    // Mappings taken from:
    // http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
    if (sqlType == Types.INTEGER) {
        return "Integer";
    } else if (sqlType == Types.VARCHAR) {
        return "String";
    } else if (sqlType == Types.CHAR) {
        return "String";
    } else if (sqlType == Types.LONGVARCHAR) {
        return "String";
    } else if (sqlType == Types.NUMERIC) {
        return "java.math.BigDecimal";
    } else if (sqlType == Types.DECIMAL) {
        return "java.math.BigDecimal";
    } else if (sqlType == Types.BIT) {
        return "Boolean";
    } else if (sqlType == Types.BOOLEAN) {
        return "Boolean";
    } else if (sqlType == Types.TINYINT) {
        return "Integer";
    } else if (sqlType == Types.SMALLINT) {
        return "Integer";
    } else if (sqlType == Types.BIGINT) {
        return "Long";
    } else if (sqlType == Types.REAL) {
        return "Float";
    } else if (sqlType == Types.FLOAT) {
        return "Double";
    } else if (sqlType == Types.DOUBLE) {
        return "Double";
    } else if (sqlType == Types.DATE) {
        return "java.sql.Date";
    } else if (sqlType == Types.TIME) {
        return "java.sql.Time";
    } else if (sqlType == Types.TIMESTAMP) {
        return "java.sql.Timestamp";
    } else if (sqlType == Types.BINARY || sqlType == Types.VARBINARY) {
        return BytesWritable.class.getName();
    } else if (sqlType == Types.CLOB) {
        return ClobRef.class.getName();
    } else if (sqlType == Types.BLOB || sqlType == Types.LONGVARBINARY) {
        return BlobRef.class.getName();
    } else {
        // TODO(aaron): Support DISTINCT, ARRAY, STRUCT, REF, JAVA_OBJECT.
        // Return null indicating database-specific manager should return a
        // java data type if it can find one for any nonstandard type.
        return null;
    }
}

From source file:org.apache.empire.db.codegen.CodeGenParser.java

/**
 * converts a SQL DataType to a EmpireDataType
 *///from   w w  w  .  ja va2  s .c o m
private DataType getEmpireDataType(int sqlType) {
    DataType empireType = DataType.UNKNOWN;
    switch (sqlType) {
    case Types.INTEGER:
    case Types.SMALLINT:
    case Types.TINYINT:
    case Types.BIGINT:
        empireType = DataType.INTEGER;
        break;
    case Types.VARCHAR:
        empireType = DataType.TEXT;
        break;
    case Types.DATE:
        empireType = DataType.DATE;
        break;
    case Types.TIMESTAMP:
    case Types.TIME:
        empireType = DataType.DATETIME;
        break;
    case Types.CHAR:
        empireType = DataType.CHAR;
        break;
    case Types.DOUBLE:
    case Types.FLOAT:
    case Types.REAL:
        empireType = DataType.FLOAT;
        break;
    case Types.DECIMAL:
    case Types.NUMERIC:
        empireType = DataType.DECIMAL;
        break;
    case Types.BIT:
    case Types.BOOLEAN:
        empireType = DataType.BOOL;
        break;
    case Types.CLOB:
    case Types.LONGVARCHAR:
        empireType = DataType.CLOB;
        break;
    case Types.BINARY:
    case Types.VARBINARY:
    case Types.LONGVARBINARY:
    case Types.BLOB:
        empireType = DataType.BLOB;
        break;
    default:
        empireType = DataType.UNKNOWN;
        log.warn("SQL column type " + sqlType + " not supported.");
    }
    log.debug("Mapping date type " + String.valueOf(sqlType) + " to " + empireType);
    return empireType;
}

From source file:ch.rgw.tools.JdbcLink.java

public static int generalType(int t) {
    switch (t) {//from w w w  .  j a va 2 s .  c om
    case Types.BIGINT:
    case Types.BIT:
    case Types.BOOLEAN:
    case Types.INTEGER:
    case Types.SMALLINT:
    case Types.TINYINT:
        return INTEGRAL;

    case Types.VARCHAR:
    case Types.CHAR:
    case Types.LONGVARCHAR:
        return TEXT;

    case Types.BINARY:
    case Types.BLOB:
    case Types.CLOB:
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
        return BINARY;

    default:
        return OTHER;

    }
}

From source file:org.executequery.databasemediators.spi.DefaultStatementExecutor.java

/** <p>Executes the specified procedure.
 *
 *  @param  the SQL procedure to execute
 *  @return the query result//from ww w  .  j a  va  2 s.  co  m
 */
public SqlStatementResult execute(DatabaseExecutable databaseExecutable) throws SQLException {

    if (!prepared()) {

        return statementResult;
    }

    ProcedureParameter[] param = databaseExecutable.getParametersArray();
    Arrays.sort(param, new ProcedureParameterSorter());

    String procQuery = null;
    boolean hasOut = false;
    boolean hasParameters = (param != null && param.length > 0);

    List<ProcedureParameter> outs = null;
    List<ProcedureParameter> ins = null;

    if (hasParameters) {

        // split the params into ins and outs
        outs = new ArrayList<ProcedureParameter>();
        ins = new ArrayList<ProcedureParameter>();

        int type = -1;
        for (int i = 0; i < param.length; i++) {
            type = param[i].getType();
            if (type == DatabaseMetaData.procedureColumnIn || type == DatabaseMetaData.procedureColumnInOut) {

                // add to the ins list
                ins.add(param[i]);

            } else if (type == DatabaseMetaData.procedureColumnOut
                    || type == DatabaseMetaData.procedureColumnResult
                    || type == DatabaseMetaData.procedureColumnReturn
                    || type == DatabaseMetaData.procedureColumnUnknown
                    || type == DatabaseMetaData.procedureColumnInOut) {

                // add to the outs list
                outs.add(param[i]);

            }
        }

        char QUESTION_MARK = '?';
        String COMMA = ", ";

        // init the string buffer
        StringBuilder sb = new StringBuilder("{ ");
        if (!outs.isEmpty()) {

            // build the out params place holders
            for (int i = 0, n = outs.size(); i < n; i++) {

                sb.append(QUESTION_MARK);

                if (i < n - 1) {

                    sb.append(COMMA);
                }

            }

            sb.append(" = ");
        }

        sb.append(" call ");

        if (databaseExecutable.supportCatalogOrSchemaInFunctionOrProcedureCalls()) {

            String namePrefix = null;
            if (databaseExecutable.supportCatalogInFunctionOrProcedureCalls()) {

                namePrefix = databaseExecutable.getCatalogName();

            }
            if (databaseExecutable.supportSchemaInFunctionOrProcedureCalls()) {

                namePrefix = databaseExecutable.getSchemaName();

            }

            if (namePrefix != null) {

                sb.append(namePrefix).append('.');
            }
        }

        sb.append(databaseExecutable.getName()).append("( ");

        // build the ins params place holders
        for (int i = 0, n = ins.size(); i < n; i++) {
            sb.append(QUESTION_MARK);
            if (i < n - 1) {
                sb.append(COMMA);
            }
        }

        sb.append(" ) }");

        // determine if we have out params
        hasOut = !(outs.isEmpty());
        procQuery = sb.toString();
    } else {
        StringBuilder sb = new StringBuilder();
        sb.append("{ call ");

        if (databaseExecutable.getSchemaName() != null) {
            sb.append(databaseExecutable.getSchemaName()).append('.');
        }

        sb.append(databaseExecutable.getName()).append("( ) }");

        procQuery = sb.toString();
    }

    //Log.debug(procQuery);

    // null value literal
    String NULL = "null";

    // clear any warnings
    conn.clearWarnings();

    Log.info("Executing: " + procQuery);

    CallableStatement cstmnt = null;
    try {
        // prepare the statement
        cstmnt = conn.prepareCall(procQuery);
        stmnt = cstmnt;
    } catch (SQLException e) {
        handleException(e);
        statementResult.setSqlException(e);
        return statementResult;
    }

    // check if we are passing parameters
    if (hasParameters) {
        // the parameter index counter
        int index = 1;

        // the java.sql.Type value
        int dataType = -1;

        // the parameter input value
        String value = null;

        // register the out params
        for (int i = 0, n = outs.size(); i < n; i++) {
            //Log.debug("setting out at index: " + index);
            cstmnt.registerOutParameter(index, outs.get(i).getDataType());
            index++;
        }

        try {

            // register the in params
            for (int i = 0, n = ins.size(); i < n; i++) {

                ProcedureParameter procedureParameter = ins.get(i);
                value = procedureParameter.getValue();
                dataType = procedureParameter.getDataType();

                // try infer a type if OTHER
                if (dataType == Types.OTHER) {

                    // checking only for bit/bool for now

                    if (isTrueFalse(value)) {

                        dataType = Types.BOOLEAN;

                    } else if (isBit(value)) {

                        dataType = Types.BIT;
                        value = value.substring(2, value.length() - 1);
                    }

                }

                if (MiscUtils.isNull(value) || value.equalsIgnoreCase(NULL)) {

                    cstmnt.setNull(index, dataType);

                } else {

                    switch (dataType) {

                    case Types.TINYINT:
                        byte _byte = Byte.valueOf(value).byteValue();
                        cstmnt.setShort(index, _byte);
                        break;

                    case Types.SMALLINT:
                        short _short = Short.valueOf(value).shortValue();
                        cstmnt.setShort(index, _short);
                        break;

                    case Types.LONGVARCHAR:
                    case Types.CHAR:
                    case Types.VARCHAR:
                        cstmnt.setString(index, value);
                        break;

                    case Types.BIT:
                    case Types.BOOLEAN:

                        boolean _boolean = false;
                        if (NumberUtils.isNumber(value)) {

                            int number = Integer.valueOf(value);
                            if (number > 0) {

                                _boolean = true;
                            }

                        } else {

                            _boolean = Boolean.valueOf(value).booleanValue();
                        }

                        cstmnt.setBoolean(index, _boolean);
                        break;

                    case Types.BIGINT:
                        long _long = Long.valueOf(value).longValue();
                        cstmnt.setLong(index, _long);
                        break;

                    case Types.INTEGER:
                        int _int = Integer.valueOf(value).intValue();
                        cstmnt.setInt(index, _int);
                        break;

                    case Types.REAL:
                        float _float = Float.valueOf(value).floatValue();
                        cstmnt.setFloat(index, _float);
                        break;

                    case Types.NUMERIC:
                    case Types.DECIMAL:
                        cstmnt.setBigDecimal(index, new BigDecimal(value));
                        break;
                    /*
                                      case Types.DATE:
                                      case Types.TIMESTAMP:
                                      case Types.TIME:
                                        cstmnt.setTimestamp(index, new Timestamp( BigDecimal(value));
                    */
                    case Types.FLOAT:
                    case Types.DOUBLE:
                        double _double = Double.valueOf(value).doubleValue();
                        cstmnt.setDouble(index, _double);
                        break;

                    default:
                        cstmnt.setObject(index, value);

                    }

                }

                // increment the index
                index++;
            }

        } catch (Exception e) {

            statementResult.setOtherErrorMessage(e.getClass().getName() + ": " + e.getMessage());
            return statementResult;
        }

    }

    /*
    test creating function for postgres:
            
    CREATE FUNCTION concat_lower_or_upper(a text, b text, uppercase boolean DEFAULT false)
    RETURNS text
    AS
    $$
     SELECT CASE
        WHEN $3 THEN UPPER($1 || ' ' || $2)
        ELSE LOWER($1 || ' ' || $2)
        END;
    $$
    LANGUAGE SQL IMMUTABLE STRICT;
    */

    try {
        cstmnt.clearWarnings();
        boolean hasResultSet = cstmnt.execute();
        Map<String, Object> results = new HashMap<String, Object>();

        if (hasOut) {
            // incrementing index
            int index = 1;

            // return value from each registered out
            String returnValue = null;

            for (int i = 0; i < param.length; i++) {

                int type = param[i].getType();
                int dataType = param[i].getDataType();

                if (type == DatabaseMetaData.procedureColumnOut
                        || type == DatabaseMetaData.procedureColumnResult
                        || type == DatabaseMetaData.procedureColumnReturn
                        || type == DatabaseMetaData.procedureColumnUnknown
                        || type == DatabaseMetaData.procedureColumnInOut) {

                    switch (dataType) {

                    case Types.TINYINT:
                        returnValue = Byte.toString(cstmnt.getByte(index));
                        break;

                    case Types.SMALLINT:
                        returnValue = Short.toString(cstmnt.getShort(index));
                        break;

                    case Types.LONGVARCHAR:
                    case Types.CHAR:
                    case Types.VARCHAR:
                        returnValue = cstmnt.getString(index);
                        break;

                    case Types.BIT:
                    case Types.BOOLEAN:
                        returnValue = Boolean.toString(cstmnt.getBoolean(index));
                        break;

                    case Types.INTEGER:
                        returnValue = Integer.toString(cstmnt.getInt(index));
                        break;

                    case Types.BIGINT:
                        returnValue = Long.toString(cstmnt.getLong(index));
                        break;

                    case Types.REAL:
                        returnValue = Float.toString(cstmnt.getFloat(index));
                        break;

                    case Types.NUMERIC:
                    case Types.DECIMAL:
                        returnValue = cstmnt.getBigDecimal(index).toString();
                        break;

                    case Types.DATE:
                    case Types.TIME:
                    case Types.TIMESTAMP:
                        returnValue = cstmnt.getDate(index).toString();
                        break;

                    case Types.FLOAT:
                    case Types.DOUBLE:
                        returnValue = Double.toString(cstmnt.getDouble(index));
                        break;

                    }

                    if (returnValue == null) {
                        returnValue = "NULL";
                    }

                    results.put(param[i].getName(), returnValue);
                    index++;
                }

            }

        }

        if (!hasResultSet) {

            statementResult.setUpdateCount(cstmnt.getUpdateCount());

        } else {

            statementResult.setResultSet(cstmnt.getResultSet());
        }

        useCount++;
        statementResult.setOtherResult(results);

    } catch (SQLException e) {

        statementResult.setSqlException(e);

    } catch (Exception e) {

        statementResult.setMessage(e.getMessage());
    }

    return statementResult;
}