Example usage for java.sql Types TIME

List of usage examples for java.sql Types TIME

Introduction

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

Prototype

int TIME

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

Click Source Link

Document

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

Usage

From source file:jp.co.tis.gsp.tools.dba.dialect.SqlserverDialect.java

@Override
public void setObjectInStmt(PreparedStatement stmt, int parameterIndex, String value, int sqlType)
        throws SQLException {
    if (sqlType == UN_USABLE_TYPE) {
        stmt.setNull(parameterIndex, Types.NULL);
    } else if (StringUtil.isBlank(value) || "".equals(value)) {
        stmt.setNull(parameterIndex, sqlType);
    } else if (sqlType == Types.TIME) {
        stmt.setTimestamp(parameterIndex, Timestamp.valueOf("1970-01-01 " + value));
    } else {/*from   ww  w  . jav  a2 s  .com*/
        stmt.setObject(parameterIndex, value, sqlType);
    }
}

From source file:org.apache.sqoop.hcat.HCatalogExportTest.java

public void testDateTypesToBigInt() throws Exception {
    final int TOTAL_RECORDS = 1 * 10;
    long offset = TimeZone.getDefault().getRawOffset();
    String table = getTableName().toUpperCase();
    ColumnGenerator[] cols = new ColumnGenerator[] {
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(0), "date", Types.DATE,
                    HCatFieldSchema.Type.BIGINT, 0, 0, 0 - offset, new Date(70, 0, 1), KeyType.NOT_A_KEY),
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(1), "time", Types.TIME,
                    HCatFieldSchema.Type.BIGINT, 0, 0, 36672000L - offset, new Time(10, 11, 12),
                    KeyType.NOT_A_KEY),/*  w  ww.j a  v  a  2 s . co m*/
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(2), "timestamp", Types.TIMESTAMP,
                    HCatFieldSchema.Type.BIGINT, 0, 0, 36672000L - offset,
                    new Timestamp(70, 0, 1, 10, 11, 12, 0), KeyType.NOT_A_KEY), };
    List<String> addlArgsArray = new ArrayList<String>();
    addlArgsArray.add("--map-column-hive");
    addlArgsArray.add("COL0=bigint,COL1=bigint,COL2=bigint");
    runHCatExport(addlArgsArray, TOTAL_RECORDS, table, cols);
}

From source file:org.castor.jdo.engine.SQLTypeInfos.java

/**
 * Set given value on given PreparedStatement at given index with given SQL type.
 * //  w  w w .j  a  v  a 2 s  . co m
 * @param stmt The PreparedStatement to set value on.
 * @param index The index of the value in the PreparedStatement.
 * @param value The value to set.
 * @param sqlType The SQL type of the value.
 */
public static void setValue(final PreparedStatement stmt, final int index, final Object value,
        final int sqlType) {
    try {
        if (value == null) {
            stmt.setNull(index, sqlType);
        } else {
            // Special processing for BLOB and CLOB types, because they are mapped
            // by Castor to java.io.InputStream and java.io.Reader, respectively,
            // while JDBC driver expects java.sql.Blob and java.sql.Clob.
            switch (sqlType) {
            case Types.FLOAT:
            case Types.DOUBLE:
                stmt.setDouble(index, ((Double) value).doubleValue());
                break;
            case Types.REAL:
                stmt.setFloat(index, ((Float) value).floatValue());
                break;
            case Types.TIME:
                final Time time = value instanceof java.util.Date ? new Time(((java.util.Date) value).getTime())
                        : null;
                stmt.setTime(index, time != null ? time : (Time) value, getCalendar());
                break;
            case Types.DATE:
                final Date date = value instanceof java.util.Date ? new Date(((java.util.Date) value).getTime())
                        : null;
                stmt.setDate(index, date != null ? date : (Date) value);
                break;
            case Types.TIMESTAMP:
                final Timestamp timestamp = value instanceof java.util.Date
                        ? new Timestamp(((java.util.Date) value).getTime())
                        : null;
                stmt.setTimestamp(index, timestamp != null ? timestamp : (Timestamp) value, getCalendar());
                break;
            case Types.BLOB:
                try {
                    InputStream stream;
                    if (value instanceof byte[]) {
                        stream = new ByteArrayInputStream((byte[]) value);
                    } else {
                        stream = (InputStream) value;
                    }
                    stmt.setBinaryStream(index, stream, stream.available());
                } catch (IOException ex) {
                    throw new SQLException(ex.toString());
                }
                break;
            case Types.CLOB:
                if (value instanceof String) {
                    stmt.setCharacterStream(index, new StringReader((String) value),
                            Math.min(((String) value).length(), Integer.MAX_VALUE));
                } else {
                    stmt.setCharacterStream(index, ((Clob) value).getCharacterStream(),
                            (int) Math.min(((Clob) value).length(), Integer.MAX_VALUE));
                }
                break;
            default:
                stmt.setObject(index, value, sqlType);
                break;
            }
        }
    } catch (SQLException ex) {
        LOG.error("Unexpected SQL exception: ", ex);
    }
}

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

