Example usage for java.sql Types DATE

List of usage examples for java.sql Types DATE

Introduction

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

Prototype

int DATE

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

Click Source Link

Document

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

Usage

From source file:org.brucalipto.sqlutil.SQLManager.java

/**
 * Method useful for SQL SELECT//from   w w w.j  a  v a 2s .c  o  m
 * @param preparedStatement The prepared statement to execute
 * @param params List of {@link SQLParameter} to use to complete the prepared statement
 * @param outputSQLType A java.sql.Types type of return value
 * @return The {@link SPParameter} containing the returned value
 */
public SQLParameter simpleSelect(final String preparedStatement, SQLParameter[] params,
        final int outputSQLType) {
    final SQLParameter[] parameters;
    if (params == null) {
        parameters = new SQLParameter[0];
        log.debug("Going to execute a query without parameters.");
    } else {
        parameters = (SQLParameter[]) params.clone();
    }
    Connection dbConn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        if (this.dataSource != null) {
            dbConn = this.dataSource.getConnection();
        } else {
            dbConn = this.connection;
        }
        pstmt = dbConn.prepareStatement(preparedStatement);
        for (int i = 0; i < parameters.length; i++) {
            final SQLParameter param = parameters[i];
            log.debug((i + 1) + ") Going to add parameter " + param);
            final int sqlType = param.getSqlType();
            final Object paramValue = param.getValue();
            if (paramValue == null) {
                pstmt.setNull(i + 1, sqlType);
                continue;
            }
            switch (sqlType) {
            case Types.VARCHAR:
                pstmt.setString(i + 1, (String) paramValue);
                break;
            case Types.INTEGER:
                if (paramValue instanceof Integer) {
                    pstmt.setInt(i + 1, ((Integer) paramValue).intValue());
                } else if (paramValue instanceof Long) {
                    pstmt.setLong(i + 1, ((Long) paramValue).longValue());
                }
                break;
            case Types.DATE:
                pstmt.setDate(i + 1, (Date) paramValue);
                break;
            case Types.BOOLEAN:
                pstmt.setBoolean(i + 1, ((Boolean) paramValue).booleanValue());
                break;
            case Types.CHAR:
                pstmt.setString(i + 1, ((Character) paramValue).toString());
                break;
            case Types.DOUBLE:
                pstmt.setDouble(i + 1, ((Double) paramValue).doubleValue());
                break;
            case Types.FLOAT:
                pstmt.setFloat(i + 1, ((Float) paramValue).floatValue());
                break;
            case Types.TIMESTAMP:
                pstmt.setTimestamp(i + 1, (Timestamp) paramValue);
                break;
            default:
                pstmt.setObject(i + 1, paramValue);
                break;
            }
        }

        rs = pstmt.executeQuery();
        log.debug("Prepared statement '" + preparedStatement + "' succesfully executed!");
        while (rs.next()) {
            return new SQLParameter(outputSQLType, (Serializable) rs.getObject(1));
        }
        log.info("Prepared statement '" + preparedStatement + "' returned '0' rows");
    } catch (SQLException e) {
        log.error("Error executing prepared statement '" + preparedStatement + "'", e);
    } catch (Exception e) {
        log.error("Error executing prepared statement '" + preparedStatement + "'", e);
    } finally {
        closeResources(rs, pstmt, dbConn);
    }

    return new SQLParameter(outputSQLType, null);
}

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;//from   w  ww.  j  ava2s  .com

    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:oscar.util.SqlUtils.java

