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: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);
        }/*ww  w.j  a v  a 2s  . c o  m*/
        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:it.greenvulcano.gvesb.datahandling.dbo.utils.StandardRowSetBuilder.java

public int build(Document doc, String id, ResultSet rs, Set<Integer> keyField,
        Map<String, FieldFormatter> fieldNameToFormatter, Map<String, FieldFormatter> fieldIdToFormatter)
        throws Exception {
    if (rs == null) {
        return 0;
    }//from   w  w w.j ava2  s  .  c o m
    int rowCounter = 0;
    Element docRoot = doc.getDocumentElement();
    ResultSetMetaData metadata = rs.getMetaData();
    FieldFormatter[] fFormatters = buildFormatterArray(metadata, fieldNameToFormatter, fieldIdToFormatter);

    boolean noKey = ((keyField == null) || keyField.isEmpty());

    //boolean isNull = false;
    Element data = null;
    Element row = null;
    Element col = null;
    Text text = null;
    String textVal = null;
    String precKey = null;
    String colKey = null;
    Map<String, String> keyAttr = new HashMap<String, String>();
    while (rs.next()) {
        if (rowCounter % 10 == 0) {
            ThreadUtils.checkInterrupted(getClass().getSimpleName(), name, logger);
        }
        row = parser.createElement(doc, AbstractDBO.ROW_NAME);

        parser.setAttribute(row, AbstractDBO.ID_NAME, id);
        for (int j = 1; j <= metadata.getColumnCount(); j++) {
            FieldFormatter fF = fFormatters[j];

            //isNull = false;
            col = parser.createElement(doc, AbstractDBO.COL_NAME);
            switch (metadata.getColumnType(j)) {
            case Types.DATE: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DATE_TYPE);
                java.sql.Date dateVal = rs.getDate(j);
                textVal = processDateTime(col, fF, dateVal, AbstractDBO.DEFAULT_DATE_FORMAT);
            }
                break;
            case Types.TIME: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.TIME_TYPE);
                java.sql.Time dateVal = rs.getTime(j);
                textVal = processDateTime(col, fF, dateVal, AbstractDBO.DEFAULT_TIME_FORMAT);
            }
                break;
            case Types.TIMESTAMP: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.TIMESTAMP_TYPE);
                Timestamp dateVal = rs.getTimestamp(j);
                textVal = processDateTime(col, fF, dateVal, AbstractDBO.DEFAULT_DATE_FORMAT);
            }
                break;
            case Types.DOUBLE: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.FLOAT_TYPE);
                double numVal = rs.getDouble(j);
                textVal = processDouble(col, fF, numVal);
            }
                break;
            case Types.FLOAT:
            case Types.REAL: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.FLOAT_TYPE);
                float numVal = rs.getFloat(j);
                textVal = processDouble(col, fF, numVal);
            }
                break;
            case Types.BIGINT: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.BIGINT_TYPE);
                long numVal = rs.getLong(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(numVal);
            }
                break;
            case Types.INTEGER: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.INTEGER_TYPE);
                int numVal = rs.getInt(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(numVal);
            }
                break;
            case Types.SMALLINT:
            case Types.TINYINT: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.SMALLINT_TYPE);
                short numVal = rs.getShort(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(numVal);
            }
                break;
            case Types.NUMERIC:
            case Types.DECIMAL: {
                BigDecimal bigdecimal = rs.getBigDecimal(j);
                boolean isNull = bigdecimal == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    if (metadata.getScale(j) > 0) {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DECIMAL_TYPE);
                    } else {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.NUMERIC_TYPE);
                    }
                    textVal = "";
                } else {
                    if (fF != null) {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DECIMAL_TYPE);
                        parser.setAttribute(col, AbstractDBO.FORMAT_NAME, fF.getNumberFormat());
                        parser.setAttribute(col, AbstractDBO.GRP_SEPARATOR_NAME, fF.getGroupSeparator());
                        parser.setAttribute(col, AbstractDBO.DEC_SEPARATOR_NAME, fF.getDecSeparator());
                        textVal = fF.formatNumber(bigdecimal);
                    } else if (metadata.getScale(j) > 0) {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DECIMAL_TYPE);
                        parser.setAttribute(col, AbstractDBO.FORMAT_NAME, numberFormat);
                        parser.setAttribute(col, AbstractDBO.GRP_SEPARATOR_NAME, groupSeparator);
                        parser.setAttribute(col, AbstractDBO.DEC_SEPARATOR_NAME, decSeparator);
                        textVal = numberFormatter.format(bigdecimal);
                    } else {
                        parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.NUMERIC_TYPE);
                        textVal = bigdecimal.toString();
                    }
                }
            }
                break;
            case Types.BOOLEAN: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.BOOLEAN_TYPE);
                boolean bVal = rs.getBoolean(j);
                parser.setAttribute(col, AbstractDBO.NULL_NAME, "false");
                textVal = String.valueOf(bVal);
            }
                break;
            case Types.SQLXML: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.XML_TYPE);
                SQLXML xml = rs.getSQLXML(j);
                boolean isNull = xml == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                } else {
                    textVal = xml.getString();
                }
            }
                break;
            case Types.NCHAR:
            case Types.NVARCHAR: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.NSTRING_TYPE);
                textVal = rs.getNString(j);
                if (textVal == null) {
                    textVal = "";
                }
            }
                break;
            case Types.CHAR:
            case Types.VARCHAR: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.STRING_TYPE);
                textVal = rs.getString(j);
                boolean isNull = textVal == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                }
            }
                break;
            case Types.NCLOB: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.LONG_NSTRING_TYPE);
                NClob clob = rs.getNClob(j);
                if (clob != null) {
                    Reader is = clob.getCharacterStream();
                    StringWriter str = new StringWriter();

                    IOUtils.copy(is, str);
                    is.close();
                    textVal = str.toString();
                } else {
                    textVal = "";
                }
            }
                break;
            case Types.CLOB: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.LONG_STRING_TYPE);
                Clob clob = rs.getClob(j);
                if (clob != null) {
                    Reader is = clob.getCharacterStream();
                    StringWriter str = new StringWriter();

                    IOUtils.copy(is, str);
                    is.close();
                    textVal = str.toString();
                } else {
                    textVal = "";
                }
            }
                break;
            case Types.BLOB: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.BASE64_TYPE);
                Blob blob = rs.getBlob(j);
                boolean isNull = blob == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                } else {
                    InputStream is = blob.getBinaryStream();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    IOUtils.copy(is, baos);
                    is.close();
                    try {
                        byte[] buffer = Arrays.copyOf(baos.toByteArray(), (int) blob.length());
                        textVal = Base64.getEncoder().encodeToString(buffer);
                    } catch (SQLFeatureNotSupportedException exc) {
                        textVal = Base64.getEncoder().encodeToString(baos.toByteArray());
                    }
                }
            }
                break;
            default: {
                parser.setAttribute(col, AbstractDBO.TYPE_NAME, AbstractDBO.DEFAULT_TYPE);
                textVal = rs.getString(j);
                boolean isNull = textVal == null;
                parser.setAttribute(col, AbstractDBO.NULL_NAME, String.valueOf(isNull));
                if (isNull) {
                    textVal = "";
                }
            }
            }
            if (textVal != null) {
                text = doc.createTextNode(textVal);
                col.appendChild(text);
            }
            if (!noKey && keyField.contains(new Integer(j))) {
                if (textVal != null) {
                    if (colKey == null) {
                        colKey = textVal;
                    } else {
                        colKey += "##" + textVal;
                    }
                    keyAttr.put("key_" + j, textVal);
                }
            } else {
                row.appendChild(col);
            }
        }
        if (noKey) {
            if (data == null) {
                data = parser.createElement(doc, AbstractDBO.DATA_NAME);
                parser.setAttribute(data, AbstractDBO.ID_NAME, id);
            }
        } else if ((colKey != null) && !colKey.equals(precKey)) {
            if (data != null) {
                docRoot.appendChild(data);
            }
            data = parser.createElement(doc, AbstractDBO.DATA_NAME);
            parser.setAttribute(data, AbstractDBO.ID_NAME, id);
            for (Entry<String, String> keyAttrEntry : keyAttr.entrySet()) {
                parser.setAttribute(data, keyAttrEntry.getKey(), keyAttrEntry.getValue());
            }
            keyAttr.clear();
            precKey = colKey;
        }
        colKey = null;
        data.appendChild(row);
        rowCounter++;
    }
    if (data != null) {
        docRoot.appendChild(data);
    }

    return rowCounter;
}

