Example usage for java.sql Types TIMESTAMP

List of usage examples for java.sql Types TIMESTAMP

Introduction

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

Prototype

int TIMESTAMP

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

Click Source Link

Document

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

Usage

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

/**
 * Get value from given ResultSet at given index with given SQL type.
 * //from   ww w  .j  a v  a 2 s .c  o m
 * @param rs The ResultSet to get the value from.
 * @param index The index of the value in the ResultSet.
 * @param sqlType The SQL type of the value.
 * @return The value.
 * @throws SQLException If a database access error occurs.
 */
public static Object getValue(final ResultSet rs, final int index, final int sqlType) throws SQLException {
    switch (sqlType) {
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGVARCHAR:
        return rs.getString(index);
    case Types.DECIMAL:
    case Types.NUMERIC:
        return rs.getBigDecimal(index);
    case Types.INTEGER:
        int intVal = rs.getInt(index);
        return (rs.wasNull() ? null : new Integer(intVal));
    case Types.TIME:
        return rs.getTime(index, getCalendar());
    case Types.DATE:
        return rs.getDate(index);
    case Types.TIMESTAMP:
        return rs.getTimestamp(index, getCalendar());
    case Types.FLOAT:
    case Types.DOUBLE:
        double doubleVal = rs.getDouble(index);
        return (rs.wasNull() ? null : new Double(doubleVal));
    case Types.REAL:
        float floatVal = rs.getFloat(index);
        return (rs.wasNull() ? null : new Float(floatVal));
    case Types.SMALLINT:
        short shortVal = rs.getShort(index);
        return (rs.wasNull() ? null : new Short(shortVal));
    case Types.TINYINT:
        byte byteVal = rs.getByte(index);
        return (rs.wasNull() ? null : new Byte(byteVal));
    case Types.LONGVARBINARY:
    case Types.VARBINARY:
    case Types.BINARY:
        return rs.getBytes(index);
    case Types.BLOB:
        Blob blob = rs.getBlob(index);
        return (blob == null ? null : blob.getBinaryStream());
    case Types.CLOB:
        return rs.getClob(index);
    case Types.BIGINT:
        long longVal = rs.getLong(index);
        return (rs.wasNull() ? null : new Long(longVal));
    case Types.BIT:
        boolean boolVal = rs.getBoolean(index);
        return (rs.wasNull() ? null : new Boolean(boolVal));
    default:
        Object value = rs.getObject(index);
        return (rs.wasNull() ? null : value);
    }
}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.dialect.DialectPostgreSQL.java