private static Object getNewType(ResultSet rs, int colNum) {
    int type = 0;
    try {//  w  w w  . j  a  v  a 2  s  . com
        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;
}

From source file:com.squid.core.domain.operators.ExtendedType.java

public static int FromTypeName(String SQLName) {
    if (SQLName.equals("ARRAY")) {
        return Types.ARRAY;
    }/*w  w w .  j av a  2 s  .com*/
    if (SQLName.equals("INTEGER")) {
        return Types.INTEGER;
    }
    if (SQLName.equals("BINARY")) {
        return Types.BINARY;
    }
    if (SQLName.equals("BIT")) {
        return Types.BIT;
    }
    if (SQLName.equals("BLOB")) {
        return Types.BLOB;
    }
    if (SQLName.equals("BOOLEAN")) {
        return Types.BOOLEAN;
    }
    if (SQLName.equals("CHAR")) {
        return Types.CHAR;
    }
    if (SQLName.equals("CLOB")) {
        return Types.CLOB;
    }
    if (SQLName.equals("DATALINK")) {
        return Types.DATALINK;
    }
    if (SQLName.equals("DATE")) {
        return Types.DATE;
    }
    if (SQLName.equals("DECIMAL")) {
        return Types.DECIMAL;
    }
    if (SQLName.equals("DOUBLE")) {
        return Types.DOUBLE;
    }
    if (SQLName.equals("FLOAT")) {
        return Types.FLOAT;
    }
    if (SQLName.equals("JAVA_OBJECT")) {
        return Types.JAVA_OBJECT;
    }
    if (SQLName.equals("LONGNVARCHAR")) {
        return Types.LONGNVARCHAR;
    }
    if (SQLName.equals("LONGVARBINARY")) {
        return Types.LONGVARBINARY;
    }
    if (SQLName.equals("NCHAR")) {
        return Types.NCHAR;
    }
    if (SQLName.equals("NCLOB")) {
        return Types.NCLOB;
    }
    if (SQLName.equals("UNDEFINED")) {
        return Types.OTHER;
    }
    if (SQLName.equals("NUMERIC")) {
        return Types.NUMERIC;
    }
    if (SQLName.equals("NVARCHAR")) {
        return Types.NVARCHAR;
    }
    if (SQLName.equals("REAL")) {
        return Types.REAL;
    }
    if (SQLName.equals("REF")) {
        return Types.REF;
    }
    if (SQLName.equals("ROWID")) {
        return Types.ROWID;
    }
    if (SQLName.equals("SMALLINT")) {
        return Types.SMALLINT;
    }
    if (SQLName.equals("SQLXML")) {
        return Types.SQLXML;
    }
    if (SQLName.equals("STRUCT")) {
        return Types.STRUCT;
    }
    if (SQLName.equals("TIME")) {
        return Types.TIME;
    }
    if (SQLName.equals("TIMESTAMP")) {
        return Types.TIMESTAMP;
    }
    if (SQLName.equals("TINYINT")) {
        return Types.TINYINT;
    }
    if (SQLName.equals("VARCHAR")) {
        return Types.VARCHAR;
    }
    if (SQLName.equals("VARBINARY")) {
        return Types.VARBINARY;
    }
    return Types.OTHER;
}

From source file:org.netflux.core.FieldMetadata.java

/**
 * Returns a string representation of the provided type.
 * //from ww w .j  a v  a 2s  .c  o  m
 * @param type the type from {@link Types} to transform to a string.
 * @return a string representation of the provided type.
 */
public static String typeToString(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.DATALINK:
        return "DATALINK";

    case Types.DATE:
        return "DATE";

    case Types.DECIMAL:
        return "DECIMAL";

    case Types.DISTINCT:
        return "DISTINCT";

    case Types.DOUBLE:
        return "DOUBLE";

    case Types.FLOAT:
        return "FLOAT";

    case Types.INTEGER:
        return "INTEGER";

    case Types.JAVA_OBJECT:
        return "JAVA_OBJECT";

    case Types.LONGVARBINARY:
        return "LONGVARBINARY";

    case Types.LONGVARCHAR:
        return "LONGVARCHAR";

    case Types.NULL:
        return "NULL";

    case Types.NUMERIC:
        return "NUMERIC";

    case Types.OTHER:
        return "OTHER";

    case Types.REAL:
        return "REAL";

    case Types.REF:
        return "REF";

    case Types.SMALLINT:
        return "SMALLINT";

    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:
        return "*unsupported type*";
    }
}

From source file:nl.nn.adapterframework.jdbc.JdbcFacade.java

