Example usage for java.sql ResultSet getTime

List of usage examples for java.sql ResultSet getTime

Introduction

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

Prototype

java.sql.Time getTime(String columnLabel, Calendar cal) throws SQLException;

Source Link

Document

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

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. ja v a2  s. c  om*/
 * @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.moqui.impl.entity.EntityJavaUtil.java

public static Object getResultSetValue(ResultSet rs, int index, FieldInfo fi, EntityFacade efi)
        throws EntityException {
    if (fi.typeValue == -1)
        throw new EntityException("No typeValue found for " + fi.entityName + "." + fi.name);

    Object value = null;/*from  ww  w . j a  v a  2s.com*/
    try {
        switch (fi.typeValue) {
        case 1:
            // getMetaData and the column type are somewhat slow (based on profiling), and String values are VERY
            //     common, so only do for text-very-long
            if (fi.isTextVeryLong) {
                ResultSetMetaData rsmd = rs.getMetaData();
                if (Types.CLOB == rsmd.getColumnType(index)) {
                    // if the String is empty, try to get a text input stream, this is required for some databases
                    // for larger fields, like CLOBs
                    Clob valueClob = rs.getClob(index);
                    Reader valueReader = null;
                    if (valueClob != null)
                        valueReader = valueClob.getCharacterStream();
                    if (valueReader != null) {
                        // read up to 4096 at a time
                        char[] inCharBuffer = new char[4096];
                        StringBuilder strBuf = new StringBuilder();
                        try {
                            int charsRead;
                            while ((charsRead = valueReader.read(inCharBuffer, 0, 4096)) > 0) {
                                strBuf.append(inCharBuffer, 0, charsRead);
                            }
                            valueReader.close();
                        } catch (IOException e) {
                            throw new EntityException("Error reading long character stream for field ["
                                    + fi.name + "] of entity [" + fi.entityName + "]", e);
                        }
                        value = strBuf.toString();
                    }
                } else {
                    value = rs.getString(index);
                }
            } else {
                value = rs.getString(index);
            }
            break;
        case 2:
            try {
                value = rs.getTimestamp(index, efi.getCalendarForTzLc());
            } catch (SQLException e) {
                if (logger.isTraceEnabled())
                    logger.trace(
                            "Ignoring SQLException for getTimestamp(), leaving null (found this in MySQL with a date/time value of [0000-00-00 00:00:00]): "
                                    + e.toString());
            }
            break;
        case 3:
            value = rs.getTime(index, efi.getCalendarForTzLc());
            break;
        case 4:
            value = rs.getDate(index, efi.getCalendarForTzLc());
            break;
        case 5:
            int intValue = rs.getInt(index);
            if (!rs.wasNull())
                value = intValue;
            break;
        case 6:
            long longValue = rs.getLong(index);
            if (!rs.wasNull())
                value = longValue;
            break;
        case 7:
            float floatValue = rs.getFloat(index);
            if (!rs.wasNull())
                value = floatValue;
            break;
        case 8:
            double doubleValue = rs.getDouble(index);
            if (!rs.wasNull())
                value = doubleValue;
            break;
        case 9:
            BigDecimal bigDecimalValue = rs.getBigDecimal(index);
            if (!rs.wasNull())
                value = bigDecimalValue != null ? bigDecimalValue.stripTrailingZeros() : null;
            break;
        case 10:
            boolean booleanValue = rs.getBoolean(index);
            if (!rs.wasNull())
                value = booleanValue;
            break;
        case 11:
            Object obj = null;
            byte[] originalBytes = rs.getBytes(index);
            InputStream binaryInput = null;
            if (originalBytes != null && originalBytes.length > 0) {
                binaryInput = new ByteArrayInputStream(originalBytes);
            }
            if (originalBytes != null && originalBytes.length <= 0) {
                logger.warn("Got byte array back empty for serialized Object with length ["
                        + originalBytes.length + "] for field [" + fi.name + "] (" + index + ")");
            }
            if (binaryInput != null) {
                ObjectInputStream inStream = null;
                try {
                    inStream = new ObjectInputStream(binaryInput);
                    obj = inStream.readObject();
                } catch (IOException ex) {
                    if (logger.isTraceEnabled())
                        logger.trace("Unable to read BLOB from input stream for field [" + fi.name + "] ("
                                + index + "): " + ex.toString());
                } catch (ClassNotFoundException ex) {
                    if (logger.isTraceEnabled())
                        logger.trace("Class not found: Unable to cast BLOB data to an Java object for field ["
                                + fi.name + "] (" + index
                                + "); most likely because it is a straight byte[], so just using the raw bytes: "
                                + ex.toString());
                } finally {
                    if (inStream != null) {
                        try {
                            inStream.close();
                        } catch (IOException e) {
                            throw new EntityException("Unable to close binary input stream for field ["
                                    + fi.name + "] (" + index + "): " + e.toString(), e);
                        }
                    }
                }
            }
            if (obj != null) {
                value = obj;
            } else {
                value = originalBytes;
            }
            break;
        case 12:
            SerialBlob sblob = null;
            try {
                // NOTE: changed to try getBytes first because Derby blows up on getBlob and on then calling getBytes for the same field, complains about getting value twice
                byte[] fieldBytes = rs.getBytes(index);
                if (!rs.wasNull())
                    sblob = new SerialBlob(fieldBytes);
                // fieldBytes = theBlob != null ? theBlob.getBytes(1, (int) theBlob.length()) : null
            } catch (SQLException e) {
                if (logger.isTraceEnabled())
                    logger.trace("Ignoring exception trying getBytes(), trying getBlob(): " + e.toString());
                Blob theBlob = rs.getBlob(index);
                if (!rs.wasNull())
                    sblob = new SerialBlob(theBlob);
            }
            value = sblob;
            break;
        case 13:
            value = new SerialClob(rs.getClob(index));
            break;
        case 14:
        case 15:
            value = rs.getObject(index);
            break;
        }
    } catch (SQLException sqle) {
        logger.error("SQL Exception while getting value for field: [" + fi.name + "] (" + index + ")", sqle);
        throw new EntityException(
                "SQL Exception while getting value for field: [" + fi.name + "] (" + index + ")", sqle);
    }

    return value;
}