@Override
public JDBCInfo getJDBCTypeAndString(ColumnType type) {
    switch (type.spec) {
    case STRING://  www  .jav a2 s. c o m
        if (type.isUnconstrained()) {
            return jdbcInfo("varchar", Types.VARCHAR);
        } else if (type.isClob()) {
            return jdbcInfo("text", Types.CLOB);
        } else {
            return jdbcInfo("varchar(%d)", type.length, Types.VARCHAR);
        }
    case ARRAY_STRING:
        if (type.isUnconstrained()) {
            return jdbcInfo("varchar[]", Types.ARRAY, "varchar", Types.VARCHAR);
        } else if (type.isClob()) {
            return jdbcInfo("text[]", Types.ARRAY, "text", Types.CLOB);
        } else {
            return jdbcInfo("varchar(%d)[]", type.length, Types.ARRAY, "varchar", Types.VARCHAR);
        }
    case BOOLEAN:
        return jdbcInfo("bool", Types.BIT);
    case ARRAY_BOOLEAN:
        return jdbcInfo("bool[]", Types.ARRAY, "bool", Types.BOOLEAN);
    case LONG:
        return jdbcInfo("int8", Types.BIGINT);
    case ARRAY_LONG:
        return jdbcInfo("int8[]", Types.ARRAY, "int8", Types.BIGINT);
    case DOUBLE:
        return jdbcInfo("float8", Types.DOUBLE);
    case ARRAY_DOUBLE:
        return jdbcInfo("float8[]", Types.ARRAY, "float8", Types.DOUBLE);
    case TIMESTAMP:
        return jdbcInfo("timestamp", Types.TIMESTAMP);
    case ARRAY_TIMESTAMP:
        return jdbcInfo("timestamp[]", Types.ARRAY, "timestamp", Types.TIMESTAMP);
    case BLOBID:
        return jdbcInfo("varchar(40)", Types.VARCHAR);
    case ARRAY_BLOBID:
        return jdbcInfo("varchar(40)[]", Types.ARRAY, "varchar", Types.VARCHAR);
    // -----
    case NODEID:
    case NODEIDFK:
    case NODEIDFKNP:
    case NODEIDFKMUL:
    case NODEIDFKNULL:
    case NODEIDPK:
    case NODEVAL:
        switch (idType) {
        case VARCHAR:
            return jdbcInfo("varchar(36)", Types.VARCHAR);
        case UUID:
            return jdbcInfo("uuid", Types.OTHER);
        case SEQUENCE:
            return jdbcInfo("int8", Types.BIGINT);
        }
    case NODEARRAY:
        switch (idType) {
        case VARCHAR:
            return jdbcInfo("varchar(36)[]", Types.ARRAY, "varchar", Types.VARCHAR);
        case UUID:
            return jdbcInfo("uuid[]", Types.ARRAY, "uuid", Types.OTHER);
        case SEQUENCE:
            return jdbcInfo("int8[]", Types.ARRAY, "int8", Types.BIGINT);
        }
    case SYSNAME:
        return jdbcInfo("varchar(250)", Types.VARCHAR);
    case SYSNAMEARRAY:
        return jdbcInfo("varchar(250)[]", Types.ARRAY, "varchar", Types.VARCHAR);
    case TINYINT:
        return jdbcInfo("int2", Types.SMALLINT);
    case INTEGER:
        return jdbcInfo("int4", Types.INTEGER);
    case ARRAY_INTEGER:
        return jdbcInfo("int4[]", Types.ARRAY, "int4", Types.INTEGER);
    case AUTOINC:
        return jdbcInfo("serial", Types.INTEGER);
    case FTINDEXED:
        if (compatibilityFulltextTable) {
            return jdbcInfo("tsvector", Types.OTHER);
        } else {
            return jdbcInfo("text", Types.CLOB);
        }
    case FTSTORED:
        if (compatibilityFulltextTable) {
            return jdbcInfo("tsvector", Types.OTHER);
        } else {
            return jdbcInfo("text", Types.CLOB);
        }
    case CLUSTERNODE:
        return jdbcInfo("int4", Types.INTEGER);
    case CLUSTERFRAGS:
        return jdbcInfo("varchar[]", Types.ARRAY, "varchar", Types.VARCHAR);
    }
    throw new AssertionError(type);
}

From source file:org.jamwiki.db.WikiPreparedStatement.java

/**
 * Sets the designated parameter to the given java.sql.Timestamp value. The
 * driver converts this to an SQL TIMESTAMP value when it sends it to the database.
 *
 * @param parameterIndex The first parameter is 1, the second is 2, ...
 * @param x The parameter value.//from w w  w.  ja  va2 s  . c  om
 * @throws Exception If a parameter is invalid.
 */
public void setTimestamp(int parameterIndex, Timestamp x) throws Exception {
    this.verifyParams(parameterIndex);
    this.paramTypes[parameterIndex - 1] = Types.TIMESTAMP;
    this.params[parameterIndex - 1] = x;
}

From source file:com.connsec.authentication.AcceptJdbcUsersAuthenticationHandler.java

public boolean logout(HttpServletResponse response) {
    Object sessionIdAttribute = WebContext.getAttribute(WebConstants.CURRENT_USER_SESSION_ID);
    if (sessionIdAttribute != null) {
        UserInfo userInfo = WebContext.getUserInfo();
        Date logoutDateTime = new Date();
        if (sessionIdAttribute != null) {

            jdbcTemplate.update(HISTORY_LOGOUT_UPDATE_STATEMENT,
                    new Object[] { logoutDateTime, sessionIdAttribute.toString() },
                    new int[] { Types.TIMESTAMP, Types.VARCHAR });
        }/*from   w  ww.j  ava  2  s. c o m*/

        jdbcTemplate.update(LOGOUT_USERINFO_UPDATE_STATEMENT, new Object[] { logoutDateTime, userInfo.getId() },
                new int[] { Types.TIMESTAMP, Types.VARCHAR });

        _logger.debug("Session " + WebContext.getAttribute(WebConstants.CURRENT_USER_SESSION_ID) + ", user "
                + userInfo.getUsername() + " Logout, datetime " + logoutDateTime + " .");
    }
    return true;

}

From source file:com.streamsets.pipeline.stage.origin.jdbc.table.BaseTableJdbcSourceIT.java