/**
 * Tries to parse the default value of the column and returns it as an object of the
 * corresponding java type. If the value could not be parsed, then the original
 * definition is returned.//from  w w w . j av a2 s  . c  om
 * 
 * @return The parsed default value
 */
public Object getParsedDefaultValue() {
    if ((_defaultValue != null) && (_defaultValue.length() > 0)) {
        try {
            switch (_typeCode) {
            case Types.TINYINT:
            case Types.SMALLINT:
                return new Short(_defaultValue);
            case Types.INTEGER:
                return new Integer(_defaultValue);
            case Types.BIGINT:
                return new Long(_defaultValue);
            case Types.DECIMAL:
            case Types.NUMERIC:
                return new BigDecimal(_defaultValue);
            case Types.REAL:
                return new Float(_defaultValue);
            case Types.DOUBLE:
            case Types.FLOAT:
                return new Double(_defaultValue);
            case Types.DATE:
                return Date.valueOf(_defaultValue);
            case Types.TIME:
                return Time.valueOf(_defaultValue);
            case Types.TIMESTAMP:
                return Timestamp.valueOf(_defaultValue);
            case Types.BIT:
            case Types.BOOLEAN:
                return ConvertUtils.convert(_defaultValue, Boolean.class);
            }
        } catch (NumberFormatException ex) {
            return null;
        } catch (IllegalArgumentException ex) {
            return null;
        }
    }
    return _defaultValue;
}

From source file:com.jaspersoft.jrx.query.PlSqlQueryExecuter.java