From source file:org.sakaiproject.tool.tasklist.impl.TaskListManagerJdbcImpl.java

public boolean saveTask(Task t) {
    try {/*from ww  w. java 2  s. c om*/
        if (t.getId() == null) {
            // if the id is not set the we are inserting a new record
            getJdbcTemplate().update(TASK_INSERT_QUERY,
                    new Object[] { t.getOwner(), t.getSiteId(), t.getCreationDate(), t.getTask() },
                    new int[] { Types.VARCHAR, Types.VARCHAR, Types.TIMESTAMP, Types.CLOB });
        } else {
            getJdbcTemplate().update(TASK_UPDATE_QUERY,
                    new Object[] { t.getOwner(), t.getSiteId(), t.getTask(), t.getId() },
                    new int[] { Types.VARCHAR, Types.VARCHAR, Types.CLOB, Types.BIGINT });
        }
    } catch (DataAccessException e) {
        log.error("Exception: Could not add task:" + e.toString());
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:co.nubetech.apache.hadoop.DateSplitter.java

/**
 * Retrieve the value from the column in a type-appropriate manner and
 * return its timestamp since the epoch. If the column is null, then return
 * Long.MIN_VALUE. This will cause a special split to be generated for the
 * NULL case, but may also cause poorly-balanced splits if most of the
 * actual dates are positive time since the epoch, etc.
 *//*w  ww.  j av a2  s.  c o m*/
private long resultSetColToLong(ResultSet rs, int colNum, int sqlDataType) throws SQLException {
    try {
        switch (sqlDataType) {
        case Types.DATE:
            return rs.getDate(colNum).getTime();
        case Types.TIME:
            return rs.getTime(colNum).getTime();
        case Types.TIMESTAMP:
            return rs.getTimestamp(colNum).getTime();
        default:
            throw new SQLException("Not a date-type field");
        }
    } catch (NullPointerException npe) {
        // null column. return minimum long value.
        LOG.warn("Encountered a NULL date in the split column. Splits may be poorly balanced.");
        return Long.MIN_VALUE;
    }
}

From source file:com.cloudera.sqoop.mapreduce.db.DateSplitter.java

/**
    Retrieve the value from the column in a type-appropriate manner and
    return its timestamp since the epoch. If the column is null, then return
    Long.MIN_VALUE.  This will cause a special split to be generated for the
    NULL case, but may also cause poorly-balanced splits if most of the
    actual dates are positive time since the epoch, etc.
  *///from ww w  .  j  ava2  s  . c o  m
private long resultSetColToLong(ResultSet rs, int colNum, int sqlDataType) throws SQLException {
    try {
        switch (sqlDataType) {
        case Types.DATE:
            return rs.getDate(colNum).getTime();
        case Types.TIME:
            return rs.getTime(colNum).getTime();
        case Types.TIMESTAMP:
            return rs.getTimestamp(colNum).getTime();
        default:
            throw new SQLException("Not a date-type field");
        }
    } catch (NullPointerException npe) {
        // null column. return minimum long value.
        LOG.warn("Encountered a NULL date in the split column. " + "Splits may be poorly balanced.");
        return Long.MIN_VALUE;
    }
}

From source file:org.apache.hadoop.chukwa.extraction.Consolidator.java

public void run() {
    ResultSet rs = null;/*from  w ww . j  av a 2 s. c  o  m*/
    String[] columns;
    int[] columnsType;
    String groupBy = "";

    for (int interval : intervals) {
        // Start reducing from beginning of time;
        Calendar aYearAgo = Calendar.getInstance();
        aYearAgo.set(2008, 12, 30, 0, 0, 0);

        long start = aYearAgo.getTimeInMillis(); //starting from 2008/01/01
        long end = start + (interval * 60000);
        log.debug("start time: " + start);
        log.debug("end time: " + end);
        Calendar now = Calendar.getInstance();
        DatabaseWriter db = new DatabaseWriter();
        String fields = null;
        String dateclause = null;
        boolean emptyPrimeKey = false;
        log.debug("Consolidate for " + interval + " minutes interval.");
        String table = this.table + "_" + interval;
        // Find the most recent entry
        try {
            String query = "select * from " + table + " order by timestamp desc limit 1";
            log.debug("Query: " + query);
            rs = db.query(query);
            if (rs == null) {
                throw new SQLException("Table undefined.");
            }
            ResultSetMetaData rmeta = rs.getMetaData();
            boolean empty = true;
            if (rs.next()) {
                for (int i = 1; i <= rmeta.getColumnCount(); i++) {
                    if (rmeta.getColumnName(i).toLowerCase().equals("timestamp")) {
                        start = rs.getTimestamp(i).getTime();
                    }
                }
                empty = false;
            }
            if (empty) {
                throw new SQLException("Table is empty.");
            }
            end = start + (interval * 60000);
        } catch (SQLException ex) {
            try {
                String query = "select * from " + this.table + " order by timestamp limit 1";
                log.debug("Query: " + query);
                rs = db.query(query);
                if (rs.next()) {
                    ResultSetMetaData rmeta = rs.getMetaData();
                    for (int i = 1; i <= rmeta.getColumnCount(); i++) {
                        if (rmeta.getColumnName(i).toLowerCase().equals("timestamp")) {
                            start = rs.getTimestamp(i).getTime();
                        }
                    }
                }
                end = start + (interval * 60000);
            } catch (SQLException ex2) {
                log.error("Unable to determine starting point in table: " + this.table);
                log.error("SQL Error:" + ExceptionUtil.getStackTrace(ex2));
                return;
            }
        }
        try {
            ResultSetMetaData rmeta = rs.getMetaData();
            int col = rmeta.getColumnCount();
            columns = new String[col];
            columnsType = new int[col];
            for (int i = 1; i <= col; i++) {
                columns[i - 1] = rmeta.getColumnName(i);
                columnsType[i - 1] = rmeta.getColumnType(i);
            }

            for (int i = 0; i < columns.length; i++) {
                if (i == 0) {
                    fields = columns[i];
                    if (columnsType[i] == java.sql.Types.VARCHAR) {
                        groupBy = " group by " + columns[i];
                    }
                } else {
                    if (columnsType[i] == java.sql.Types.VARCHAR
                            || columnsType[i] == java.sql.Types.TIMESTAMP) {
                        fields = fields + "," + columns[i];
                        if (columnsType[i] == java.sql.Types.VARCHAR) {
                            groupBy = " group by " + columns[i];
                        }
                    } else {
                        fields = fields + ",AVG(" + columns[i] + ") as " + columns[i];
                    }
                }
            }
        } catch (SQLException ex) {
            log.error("SQL Error:" + ExceptionUtil.getStackTrace(ex));
            return;
        }
        if (groupBy.equals("")) {
            emptyPrimeKey = true;
        }
        long previousStart = start;
        while (end < now.getTimeInMillis() - (interval * 2 * 60000)) {
            // Select new data sample for the given intervals
            if (interval == 5) {
                table = this.table;
            } else if (interval == 30) {
                table = this.table + "_5";
            } else if (interval == 120) {
                table = this.table + "_30";
            }
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startS = formatter.format(start);
            String endS = formatter.format(end);
            dateclause = "Timestamp >= '" + startS + "' and Timestamp <= '" + endS + "'";
            if (emptyPrimeKey) {
                groupBy = "group by " + dateclause;
            }
            String query = "insert ignore into " + this.table + "_" + interval + " (select " + fields + " from "
                    + table + " where " + dateclause + groupBy + ")";
            log.debug(query);
            db.execute(query);
            db.close();
            if (previousStart == start) {
                start = start + (interval * 60000);
                end = start + (interval * 60000);
                previousStart = start;
            }
        }
    }
}

From source file:org.apache.ddlutils.platform.oracle.Oracle8ModelReader.java

/**
  * {@inheritDoc}//from www  . ja  v  a  2  s.c om
  */
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map values) throws SQLException {
    Column column = super.readColumn(metaData, values);

    if (column.getDefaultValue() != null) {
        // Oracle pads the default value with spaces
        column.setDefaultValue(column.getDefaultValue().trim());
    }
    if (column.getTypeCode() == Types.DECIMAL) {
        // We're back-mapping the NUMBER columns returned by Oracle
        // Note that the JDBC driver returns DECIMAL for these NUMBER columns
        switch (column.getSizeAsInt()) {
        case 1:
            if (column.getScale() == 0) {
                column.setTypeCode(Types.BIT);
            }
            break;
        case 3:
            if (column.getScale() == 0) {
                column.setTypeCode(Types.TINYINT);
            }
            break;
        case 5:
            if (column.getScale() == 0) {
                column.setTypeCode(Types.SMALLINT);
            }
            break;
        case 18:
            column.setTypeCode(Types.REAL);
            break;
        case 22:
            if (column.getScale() == 0) {
                column.setTypeCode(Types.INTEGER);
            }
            break;
        case 38:
            if (column.getScale() == 0) {
                column.setTypeCode(Types.BIGINT);
            } else {
                column.setTypeCode(Types.DOUBLE);
            }
            break;
        }
    } else if (column.getTypeCode() == Types.FLOAT) {
        // Same for REAL, FLOAT, DOUBLE PRECISION, which all back-map to FLOAT but with
        // different sizes (63 for REAL, 126 for FLOAT/DOUBLE PRECISION)
        switch (column.getSizeAsInt()) {
        case 63:
            column.setTypeCode(Types.REAL);
            break;
        case 126:
            column.setTypeCode(Types.DOUBLE);
            break;
        }
    } else if ((column.getTypeCode() == Types.DATE) || (column.getTypeCode() == Types.TIMESTAMP)) {
        // Oracle has only one DATE/TIME type, so we can't know which it is and thus map
        // it back to TIMESTAMP
        column.setTypeCode(Types.TIMESTAMP);

        // we also reverse the ISO-format adaptation, and adjust the default value to timestamp
        if (column.getDefaultValue() != null) {
            Matcher matcher = _oracleIsoTimestampPattern.matcher(column.getDefaultValue());
            Timestamp timestamp = null;

            if (matcher.matches()) {
                String timestampVal = matcher.group(1);

                timestamp = Timestamp.valueOf(timestampVal);
            } else {
                matcher = _oracleIsoDatePattern.matcher(column.getDefaultValue());
                if (matcher.matches()) {
                    String dateVal = matcher.group(1);

                    timestamp = new Timestamp(Date.valueOf(dateVal).getTime());
                } else {
                    matcher = _oracleIsoTimePattern.matcher(column.getDefaultValue());
                    if (matcher.matches()) {
                        String timeVal = matcher.group(1);

                        timestamp = new Timestamp(Time.valueOf(timeVal).getTime());
                    }
                }
            }
            if (timestamp != null) {
                column.setDefaultValue(timestamp.toString());
            }
        }
    } else if (TypeMap.isTextType(column.getTypeCode())) {
        column.setDefaultValue(unescape(column.getDefaultValue(), "'", "''"));
    }
    return column;
}

From source file:org.apache.cayenne.dbsync.merge.MergeCase.java

private static boolean isDateTimeType(int type) {
    return type == Types.DATE || type == Types.TIME || type == Types.TIMESTAMP;
}

From source file:org.jumpmind.db.platform.db2.Db2DdlReader.java

@Override
protected Column readColumn(DatabaseMetaDataWrapper metaData, Map<String, Object> values) throws SQLException {
    Column column = super.readColumn(metaData, values);

    if (column.getDefaultValue() != null) {
        if (column.getMappedTypeCode() == Types.TIME) {
            Matcher matcher = db2TimePattern.matcher(column.getDefaultValue());

            // Db2 returns "HH24.MI.SS"
            if (matcher.matches()) {
                StringBuffer newDefault = new StringBuffer();

                newDefault.append("'");
                // the hour
                newDefault.append(matcher.group(1));
                newDefault.append(":");
                // the minute
                newDefault.append(matcher.group(2));
                newDefault.append(":");
                // the second
                newDefault.append(matcher.group(3));
                newDefault.append("'");

                column.setDefaultValue(newDefault.toString());
            }//from  w  w w.  j a v a  2  s .  co  m
        } else if (column.getMappedTypeCode() == Types.TIMESTAMP) {

            Matcher matcher = db2TimestampPattern.matcher(column.getDefaultValue());

            // Db2 returns "YYYY-MM-DD-HH24.MI.SS.FF"
            if (matcher.matches()) {
                StringBuffer newDefault = new StringBuffer();

                newDefault.append("'");
                // group 1 is the date which has the correct format
                newDefault.append(matcher.group(1));
                newDefault.append(" ");
                // the hour
                newDefault.append(matcher.group(2));
                newDefault.append(":");
                // the minute
                newDefault.append(matcher.group(3));
                newDefault.append(":");
                // the second
                newDefault.append(matcher.group(4));
                // optionally, the fraction
                if ((matcher.groupCount() > 4) && (matcher.group(4) != null)) {
                    newDefault.append(matcher.group(5));
                }
                newDefault.append("'");

                column.setDefaultValue(newDefault.toString());
            }
        } else if (TypeMap.isTextType(column.getMappedTypeCode())) {
            String defaultValue = column.getDefaultValue();
            // DB2 stores default text values quoted
            if ((defaultValue.length() >= 2) && defaultValue.startsWith("'") && defaultValue.endsWith("'")) {
                defaultValue = defaultValue.substring(1, defaultValue.length() - 1);
            }
            column.setDefaultValue(unescape(defaultValue, "'", "''"));
        }
    }
    return column;
}

From source file:com.xpfriend.fixture.cast.temp.TypeConverter.java

private static Class<?> getJavaType(int sqltype, int precision, int scale) {
    switch (sqltype) {
    case Types.BIGINT:
        return Long.class;
    case Types.BIT:
        return Boolean.class;
    case Types.BOOLEAN:
        return Boolean.class;
    case Types.CHAR:
        return String.class;
    case Types.DECIMAL:
        return getNumericType(precision, scale);
    case Types.DOUBLE:
        return Double.class;
    case Types.FLOAT:
        return Double.class;
    case Types.INTEGER:
        return Integer.class;
    case Types.LONGVARCHAR:
        return String.class;
    case Types.NUMERIC:
        return getNumericType(precision, scale);
    case Types.REAL:
        return Float.class;
    case Types.SMALLINT:
        return Short.class;
    case Types.DATE:
        return java.sql.Timestamp.class;
    case Types.TIME:
        return java.sql.Time.class;
    case Types.TIMESTAMP:
        return java.sql.Timestamp.class;
    case Types.TINYINT:
        return Byte.class;
    case Types.VARCHAR:
        return String.class;
    case Types.BLOB:
        return byte[].class;
    case Types.LONGVARBINARY:
        return byte[].class;
    case Types.CLOB:
        return String.class;
    case Types.BINARY:
        return byte[].class;
    case Types.VARBINARY:
        return byte[].class;
    case Types.NVARCHAR:
        return String.class;
    case Types.NCHAR:
        return String.class;
    case Types.LONGNVARCHAR:
        return String.class;
    case -155:/*  w  ww.  jav  a  2  s.  co  m*/
        return java.sql.Timestamp.class;
    default:
        return Object.class;
    }
}