protected static void setParamsToPreparedStatement(PreparedStatement ps, int paramIdx, int sqlType,
        Object value) throws SQLException {
    switch (sqlType) {
    case Types.DATE:
        ps.setDate(paramIdx, new java.sql.Date(((Date) value).getTime()));
        break;//from w  w w .ja  v  a 2s  .  c om
    case Types.TIME:
        ps.setTime(paramIdx, new java.sql.Time(((Date) value).getTime()));
        break;
    case Types.TIMESTAMP:
        ps.setTimestamp(paramIdx, new java.sql.Timestamp(((Date) value).getTime()));
        break;
    default:
        ps.setObject(paramIdx, value);
    }
}

From source file:org.apache.cayenne.migration.MigrationGenerator.java

protected String nameForJdbcType(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.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.LONGVARBINARY:
        return "longVarBinary";
    case Types.LONGVARCHAR:
        return "longVarChar";
    case Types.NUMERIC:
        return "numeric";
    case Types.REAL:
        return "real";
    case Types.SMALLINT:
        return "smallInt";
    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   w ww  .  ja va2 s  . c o m
        return null;
    }
}

From source file:com.tesora.dve.sql.parser.TimestampVariableUtils.java

public static boolean setTimestampVariableForUnspecifiedColumn(SchemaContext sc, DMLStatement dmls,
        PEColumn column) {/*from   w ww  .j a v a2 s  .c om*/
    boolean ret = false;

    // only set the timestamp variable if this is a timestamp column
    if (column.getType().getBaseType().getDataType() != Types.TIMESTAMP) {
        return ret;
    }

    // for an update statement if the on update is set and 
    // the column is not specified then set the timestamp variable
    if ((dmls instanceof UpdateStatement) && column.isOnUpdated()) {
        ret = true;
        return ret;
    }

    boolean isNullable = column.isNullable();
    ExpressionNode defaultValue = column.getDefaultValue();
    if (defaultValue == null) {
        // no default value column modifier specified
        // now we need to know if the on update has also been set or not
        if (!column.isOnUpdated() && !isNullable) {
            // on update is not specified so default value becomes current timestamp
            ret = true;
        }
        //         else {
        //            With an ON UPDATE CURRENT_TIMESTAMP clause but no DEFAULT clause, 
        //            the column is automatically updated to the current timestamp. 
        //            The default is 0 unless the column is defined with the NULL attribute, 
        //            in which case the default is NULL.
        //         }
    } else {
        if (dmls instanceof UpdateStatement) {
            if (column.isOnUpdated()) {
                ret = true;
            }
        } else {
            if (column.getDefaultValue() == null) {
                // null default value
                // do not set timestamp variable
            } else {
                Object o = column.getDefaultValue();
                if (o instanceof IdentifierLiteralExpression) {
                    if (StringUtils.equals(((IdentifierLiteralExpression) o).asString(sc.getValues()), "0")) {
                        // do nothing
                    } else {
                        // for a timestamp column only other choice is current_timestamp
                        ret = true;
                    }
                } else if (o instanceof LiteralExpression) {
                    // for literal default value (ie. 0 or 'yyyy-mm-dd hh:mm:ss') 
                    // do not set timestamp variable
                }
            }
        }
    }

    return ret;
}

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

public void testDateTypes() throws Exception {
    final int TOTAL_RECORDS = 1 * 10;
    String table = getTableName().toUpperCase();
    ColumnGenerator[] cols = new ColumnGenerator[] {
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(0), "date", Types.DATE,
                    HCatFieldSchema.Type.STRING, 0, 0, "2013-12-31", new Date(113, 11, 31), KeyType.NOT_A_KEY),
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(1), "date", Types.DATE,
                    HCatFieldSchema.Type.DATE, 0, 0, new Date(113, 11, 31), new Date(113, 11, 31),
                    KeyType.NOT_A_KEY),//w w w . jav a2 s  .c o m
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(2), "time", Types.TIME,
                    HCatFieldSchema.Type.STRING, 0, 0, "10:11:12", new Time(10, 11, 12), KeyType.NOT_A_KEY),
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(3), "timestamp", Types.TIMESTAMP,
                    HCatFieldSchema.Type.STRING, 0, 0, "2013-12-31 10:11:12",
                    new Timestamp(113, 11, 31, 10, 11, 12, 0), KeyType.NOT_A_KEY),
            HCatalogTestUtils.colGenerator(HCatalogTestUtils.forIdx(4), "timestamp", Types.TIMESTAMP,
                    HCatFieldSchema.Type.TIMESTAMP, 0, 0, new Timestamp(113, 11, 31, 10, 11, 12, 0),
                    new Timestamp(113, 11, 31, 10, 11, 12, 0), KeyType.NOT_A_KEY), };
    List<String> addlArgsArray = new ArrayList<String>();
    runHCatExport(addlArgsArray, TOTAL_RECORDS, table, cols);
}