protected void applyParameters(PreparedStatement statement, ParameterValueList parameters)
        throws SQLException, SenderException {
    // statement.clearParameters();

    /*//from   www  .  j  a v a  2s.c  om
          // getParameterMetaData() is not supported on the WebSphere java.sql.PreparedStatement implementation.
          int senderParameterCount = parameters.size();
          int statementParameterCount = statement.getParameterMetaData().getParameterCount();
          if (statementParameterCount<senderParameterCount) {
             throw new SenderException(getLogPrefix()+"statement has more ["+statementParameterCount+"] parameters defined than sender ["+senderParameterCount+"]");
          }
    */

    for (int i = 0; i < parameters.size(); i++) {
        ParameterValue pv = parameters.getParameterValue(i);
        String paramType = pv.getDefinition().getType();
        Object value = pv.getValue();
        //      log.debug("applying parameter ["+(i+1)+","+parameters.getParameterValue(i).getDefinition().getName()+"], value["+parameterValue+"]");

        if (Parameter.TYPE_DATE.equals(paramType)) {
            if (value == null) {
                statement.setNull(i + 1, Types.DATE);
            } else {
                statement.setDate(i + 1, new java.sql.Date(((Date) value).getTime()));
            }
        } else if (Parameter.TYPE_DATETIME.equals(paramType)) {
            if (value == null) {
                statement.setNull(i + 1, Types.TIMESTAMP);
            } else {
                statement.setTimestamp(i + 1, new Timestamp(((Date) value).getTime()));
            }
        } else if (Parameter.TYPE_TIMESTAMP.equals(paramType)) {
            if (value == null) {
                statement.setNull(i + 1, Types.TIMESTAMP);
            } else {
                statement.setTimestamp(i + 1, new Timestamp(((Date) value).getTime()));
            }
        } else if (Parameter.TYPE_TIME.equals(paramType)) {
            if (value == null) {
                statement.setNull(i + 1, Types.TIME);
            } else {
                statement.setTime(i + 1, new java.sql.Time(((Date) value).getTime()));
            }
        } else if (Parameter.TYPE_XMLDATETIME.equals(paramType)) {
            if (value == null) {
                statement.setNull(i + 1, Types.TIMESTAMP);
            } else {
                statement.setTimestamp(i + 1, new Timestamp(((Date) value).getTime()));
            }
        } else if (Parameter.TYPE_NUMBER.equals(paramType)) {
            if (value == null) {
                statement.setNull(i + 1, Types.NUMERIC);
            } else {
                statement.setDouble(i + 1, ((Number) value).doubleValue());
            }
        } else if (Parameter.TYPE_INTEGER.equals(paramType)) {
            if (value == null) {
                statement.setNull(i + 1, Types.INTEGER);
            } else {
                statement.setInt(i + 1, (Integer) value);
            }
        } else if (Parameter.TYPE_INPUTSTREAM.equals(paramType)) {
            if (value instanceof FileInputStream) {
                FileInputStream fis = (FileInputStream) value;
                long len = 0;
                try {
                    len = fis.getChannel().size();
                } catch (IOException e) {
                    log.warn(getLogPrefix() + "could not determine file size", e);
                }
                statement.setBinaryStream(i + 1, fis, (int) len);
            } else if (value instanceof ByteArrayInputStream) {
                ByteArrayInputStream bais = (ByteArrayInputStream) value;
                long len = bais.available();
                statement.setBinaryStream(i + 1, bais, (int) len);
            } else {
                throw new SenderException(getLogPrefix() + "unknown inputstream [" + value.getClass()
                        + "] for parameter [" + pv.getDefinition().getName() + "]");
            }
        } else if ("string2bytes".equals(paramType)) {
            statement.setBytes(i + 1, ((String) value).getBytes());
        } else if ("bytes".equals(paramType)) {
            statement.setBytes(i + 1, (byte[]) value);
        } else {
            statement.setString(i + 1, (String) value);
        }
    }
}

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

@Override
public int guessType(Connection conn, String schema, String tableName, String colName) throws SQLException {
    if (metaData == null) {
        metaData = conn.getMetaData();//from   w  w  w .j  a  va 2s.  co  m
    }

    ResultSet rs = null;
    try {
        rs = metaData.getColumns(null, normalizeSchemaName(schema), normalizeTableName(tableName),
                normalizeColumnName(colName));

        if (!rs.next()) {
            throw new SQLException(tableName + "?" + colName + "????");
        }
        String type = rs.getString("TYPE_NAME");
        if (!isUsableType(type)) {
            System.err.println("[WARN] " + tableName + "." + colName + "  " + type
                    + "???????");
            return UN_USABLE_TYPE;
        } else if ("VARCHAR2".equals(type) || "NVARCHAR2".equals(type) || "NCHAR".equals(type)) {
            return Types.VARCHAR;
        } else if ("DATE".equals(type)) { // Types.TIMESTAMP????
            return Types.DATE;
        } else if (type.startsWith("TIMESTAMP")) { // TIMESTAMP(6)?Types.OTHER????
            return Types.TIMESTAMP;
        }
        return rs.getInt("DATA_TYPE");
    } finally {
        if (rs != null) {
            rs.close();
        }
    }
}

From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java