From source file:com.alibaba.wasp.jdbc.TestPreparedStatement.java

public void testDateTimeTimestampWithCalendar() throws SQLException {
    Statement stat = conn.createStatement();
    stat.execute("create table ts(x timestamp primary key)");
    stat.execute("create table t(x time primary key)");
    stat.execute("create table d(x date)");
    Calendar utcCalendar = new GregorianCalendar(new SimpleTimeZone(0, "Z"));
    TimeZone old = TimeZone.getDefault();
    DateTimeUtils.resetCalendar();/* w  ww  .ja  v  a 2 s  . com*/
    TimeZone.setDefault(TimeZone.getTimeZone("PST"));
    try {
        Timestamp ts1 = Timestamp.valueOf("2010-03-13 18:15:00");
        Time t1 = new Time(ts1.getTime());
        Date d1 = new Date(ts1.getTime());
        // when converted to UTC, this is 03:15, which doesn't actually exist
        // because of summer time change at that day
        Timestamp ts2 = Timestamp.valueOf("2010-03-13 19:15:00");
        Time t2 = new Time(ts2.getTime());
        Date d2 = new Date(ts2.getTime());
        PreparedStatement prep;
        ResultSet rs;
        prep = conn.prepareStatement("insert into ts values(?)");
        prep.setTimestamp(1, ts1, utcCalendar);
        prep.execute();
        prep.setTimestamp(1, ts2, utcCalendar);
        prep.execute();
        prep = conn.prepareStatement("insert into t values(?)");
        prep.setTime(1, t1, utcCalendar);
        prep.execute();
        prep.setTime(1, t2, utcCalendar);
        prep.execute();
        prep = conn.prepareStatement("insert into d values(?)");
        prep.setDate(1, d1, utcCalendar);
        prep.execute();
        prep.setDate(1, d2, utcCalendar);
        prep.execute();
        rs = stat.executeQuery("select * from ts order by x");
        rs.next();
        assertEquals("2010-03-14 02:15:00.0", rs.getString(1));
        assertEquals("2010-03-13 18:15:00.0", rs.getTimestamp(1, utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp(1).toString());
        assertEquals("2010-03-14 02:15:00.0", rs.getString("x"));
        assertEquals("2010-03-13 18:15:00.0", rs.getTimestamp("x", utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp("x").toString());
        rs.next();
        assertEquals("2010-03-14 03:15:00.0", rs.getString(1));
        assertEquals("2010-03-13 19:15:00.0", rs.getTimestamp(1, utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp(1).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getString("x"));
        assertEquals("2010-03-13 19:15:00.0", rs.getTimestamp("x", utcCalendar).toString());
        assertEquals("2010-03-14 03:15:00.0", rs.getTimestamp("x").toString());
        rs = stat.executeQuery("select * from t order by x");
        rs.next();
        assertEquals("02:15:00", rs.getString(1));
        assertEquals("18:15:00", rs.getTime(1, utcCalendar).toString());
        assertEquals("02:15:00", rs.getTime(1).toString());
        assertEquals("02:15:00", rs.getString("x"));
        assertEquals("18:15:00", rs.getTime("x", utcCalendar).toString());
        assertEquals("02:15:00", rs.getTime("x").toString());
        rs.next();
        assertEquals("03:15:00", rs.getString(1));
        assertEquals("19:15:00", rs.getTime(1, utcCalendar).toString());
        assertEquals("03:15:00", rs.getTime(1).toString());
        assertEquals("03:15:00", rs.getString("x"));
        assertEquals("19:15:00", rs.getTime("x", utcCalendar).toString());
        assertEquals("03:15:00", rs.getTime("x").toString());
        rs = stat.executeQuery("select * from d order by x");
        rs.next();
        assertEquals("2010-03-14", rs.getString(1));
        assertEquals("2010-03-13", rs.getDate(1, utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate(1).toString());
        assertEquals("2010-03-14", rs.getString("x"));
        assertEquals("2010-03-13", rs.getDate("x", utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate("x").toString());
        rs.next();
        assertEquals("2010-03-14", rs.getString(1));
        assertEquals("2010-03-13", rs.getDate(1, utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate(1).toString());
        assertEquals("2010-03-14", rs.getString("x"));
        assertEquals("2010-03-13", rs.getDate("x", utcCalendar).toString());
        assertEquals("2010-03-14", rs.getDate("x").toString());
    } finally {
        TimeZone.setDefault(old);
        DateTimeUtils.resetCalendar();
    }
    stat.execute("drop table ts");
    stat.execute("drop table t");
    stat.execute("drop table d");
}

From source file:org.ramadda.repository.database.DatabaseManager.java

/**
 * _more_//from   w w w  . jav  a2 s  .  c  o m
 *
 * @param results _more_
 * @param col _more_
 * @param makeDflt _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public Date getDate(ResultSet results, int col, boolean makeDflt) throws Exception {
    //        if (!db.equals(DB_MYSQL)) {
    if (true || !db.equals(DB_MYSQL)) {
        return getTimestamp(results, col, makeDflt);
    }
    Date date = results.getTime(col, Repository.calendar);
    if (date != null) {
        return date;
    }
    if (makeDflt) {
        return new Date();
    }

    return null;
}

From source file:com.alibaba.wasp.jdbc.TestJdbcResultSet.java

public void testDatetimeWithCalendar() throws SQLException {
    trace("test DATETIME with Calendar");
    ResultSet rs;

    stat = conn.createStatement();/*from w ww. j  a v a  2 s. c  om*/
    stat.execute("CREATE TABLE test(ID INT PRIMARY KEY, D DATE, T TIME, TS TIMESTAMP)");
    PreparedStatement prep = conn.prepareStatement("INSERT INTO test VALUES(?, ?, ?, ?)");
    Calendar regular = Calendar.getInstance();
    Calendar other = null;
    // search a locale that has a _different_ raw offset
    long testTime = java.sql.Date.valueOf("2001-02-03").getTime();
    for (String s : TimeZone.getAvailableIDs()) {
        TimeZone zone = TimeZone.getTimeZone(s);
        long rawOffsetDiff = regular.getTimeZone().getRawOffset() - zone.getRawOffset();
        // must not be the same timezone (not 0 h and not 24 h difference
        // as for Pacific/Auckland and Etc/GMT+12)
        if (rawOffsetDiff != 0 && rawOffsetDiff != 1000 * 60 * 60 * 24) {
            if (regular.getTimeZone().getOffset(testTime) != zone.getOffset(testTime)) {
                other = Calendar.getInstance(zone);
                break;
            }
        }
    }

    trace("regular offset = " + regular.getTimeZone().getRawOffset() + " other = "
            + other.getTimeZone().getRawOffset());

    prep.setInt(1, 0);
    prep.setDate(2, null, regular);
    prep.setTime(3, null, regular);
    prep.setTimestamp(4, null, regular);
    prep.execute();

    prep.setInt(1, 1);
    prep.setDate(2, null, other);
    prep.setTime(3, null, other);
    prep.setTimestamp(4, null, other);
    prep.execute();

    prep.setInt(1, 2);
    prep.setDate(2, java.sql.Date.valueOf("2001-02-03"), regular);
    prep.setTime(3, java.sql.Time.valueOf("04:05:06"), regular);
    prep.setTimestamp(4, Timestamp.valueOf("2007-08-09 10:11:12.131415"), regular);
    prep.execute();

    prep.setInt(1, 3);
    prep.setDate(2, java.sql.Date.valueOf("2101-02-03"), other);
    prep.setTime(3, java.sql.Time.valueOf("14:05:06"), other);
    prep.setTimestamp(4, Timestamp.valueOf("2107-08-09 10:11:12.131415"), other);
    prep.execute();

    prep.setInt(1, 4);
    prep.setDate(2, java.sql.Date.valueOf("2101-02-03"));
    prep.setTime(3, java.sql.Time.valueOf("14:05:06"));
    prep.setTimestamp(4, Timestamp.valueOf("2107-08-09 10:11:12.131415"));
    prep.execute();

    rs = stat.executeQuery("SELECT * FROM test ORDER BY ID");
    assertResultSetMeta(rs, 4, new String[] { "ID", "D", "T", "TS" },
            new int[] { Types.INTEGER, Types.DATE, Types.TIME, Types.TIMESTAMP }, new int[] { 10, 8, 6, 23 },
            new int[] { 0, 0, 0, 10 });

    rs.next();
    assertEquals(0, rs.getInt(1));
    assertTrue(rs.getDate(2, regular) == null && rs.wasNull());
    assertTrue(rs.getTime(3, regular) == null && rs.wasNull());
    assertTrue(rs.getTimestamp(3, regular) == null && rs.wasNull());

    rs.next();
    assertEquals(1, rs.getInt(1));
    assertTrue(rs.getDate(2, other) == null && rs.wasNull());
    assertTrue(rs.getTime(3, other) == null && rs.wasNull());
    assertTrue(rs.getTimestamp(3, other) == null && rs.wasNull());

    rs.next();
    assertEquals(2, rs.getInt(1));
    assertEquals("2001-02-03", rs.getDate(2, regular).toString());
    assertEquals("04:05:06", rs.getTime(3, regular).toString());
    assertFalse(rs.getTime(3, other).toString().equals("04:05:06"));
    assertEquals("2007-08-09 10:11:12.131415", rs.getTimestamp(4, regular).toString());
    assertFalse(rs.getTimestamp(4, other).toString().equals("2007-08-09 10:11:12.131415"));

    rs.next();
    assertEquals(3, rs.getInt("ID"));
    assertFalse(rs.getTimestamp("TS", regular).toString().equals("2107-08-09 10:11:12.131415"));
    assertEquals("2107-08-09 10:11:12.131415", rs.getTimestamp("TS", other).toString());
    assertFalse(rs.getTime("T", regular).toString().equals("14:05:06"));
    assertEquals("14:05:06", rs.getTime("T", other).toString());
    // checkFalse(rs.getDate(2, regular).toString(), "2101-02-03");
    // check(rs.getDate("D", other).toString(), "2101-02-03");

    rs.next();
    assertEquals(4, rs.getInt("ID"));
    assertEquals("2107-08-09 10:11:12.131415", rs.getTimestamp("TS").toString());
    assertEquals("14:05:06", rs.getTime("T").toString());
    assertEquals("2101-02-03", rs.getDate("D").toString());

    assertFalse(rs.next());
    stat.execute("DROP TABLE test");
}