From source file:org.nuxeo.ecm.core.storage.sql.db.dialect.DialectOracle.java

@Override
@SuppressWarnings("boxing")
public Serializable getFromResultSet(ResultSet rs, int index, Column column) throws SQLException {
    switch (column.getJdbcType()) {
    case Types.VARCHAR:
        String string = rs.getString(index);
        if (column.getType() == ColumnType.BLOBID && string != null) {
            return column.getModel().getBinary(string);
        } else {/*from   w w w  . j a v a  2  s .c om*/
            return string;
        }
    case Types.CLOB:
        // Oracle cannot read CLOBs using rs.getString when the ResultSet is
        // a ScrollableResultSet (the standard OracleResultSetImpl works
        // fine).
        Reader r = rs.getCharacterStream(index);
        if (r == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        char[] buffer = new char[4096];
        try {
            int n;
            while ((n = r.read(buffer)) != -1) {
                sb.append(new String(buffer, 0, n));
            }
        } catch (IOException e) {
            log.error("Cannot read CLOB", e);
        }
        return sb.toString();
    case Types.BIT:
        return rs.getBoolean(index);
    case Types.TINYINT:
    case Types.SMALLINT:
    case Types.INTEGER:
    case Types.BIGINT:
        return rs.getLong(index);
    case Types.DOUBLE:
        return rs.getDouble(index);
    case Types.TIMESTAMP:
        Timestamp ts = rs.getTimestamp(index);
        if (ts == null) {
            return null;
        } else {
            Serializable cal = new GregorianCalendar(); // XXX timezone
            ((Calendar) cal).setTimeInMillis(ts.getTime());
            return cal;
        }
    }
    throw new SQLException("Unhandled JDBC type: " + column.getJdbcType());
}

From source file:org.nuxeo.ecm.core.storage.sql.jdbc.dialect.DialectOracle.java

@Override
public JDBCInfo getJDBCTypeAndString(ColumnType type) {
    switch (type.spec) {
    case STRING:/*from ww w .  ja  va  2s . c  o m*/
        if (type.isUnconstrained()) {
            return jdbcInfo("NVARCHAR2(2000)", Types.VARCHAR);
        } else if (type.isClob() || type.length > 2000) {
            return jdbcInfo("NCLOB", Types.CLOB);
        } else {
            return jdbcInfo("NVARCHAR2(%d)", type.length, Types.VARCHAR);
        }
    case BOOLEAN:
        return jdbcInfo("NUMBER(1,0)", Types.BIT);
    case LONG:
        return jdbcInfo("NUMBER(19,0)", Types.BIGINT);
    case DOUBLE:
        return jdbcInfo("DOUBLE PRECISION", Types.DOUBLE);
    case TIMESTAMP:
        return jdbcInfo("TIMESTAMP", Types.TIMESTAMP);
    case BLOBID:
        return jdbcInfo("VARCHAR2(40)", Types.VARCHAR);
    // -----
    case NODEID:
    case NODEIDFK:
    case NODEIDFKNP:
    case NODEIDFKMUL:
    case NODEIDFKNULL:
    case NODEIDPK:
    case NODEVAL:
        switch (idType) {
        case VARCHAR:
            return jdbcInfo("VARCHAR2(36)", Types.VARCHAR);
        case SEQUENCE:
            return jdbcInfo("NUMBER(10,0)", Types.INTEGER);
        default:
            throw new AssertionError("Unknown id type: " + idType);
        }
    case SYSNAME:
    case SYSNAMEARRAY:
        return jdbcInfo("VARCHAR2(250)", Types.VARCHAR);
    case TINYINT:
        return jdbcInfo("NUMBER(3,0)", Types.TINYINT);
    case INTEGER:
        return jdbcInfo("NUMBER(10,0)", Types.INTEGER);
    case AUTOINC:
        return jdbcInfo("NUMBER(10,0)", Types.INTEGER);
    case FTINDEXED:
        return jdbcInfo("CLOB", Types.CLOB);
    case FTSTORED:
        return jdbcInfo("NCLOB", Types.CLOB);
    case CLUSTERNODE:
        return jdbcInfo("VARCHAR(25)", Types.VARCHAR);
    case CLUSTERFRAGS:
        return jdbcInfo("VARCHAR2(4000)", Types.VARCHAR);
    }
    throw new AssertionError(type);
}