private void createStatement() throws JRException {
    String queryString = getQueryString();

    if (connection != null && queryString != null && queryString.trim().length() > 0) {

        try {/*  w ww  .java2  s . c o  m*/
            isStoredProcedure = isOracleStoredProcedure(queryString);

            if (isStoredProcedure) {
                statement = connection.prepareCall(queryString);
            } else {
                statement = connection.prepareStatement(queryString);
            }

            int fetchSize = JRPropertiesUtil.getInstance(getJasperReportsContext()).getIntegerProperty(
                    dataset.getPropertiesMap(), JRJdbcQueryExecuterFactory.PROPERTY_JDBC_FETCH_SIZE, 0);
            if (fetchSize > 0) {
                statement.setFetchSize(fetchSize);
            }

            List<String> parameterNames = getCollectedParameterNames();
            if (!parameterNames.isEmpty()) {
                for (int i = 0; i < parameterNames.size(); i++) {
                    String parameterName = (String) parameterNames.get(i);
                    JRValueParameter parameter = getValueParameter(parameterName);
                    Class<?> clazz = parameter.getValueClass();
                    Object parameterValue = parameter.getValue();

                    if (clazz.equals(java.lang.Object.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.JAVA_OBJECT);
                        } else {
                            statement.setObject(i + 1, parameterValue);
                        }
                    } else if (clazz.equals(java.lang.Boolean.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.BIT);
                        } else {
                            statement.setBoolean(i + 1, ((Boolean) parameterValue).booleanValue());
                        }
                    } else if (clazz.equals(java.lang.Byte.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.TINYINT);
                        } else {
                            statement.setByte(i + 1, ((Byte) parameterValue).byteValue());
                        }
                    } else if (clazz.equals(java.lang.Double.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.DOUBLE);
                        } else {
                            statement.setDouble(i + 1, ((Double) parameterValue).doubleValue());
                        }
                    } else if (clazz.equals(java.lang.Float.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.FLOAT);
                        } else {
                            statement.setFloat(i + 1, ((Float) parameterValue).floatValue());
                        }
                    } else if (clazz.equals(java.lang.Integer.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.INTEGER);
                        } else {
                            statement.setInt(i + 1, ((Integer) parameterValue).intValue());
                        }
                    } else if (clazz.equals(java.lang.Long.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.BIGINT);
                        } else {
                            statement.setLong(i + 1, ((Long) parameterValue).longValue());
                        }
                    } else if (clazz.equals(java.lang.Short.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.SMALLINT);
                        } else {
                            statement.setShort(i + 1, ((Short) parameterValue).shortValue());
                        }
                    } else if (clazz.equals(java.math.BigDecimal.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.DECIMAL);
                        } else {
                            statement.setBigDecimal(i + 1, (BigDecimal) parameterValue);
                        }
                    } else if (clazz.equals(java.lang.String.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.VARCHAR);
                        } else {
                            statement.setString(i + 1, parameterValue.toString());
                        }
                    } else if (clazz.equals(java.util.Date.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.DATE);
                        } else {
                            statement.setDate(i + 1,
                                    new java.sql.Date(((java.util.Date) parameterValue).getTime()));
                        }
                    } else if (clazz.equals(java.sql.Timestamp.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.TIMESTAMP);
                        } else {
                            statement.setTimestamp(i + 1, (java.sql.Timestamp) parameterValue);
                        }
                    } else if (clazz.equals(java.sql.Time.class)) {
                        if (parameterValue == null) {
                            statement.setNull(i + 1, Types.TIME);
                        } else {
                            statement.setTime(i + 1, (java.sql.Time) parameterValue);
                        }
                    } else if (clazz.equals(java.sql.ResultSet.class)) {
                        if (!isStoredProcedure) {
                            throw new JRException("OUT paramater used in non-stored procedure call : "
                                    + parameterName + " class " + clazz.getName());
                        } else if (cursorParameter > 0) {
                            throw new JRException("A stored procedure can have at most one cursor parameter : "
                                    + parameterName + " class " + clazz.getName());
                        }

                        ((CallableStatement) statement).registerOutParameter(i + 1, ORACLE_CURSOR_TYPE);
                        cursorParameter = i + 1;
                    } else {
                        throw new JRException("Parameter type not supported in query : " + parameterName
                                + " class " + clazz.getName());
                    }
                }
            }
        } catch (SQLException e) {
            throw new JRException("Error preparing statement for executing the report query : " + "\n\n"
                    + queryString + "\n\n", e);
        }
    }
}

From source file:org.hibernate.dialect.Dialect.java