public Map<String, String> getMinimumOffsetValues(Connection connection, String schema, String tableName,
        QuoteChar quoteChar, Collection<String> offsetColumnNames) throws SQLException {
    Map<String, String> minOffsetValues = new HashMap<>();
    final String qualifiedName = TableContextUtil.getQuotedQualifiedTableName(schema, tableName,
            quoteChar.getQuoteCharacter());
    for (String offsetColumn : offsetColumnNames) {
        final String minOffsetQuery = String.format(MIN_OFFSET_VALUE_QUERY, offsetColumn, qualifiedName);
        try (Statement st = connection.createStatement(); ResultSet rs = st.executeQuery(minOffsetQuery)) {
            if (rs.next()) {
                String minValue = null;
                final int colType = rs.getMetaData().getColumnType(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                switch (colType) {
                case Types.DATE:
                    java.sql.Date date = rs.getDate(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    if (date != null) {
                        minValue = String.valueOf(date.toInstant().toEpochMilli());
                    }//from   www .java 2 s. c  o m
                    break;
                case Types.TIME:
                    java.sql.Time time = rs.getTime(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    if (time != null) {
                        minValue = String.valueOf(time.toInstant().toEpochMilli());
                    }
                    break;
                case Types.TIMESTAMP:
                    Timestamp timestamp = rs.getTimestamp(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    if (timestamp != null) {
                        final Instant instant = timestamp.toInstant();
                        minValue = String.valueOf(instant.toEpochMilli());
                    }
                    break;
                default:
                    minValue = rs.getString(MIN_OFFSET_VALUE_QUERY_RESULT_SET_INDEX);
                    break;
                }
                if (minValue != null) {
                    minOffsetValues.put(offsetColumn, minValue);
                }
            } else {
                LOG.warn("Unable to get minimum offset value using query {}; result set had no rows",
                        minOffsetQuery);
            }
        }
    }

    return minOffsetValues;
}

From source file:org.jumpmind.db.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 a v a  2  s.  co  m
 * @return The parsed default value
 */
public Object getParsedDefaultValue() {
    if ((defaultValue != null) && (defaultValue.length() > 0)) {
        try {
            switch (mappedTypeCode) {
            case Types.TINYINT:
            case Types.SMALLINT:
                return new Short(defaultValue);
            case Types.INTEGER:
                try {
                    return new Integer(defaultValue);
                } catch (NumberFormatException e) {
                    return new Long(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:
                return FormatUtils.toBoolean(defaultValue);
            default:
                if (PlatformUtils.supportsJava14JdbcTypes()
                        && (mappedTypeCode == PlatformUtils.determineBooleanTypeCode())) {
                    return FormatUtils.toBoolean(defaultValue);
                }
                break;
            }
        } catch (NumberFormatException ex) {
            return null;
        } catch (IllegalArgumentException ex) {
            return null;
        }
    }
    return defaultValue;
}

From source file:org.sakaiproject.webservices.SakaiReport.java

protected String toJsonString(ResultSet rs) throws SQLException, JSONException {
    ResultSetMetaData rsmd = rs.getMetaData();
    JSONArray array = new JSONArray();
    int numColumns = rsmd.getColumnCount();

    while (rs.next()) {

        JSONObject obj = new JSONObject();
        for (int i = 1; i < numColumns + 1; i++) {

            String column_label = rsmd.getColumnLabel(i);

            LOG.debug("Column Name=" + column_label + ",type=" + rsmd.getColumnType(i));

            switch (rsmd.getColumnType(i)) {
            case Types.ARRAY:
                obj.put(column_label, rs.getArray(i));
                break;
            case Types.BIGINT:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.BOOLEAN:
                obj.put(column_label, rs.getBoolean(i));
                break;
            case Types.BLOB:
                obj.put(column_label, rs.getBlob(i));
                break;
            case Types.DOUBLE:
                obj.put(column_label, rs.getDouble(i));
                break;
            case Types.FLOAT:
                obj.put(column_label, rs.getFloat(i));
                break;
            case Types.INTEGER:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.NVARCHAR:
                obj.put(column_label, rs.getNString(i));
                break;
            case Types.VARCHAR:
                obj.put(column_label, rs.getString(i));
                break;
            case Types.TINYINT:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.SMALLINT:
                obj.put(column_label, rs.getInt(i));
                break;
            case Types.DATE:
                obj.put(column_label, rs.getDate(i));
                break;
            case Types.TIMESTAMP:
                obj.put(column_label, rs.getTimestamp(i));
                break;
            default:
                obj.put(column_label, rs.getObject(i));
                break;
            }/*  w w  w  .  ja  va 2s . c om*/

        }
        array.put(obj);

    }
    return array.toString();
}