Example usage for java.sql ResultSet getDate

List of usage examples for java.sql ResultSet getDate

Introduction

In this page you can find the example usage for java.sql ResultSet getDate.

Prototype

java.sql.Date getDate(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a java.sql.Date object in the Java programming language.

Usage

From source file:jdbc.JdbcUtils.java

/**
 * Retrieve a JDBC column value from a ResultSet, using the most appropriate
 * value type. The returned value should be a detached value object, not
 * having any ties to the active ResultSet: in particular, it should not be
 * a Blob or Clob object but rather a byte array respectively String
 * representation.//from   www.ja  v a 2  s. com
 * <p>
 * Uses the <code>getObject(index)</code> method, but includes additional
 * "hacks" to get around Oracle 10g returning a non-standard object for its
 * TIMESTAMP datatype and a <code>java.sql.Date</code> for DATE columns
 * leaving out the time portion: These columns will explicitly be extracted
 * as standard <code>java.sql.Timestamp</code> object.
 * 
 * @param rs
 *            is the ResultSet holding the data
 * @param index
 *            is the column index
 * @return the value object
 * @see java.sql.Blob
 * @see java.sql.Clob
 * @see java.sql.Timestamp
 * @see oracle.sql.TIMESTAMP
 */
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
    Object obj = rs.getObject(index);
    if (obj instanceof Blob) {
        obj = rs.getBytes(index);
    } else if (obj instanceof Clob) {
        obj = rs.getString(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) {
        obj = rs.getTimestamp(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) {
        String metaDataClassName = rs.getMetaData().getColumnClassName(index);
        if ("java.sql.Timestamp".equals(metaDataClassName)
                || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
            obj = rs.getTimestamp(index);
        } else {
            obj = rs.getDate(index);
        }
    } else if (obj != null && obj instanceof Date) {
        if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
            obj = rs.getTimestamp(index);
        }
    }
    return obj;
}

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

/**
 * Get value from given ResultSet at given index with given SQL type.
 * /*  w  w w .  java  2s  . co 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:funcoes.funcoes.java

@SuppressWarnings("rawtypes")
public static Vector<Comparable> proximaLinha(ResultSet rs, ResultSetMetaData rsmd) throws SQLException {
    Vector<Comparable> LinhaAtual = new Vector<Comparable>();

    try {/*w  w  w.  j av a 2s . c o  m*/
        for (int i = 1; i <= rsmd.getColumnCount(); ++i) {
            switch (rsmd.getColumnType(i)) {

            case Types.VARCHAR:
                LinhaAtual.addElement(rs.getString(i));
                break;
            case Types.TIMESTAMP:
                LinhaAtual.addElement(rs.getDate(i).toLocaleString().substring(0, 10));
                break;
            case Types.INTEGER:
                LinhaAtual.addElement(rs.getInt(i));
                break;
            case Types.DECIMAL:
                LinhaAtual.addElement(funcoes.paraFormatoDinheiro(rs.getDouble(i)));
                break;
            case Types.DOUBLE:
                LinhaAtual.addElement(funcoes.paraFormatoDinheiro(rs.getDouble(i)));
                break;

            }
        }
    } catch (SQLException e) {
    }
    return LinhaAtual;

}

From source file:com.fusesource.examples.horo.db.typehandler.DateTimeTypeHandler.java

@Override
public DateTime getNullableResult(ResultSet rs, int i) throws SQLException {
    Date date = rs.getDate(i);
    return (date == null) ? null : new DateTime(date.getTime());
}

From source file:com.fusesource.examples.horo.db.typehandler.DateTimeTypeHandler.java

@Override
public DateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
    Date date = rs.getDate(columnName);
    return (date == null) ? null : new DateTime(date.getTime());
}

From source file:ch.iceage.icedms.persistence.jdbc.rowmapper.AbstractGenericRowMapper.java

@Override
public void mapLoggableRow(Loggable loggable, String tableAlias, ResultSet rs, int rowNum) throws SQLException {
    loggable.setCreationDate(rs.getDate(tableAlias + "_creation_date"));
    loggable.setCreationUser(rs.getString(tableAlias + "_creation_user"));
    loggable.setModificationDate(rs.getDate(tableAlias + "_modification_date"));
    loggable.setModificationUser(rs.getString(tableAlias + "_modification_user"));
}

From source file:cn.clickvalue.cv2.model.rowmapper.BeanPropertyRowMapper.java

/**
 * Retrieve a JDBC column value from a ResultSet, using the most appropriate
 * value type. The returned value should be a detached value object, not having
 * any ties to the active ResultSet: in particular, it should not be a Blob or
 * Clob object but rather a byte array respectively String representation.
 * <p>Uses the <code>getObject(index)</code> method, but includes additional "hacks"
 * to get around Oracle 10g returning a non-standard object for its TIMESTAMP
 * datatype and a <code>java.sql.Date</code> for DATE columns leaving out the
 * time portion: These columns will explicitly be extracted as standard
 * <code>java.sql.Timestamp</code> object.
 * @param rs is the ResultSet holding the data
 * @param index is the column index/*from  www .  j  av  a2 s .  c  o  m*/
 * @return the value object
 * @throws SQLException if thrown by the JDBC API
 * @see java.sql.Blob
 * @see java.sql.Clob
 * @see java.sql.Timestamp
 */
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
    Object obj = rs.getObject(index);
    if (obj instanceof Blob) {
        obj = rs.getBytes(index);
    } else if (obj instanceof Clob) {
        obj = rs.getString(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) {
        obj = rs.getTimestamp(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) {
        String metaDataClassName = rs.getMetaData().getColumnClassName(index);
        if ("java.sql.Timestamp".equals(metaDataClassName)
                || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
            obj = rs.getTimestamp(index);
        } else {
            obj = rs.getDate(index);
        }
    } else if (obj != null && obj instanceof java.sql.Date) {
        if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
            obj = rs.getTimestamp(index);
        }
    }
    return obj;
}

From source file:ch.digitalfondue.npjt.mapper.LocalDateMapper.java

@Override
public Object getObject(ResultSet rs) throws SQLException {
    return toLocalDate(rs.getDate(name));
}

From source file:org.onesun.atomator.dao.HashEntryDAOImpl.java

private Date toDateObject(ResultSet resultSet) throws SQLException {
    Date date = resultSet.getDate("time_stamp");
    return date;//from   w ww . ja v  a2 s .c o  m
}

From source file:database.ExerciseMapper.java

@Override
public Exercise mapRow(ResultSet rs, int rowNum) throws SQLException {
    Exercise exercise = new Exercise();
    exercise.setId(rs.getInt("id"));
    exercise.setDate(rs.getDate("date"));
    exercise.setHours(rs.getFloat("hours"));
    exercise.setUserid(rs.getInt("userid"));
    return exercise;
}