protected Dialect() {
    log.info("Using dialect: " + this);
    sqlFunctions.putAll(STANDARD_AGGREGATE_FUNCTIONS);

    // standard sql92 functions (can be overridden by subclasses)
    registerFunction("substring", new SQLFunctionTemplate(Hibernate.STRING, "substring(?1, ?2, ?3)"));
    registerFunction("locate", new SQLFunctionTemplate(Hibernate.INTEGER, "locate(?1, ?2, ?3)"));
    registerFunction("trim", new SQLFunctionTemplate(Hibernate.STRING, "trim(?1 ?2 ?3 ?4)"));
    registerFunction("length", new StandardSQLFunction("length", Hibernate.INTEGER));
    registerFunction("bit_length", new StandardSQLFunction("bit_length", Hibernate.INTEGER));
    registerFunction("coalesce", new StandardSQLFunction("coalesce"));
    registerFunction("nullif", new StandardSQLFunction("nullif"));
    registerFunction("abs", new StandardSQLFunction("abs"));
    registerFunction("mod", new StandardSQLFunction("mod", Hibernate.INTEGER));
    registerFunction("sqrt", new StandardSQLFunction("sqrt", Hibernate.DOUBLE));
    registerFunction("upper", new StandardSQLFunction("upper"));
    registerFunction("lower", new StandardSQLFunction("lower"));
    registerFunction("cast", new CastFunction());
    registerFunction("extract", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(?1 ?2 ?3)"));

    //map second/minute/hour/day/month/year to ANSI extract(), override on subclasses
    registerFunction("second", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(second from ?1)"));
    registerFunction("minute", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(minute from ?1)"));
    registerFunction("hour", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(hour from ?1)"));
    registerFunction("day", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(day from ?1)"));
    registerFunction("month", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(month from ?1)"));
    registerFunction("year", new SQLFunctionTemplate(Hibernate.INTEGER, "extract(year from ?1)"));

    registerFunction("str", new SQLFunctionTemplate(Hibernate.STRING, "cast(?1 as char)"));

    // register hibernate types for default use in scalar sqlquery type auto detection
    registerHibernateType(Types.BIGINT, Hibernate.BIG_INTEGER.getName());
    registerHibernateType(Types.BINARY, Hibernate.BINARY.getName());
    registerHibernateType(Types.BIT, Hibernate.BOOLEAN.getName());
    registerHibernateType(Types.CHAR, Hibernate.CHARACTER.getName());
    registerHibernateType(Types.DATE, Hibernate.DATE.getName());
    registerHibernateType(Types.DOUBLE, Hibernate.DOUBLE.getName());
    registerHibernateType(Types.FLOAT, Hibernate.FLOAT.getName());
    registerHibernateType(Types.INTEGER, Hibernate.INTEGER.getName());
    registerHibernateType(Types.SMALLINT, Hibernate.SHORT.getName());
    registerHibernateType(Types.TINYINT, Hibernate.BYTE.getName());
    registerHibernateType(Types.TIME, Hibernate.TIME.getName());
    registerHibernateType(Types.TIMESTAMP, Hibernate.TIMESTAMP.getName());
    registerHibernateType(Types.VARCHAR, Hibernate.STRING.getName());
    registerHibernateType(Types.VARBINARY, Hibernate.BINARY.getName());
    registerHibernateType(Types.NUMERIC, Hibernate.BIG_DECIMAL.getName());
    registerHibernateType(Types.DECIMAL, Hibernate.BIG_DECIMAL.getName());
    registerHibernateType(Types.BLOB, Hibernate.BLOB.getName());
    registerHibernateType(Types.CLOB, Hibernate.CLOB.getName());
    registerHibernateType(Types.REAL, Hibernate.FLOAT.getName());
}

From source file:com.nextep.designer.sqlclient.ui.connectors.SQLResultConnector.java

private void initializeTable(ISQLQuery query, long executionTime, INextepMetadata md) {
    colMaxWidth = new int[md.getColumnCount() + 1];
    // Checking columns to display
    int displayedColumns = query.getDisplayedColumnsCount();
    if (displayedColumns <= 0) {
        displayedColumns = md.getColumnCount();
    }/*from   w w  w  .j  ava  2 s  . c  o  m*/
    for (int i = 1; i <= displayedColumns; i++) {
        final String colName = md.getColumnName(i);
        // final int colPrecision = md.getPrecision(index);
        final int colType = md.getColumnType(i);

        final int colIndex = i - 1;
        if (!sqlView.isDisposed()) {
            TableColumn c = new TableColumn(sqlView, SWT.NONE);
            c.setText(colName);
            c.setWidth(colName.length() * 8);
            c.addListener(SWT.Selection, comparator);
            colMaxWidth[colIndex] = c.getWidth();
            SQLResultLabelProvider lblProvider = new SQLResultLabelProvider(this, colIndex);
            final TableViewerColumn viewerCol = new TableViewerColumn(sqlViewer, c);
            viewerCol.setLabelProvider(lblProvider);
            // Registering column comparator
            switch (colType) {
            case Types.BIGINT:
            case Types.BIT:
            case Types.DECIMAL:
            case Types.DOUBLE:
            case Types.FLOAT:
            case Types.INTEGER:
            case Types.NUMERIC:
            case Types.REAL:
            case Types.SMALLINT:
            case Types.TINYINT:
                c.setData(SQLResultViewerComparator.KEY_COMPARATOR, new NumberComparator());
                // Setting our deserializer which can produce number from string
                c.setData(KEY_DESERIALIZER, NUMBER_DESERIALIZER);
                break;
            case Types.DATE:
            case Types.TIME:
            case Types.TIMESTAMP:
                // Defines validation so that user will be notified about problems
                c.setData(KEY_CELL_VALIDATOR, DATE_VALIDATOR);
                // Setting our deserializer which can produce date from string
                c.setData(KEY_DESERIALIZER, DATE_DESERIALIZER);
                c.setData(SQLResultViewerComparator.KEY_COMPARATOR, new DateComparator());
                break;
            default:
                c.setData(KEY_DESERIALIZER, STRING_DESERIALIZER);
                c.setData(SQLResultViewerComparator.KEY_COMPARATOR, new StringComparator());
            }
        }
    }
    registerCellEditors();
    setStatusLabel(MessageFormat.format(SQLClientMessages.getString("sql.executionTime"), executionTime),
            false);
    sqlViewer.setInput(query.getResult());
}

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;// w  w w.j  ava 2 s.c o m

    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:com.squid.core.domain.operators.ExtendedType.java

private String getTypeName(int SQLType) {
    switch (SQLType) {
    case Types.ARRAY:
        return "ARRAY";
    case Types.BIGINT:
        return "INTEGER";
    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.DOUBLE:
        return "DOUBLE";
    case Types.FLOAT:
        return "FLOAT";
    case Types.INTEGER:
        return "INTEGER";
    case Types.JAVA_OBJECT:
        return "JAVA_OBJECT";
    case Types.LONGNVARCHAR:
        return "LONGNVARCHAR";
    case Types.LONGVARBINARY:
        return "LONGVARBINARY";
    case Types.NCHAR:
        return "NCHAR";
    case Types.NCLOB:
        return "NCLOB";
    case Types.NULL:
        return "UNDEFINED";//
    case Types.NUMERIC:
        return "NUMERIC";
    case Types.NVARCHAR:
        return "NVARCHAR";
    case Types.OTHER:
        return "UNDEFINED";//
    case Types.REAL:
        return "REAL";
    case Types.REF:
        return "REF";
    case Types.ROWID:
        return "ROWID";
    case Types.SMALLINT:
        return "SMALLINT";
    case Types.SQLXML:
        return "SQLXML";
    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:/*from  ww w  . j  av  a2s . co m*/
        return "UNDEFINED";//
    }
}

From source file:oscar.util.SqlUtils.java

private static Object getNewType(ResultSet rs, int colNum) {
    int type = 0;
    try {/*w  ww  .ja v a2s  .  c o m*/
        type = rs.getMetaData().getColumnType(colNum);
        switch (type) {
        case Types.LONGVARCHAR:
        case Types.CHAR:
        case Types.VARCHAR:
            return oscar.Misc.getString(rs, colNum);
        case Types.TINYINT:
        case Types.SMALLINT:
        case Types.INTEGER:
            return new Integer(rs.getInt(colNum));
        case Types.BIGINT:
            return new Long(rs.getLong(colNum));
        case Types.FLOAT:
        case Types.DECIMAL:
        case Types.REAL:
        case Types.DOUBLE:
        case Types.NUMERIC:
            return new Double(rs.getDouble(colNum));
        // case Types.B
        case Types.BIT:
            return new Boolean(rs.getBoolean(colNum));
        case Types.TIMESTAMP:
        case Types.DATE:
        case Types.TIME:
            return rs.getDate(colNum);
        default:
            return rs.getObject(colNum);
        }
    } catch (Exception e) {
        MiscUtils.getLogger().error("Error", e);
    }
    return null;
}