下面列出了java.sql.ResultSet#getTime ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public LocalTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Time time = rs.getTime(columnIndex);
if (time != null) {
return time.toLocalTime();
}
return null;
}
@Override
public LocalTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
Time time = rs.getTime(columnName);
if (time != null) {
return time.toLocalTime();
}
return null;
}
@Test
public void assertGetTimeForColumnLabel() throws SQLException {
for (ResultSet each : resultSets.values()) {
try {
each.getTime(columnName);
fail("Expected an SQLException to be thrown");
} catch (final SQLException exception) {
assertFalse(exception.getMessage().isEmpty());
}
}
}
public Object getResult(ResultSet rs, String columnName) throws SQLException {
Object sqlTime = rs.getTime(columnName);
if (rs.wasNull()) {
return null;
}
else {
return sqlTime;
}
}
private static Time getLastLoginTimeValue(ResultSet rs, String userId) throws SQLException {
String colName = LAST_LOGIN_TIME_PREFIX + userId;
try {
return rs.getTime(colName);
} catch (SQLException e) {
// Ignore COLUMN_NOT_FOUND error b/c it means that this user didn't login
if (e.getErrorCode() == SQLExceptionCode.COLUMN_NOT_FOUND.getErrorCode()) {
return null;
}
throw e;
}
}
protected Object getJavaObject(Class columnJavaType, String columnLabel, ResultSet resultSet) throws SQLException {
if (columnJavaType.equals(String.class)) {
return resultSet.getString(columnLabel);
} else if (columnJavaType.equals(Byte.class)) {
return resultSet.getByte(columnLabel);
} else if (columnJavaType.equals(Integer.class)) {
return resultSet.getInt(columnLabel);
} else if (columnJavaType.equals(Double.class)) {
return resultSet.getDouble(columnLabel);
} else if (columnJavaType.equals(Float.class)) {
return resultSet.getFloat(columnLabel);
} else if (columnJavaType.equals(Short.class)) {
return resultSet.getShort(columnLabel);
} else if (columnJavaType.equals(Boolean.class)) {
return resultSet.getBoolean(columnLabel);
} else if (columnJavaType.equals(byte[].class)) {
return resultSet.getBytes(columnLabel);
} else if (columnJavaType.equals(Long.class)) {
return resultSet.getLong(columnLabel);
} else if (columnJavaType.equals(Date.class)) {
return resultSet.getDate(columnLabel);
} else if (columnJavaType.equals(Time.class)) {
return resultSet.getTime(columnLabel);
} else if (columnJavaType.equals(Timestamp.class)) {
return resultSet.getTimestamp(columnLabel);
} else if (columnJavaType.equals(InputStream.class)) {
Blob blob = resultSet.getBlob(columnLabel);
return blob != null ? blob.getBinaryStream() : null;
} else {
throw new StorageException("type = [" + columnJavaType + "] for column [" + columnLabel + "] not supported.");
}
}
private Object getNetezzaDateValueWorkaround( IDatabase iDatabase, ResultSet resultSet, int index )
throws SQLException, HopDatabaseException {
Object data = null;
int type = resultSet.getMetaData().getColumnType( index );
switch ( type ) {
case Types.TIME: {
data = resultSet.getTime( index );
break;
}
default: {
data = resultSet.getDate( index );
}
}
return data;
}
/**
* This method allows a database dialect to convert database specific data types to Hop data types.
*
* @param rs The result set to use
* @param val The description of the value to retrieve
* @param index the index on which we need to retrieve the value, 0-based.
* @return The correctly converted Hop data type corresponding to the valueMeta description.
* @throws HopDatabaseException
*/
@Override
public Object getValueFromResultSet( ResultSet rs, IValueMeta val, int index ) throws HopDatabaseException {
Object data;
try {
switch ( val.getType() ) {
case IValueMeta.TYPE_TIMESTAMP:
case IValueMeta.TYPE_DATE:
if ( val.getOriginalColumnType() == java.sql.Types.TIMESTAMP ) {
data = rs.getTimestamp( index + 1 );
break; // Timestamp extends java.util.Date
} else if ( val.getOriginalColumnType() == java.sql.Types.TIME ) {
data = rs.getTime( index + 1 );
break;
} else {
data = rs.getDate( index + 1 );
break;
}
default:
return super.getValueFromResultSet( rs, val, index );
}
if ( rs.wasNull() ) {
data = null;
}
} catch ( SQLException e ) {
throw new HopDatabaseException( "Unable to get value '"
+ val.toStringMeta() + "' from database resultset, index " + index, e );
}
return data;
}
public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
Object sqlTime = rs.getTime(columnIndex);
if (rs.wasNull()) {
return null;
}
else {
return sqlTime;
}
}
@Override
public Time getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
return rs.getTime(columnIndex);
}
private Object getRowValue(final ResultSet resultSet, final int columnIndex) throws SQLException {
ResultSetMetaData metaData = resultSet.getMetaData();
switch (metaData.getColumnType(columnIndex)) {
case Types.BOOLEAN:
return resultSet.getBoolean(columnIndex);
case Types.TINYINT:
case Types.SMALLINT:
return resultSet.getInt(columnIndex);
case Types.INTEGER:
if (metaData.isSigned(columnIndex)) {
return resultSet.getInt(columnIndex);
}
return resultSet.getLong(columnIndex);
case Types.BIGINT:
if (metaData.isSigned(columnIndex)) {
return resultSet.getLong(columnIndex);
}
BigDecimal bigDecimal = resultSet.getBigDecimal(columnIndex);
return bigDecimal == null ? null : bigDecimal.toBigInteger();
case Types.NUMERIC:
case Types.DECIMAL:
return resultSet.getBigDecimal(columnIndex);
case Types.FLOAT:
case Types.DOUBLE:
return resultSet.getDouble(columnIndex);
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return resultSet.getString(columnIndex);
case Types.DATE:
return resultSet.getDate(columnIndex);
case Types.TIME:
return resultSet.getTime(columnIndex);
case Types.TIMESTAMP:
return resultSet.getTimestamp(columnIndex);
case Types.CLOB:
return resultSet.getClob(columnIndex);
case Types.BLOB:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
return resultSet.getBlob(columnIndex);
default:
return resultSet.getObject(columnIndex);
}
}
/**
* Delegates to {@link ResultSet#getObject(String, Class)} without throwing an {@link AbstractMethodError}.
*
* @param <T>
* See {@link ResultSet#getObject(String, Class)}
* @param resultSet
* See {@link ResultSet#getObject(String, Class)}
* @param columnLabel
* See {@link ResultSet#getObject(String, Class)}
* @param type
* See {@link ResultSet#getObject(String, Class)}
* @return See {@link ResultSet#getObject(String, Class)}
* @throws SQLException
* See {@link ResultSet#getObject(String, Class)}
* @see ResultSet#getObject(int, Class)
*/
@SuppressWarnings("unchecked")
public static <T> T getObject(final ResultSet resultSet, final String columnLabel, final Class<T> type)
throws SQLException {
try {
return resultSet.getObject(columnLabel, type);
} catch (final AbstractMethodError e) {
// Numbers
if (type == Integer.class) {
return (T) Integer.valueOf(resultSet.getInt(columnLabel));
}
if (type == Long.class) {
return (T) Long.valueOf(resultSet.getLong(columnLabel));
}
if (type == Double.class) {
return (T) Double.valueOf(resultSet.getDouble(columnLabel));
}
if (type == Float.class) {
return (T) Float.valueOf(resultSet.getFloat(columnLabel));
}
if (type == Short.class) {
return (T) Short.valueOf(resultSet.getShort(columnLabel));
}
if (type == BigDecimal.class) {
return (T) resultSet.getBigDecimal(columnLabel);
}
if (type == Byte.class) {
return (T) Byte.valueOf(resultSet.getByte(columnLabel));
}
// Dates
if (type == Date.class) {
return (T) resultSet.getDate(columnLabel);
}
if (type == Time.class) {
return (T) resultSet.getTime(columnLabel);
}
if (type == Timestamp.class) {
return (T) resultSet.getTimestamp(columnLabel);
}
// Streams
if (type == InputStream.class) {
return (T) resultSet.getBinaryStream(columnLabel);
}
if (type == Reader.class) {
return (T) resultSet.getCharacterStream(columnLabel);
}
// Other
if (type == Object.class) {
return (T) resultSet.getObject(columnLabel);
}
if (type == Boolean.class) {
return (T) Boolean.valueOf(resultSet.getBoolean(columnLabel));
}
if (type == Array.class) {
return (T) resultSet.getArray(columnLabel);
}
if (type == Blob.class) {
return (T) resultSet.getBlob(columnLabel);
}
if (type == Clob.class) {
return (T) resultSet.getClob(columnLabel);
}
if (type == Ref.class) {
return (T) resultSet.getRef(columnLabel);
}
if (type == RowId.class) {
return (T) resultSet.getRowId(columnLabel);
}
if (type == SQLXML.class) {
return (T) resultSet.getSQLXML(columnLabel);
}
if (type == URL.class) {
return (T) resultSet.getURL(columnLabel);
}
throw new SQLFeatureNotSupportedException(
String.format("resultSet=%s, columnLabel=%s, type=%s", resultSet, columnLabel, type));
}
}
private static Object getValue(ResultSet resultSet, int type, int j,
Calendar calendar) throws SQLException {
switch (type) {
case Types.BIGINT:
final long aLong = resultSet.getLong(j + 1);
return aLong == 0 && resultSet.wasNull() ? null : aLong;
case Types.INTEGER:
final int anInt = resultSet.getInt(j + 1);
return anInt == 0 && resultSet.wasNull() ? null : anInt;
case Types.SMALLINT:
final short aShort = resultSet.getShort(j + 1);
return aShort == 0 && resultSet.wasNull() ? null : aShort;
case Types.TINYINT:
final byte aByte = resultSet.getByte(j + 1);
return aByte == 0 && resultSet.wasNull() ? null : aByte;
case Types.DOUBLE:
case Types.FLOAT:
final double aDouble = resultSet.getDouble(j + 1);
return aDouble == 0D && resultSet.wasNull() ? null : aDouble;
case Types.REAL:
final float aFloat = resultSet.getFloat(j + 1);
return aFloat == 0D && resultSet.wasNull() ? null : aFloat;
case Types.DATE:
final Date aDate = resultSet.getDate(j + 1, calendar);
return aDate == null
? null
: (int) (aDate.getTime() / DateTimeUtils.MILLIS_PER_DAY);
case Types.TIME:
final Time aTime = resultSet.getTime(j + 1, calendar);
return aTime == null
? null
: (int) (aTime.getTime() % DateTimeUtils.MILLIS_PER_DAY);
case Types.TIMESTAMP:
final Timestamp aTimestamp = resultSet.getTimestamp(j + 1, calendar);
return aTimestamp == null ? null : aTimestamp.getTime();
case Types.ARRAY:
final Array array = resultSet.getArray(j + 1);
if (null == array) {
return null;
}
try {
// Recursively extracts an Array using its ResultSet-representation
return extractUsingResultSet(array, calendar);
} catch (UnsupportedOperationException | SQLFeatureNotSupportedException e) {
// Not every database might implement Array.getResultSet(). This call
// assumes a non-nested array (depends on the db if that's a valid assumption)
return extractUsingArray(array, calendar);
}
case Types.STRUCT:
Struct struct = resultSet.getObject(j + 1, Struct.class);
Object[] attrs = struct.getAttributes();
List<Object> list = new ArrayList<>(attrs.length);
for (Object o : attrs) {
list.add(o);
}
return list;
default:
return resultSet.getObject(j + 1);
}
}
/**
* Get the column value returned by a SELECT statement for a specific entity attribute
* @param resultSet
* @param mapping
* @param entityClass
* @return
*/
public Object getColumnValueFromResultSet(ResultSet resultSet, AttributeMapping mapping, Class entityClass)
{
// calling getObject on Oracle DB returns correct type, however, on SQLLite is alywas return String
// so we need to proper get method
String attrName = mapping.getAttributeName();
Class javaType = EntityUtils.getJavaType(entityClass, attrName);
Object value = null;
try
{
if (resultSet.getObject(mapping.getColumnName()) == null)
{
return value;
}
else if (javaType == String.class)
{
value = resultSet.getString(mapping.getColumnName());
}
else if (javaType == java.util.Date.class || javaType == java.sql.Date.class)
{
// dates are saved to SQLIte as timestamp to not loose time part
Timestamp ts = resultSet.getTimestamp(mapping.getColumnName());
// value = resultSet.getDate(mapping.getColumnName());
// ts can be null when stored using wrong format, should be dd-MM-yyyy hh:mm:ss
if (ts!=null)
{
value = new java.sql.Date(ts.getTime());
}
}
else if (javaType == Time.class || javaType == Timestamp.class)
{
value = resultSet.getTime(mapping.getColumnName());
}
else if (javaType == Integer.class)
{
value = new Integer(resultSet.getInt(mapping.getColumnName()));
}
else if (javaType == Long.class)
{
value = new Long(resultSet.getLong(mapping.getColumnName()));
}
else if (javaType == Float.class)
{
value = new Float(resultSet.getFloat(mapping.getColumnName()));
}
else if (javaType == Double.class)
{
value = new Float(resultSet.getDouble(mapping.getColumnName()));
}
else if (javaType == Double.class)
{
value = new Float(resultSet.getDouble(mapping.getColumnName()));
}
else if (javaType == Clob.class)
{
value = resultSet.getClob(mapping.getColumnName());
}
else if (javaType == Blob.class)
{
value = resultSet.getBlob(mapping.getColumnName());
}
else if (javaType == Short.class)
{
value = new Short(resultSet.getShort(mapping.getColumnName()));
}
else
{
value = resultSet.getObject(mapping.getColumnName());
}
}
catch (SQLException e)
{
throw new AdfException("Error getting SQL resultSet value for column " + mapping.getColumnName() + ": " +
e.getLocalizedMessage(), AdfException.ERROR);
}
return value;
}
public Time getTime(ResultSet r) throws SQLException {
return r.getTime(label);
}
@Override
public Time getNullableResult(ResultSet rs, int index)
throws SQLException {
return rs.getTime(index);
}
private static String getColumnValue(ResultSet rs, int colType, int colIndex)
throws SQLException, IOException{
String value = "";
switch (colType) {
case Types.BIT:
Object bit = rs.getObject(colIndex);
if (bit != null) {
value = String.valueOf(bit);
}
break;
case Types.BOOLEAN:
boolean b = rs.getBoolean(colIndex);
if (!rs.wasNull()) {
value = Boolean.valueOf(b).toString();
}
break;
case Types.CLOB:
Clob c = rs.getClob(colIndex);
if (c != null) {
value = read(c);
}
break;
case Types.BIGINT:
case Types.DECIMAL:
case Types.DOUBLE:
case Types.FLOAT:
case Types.REAL:
case Types.NUMERIC:
BigDecimal bd = rs.getBigDecimal(colIndex);
if (bd != null) {
value = "" + bd.doubleValue();
}
break;
case Types.INTEGER:
case Types.TINYINT:
case Types.SMALLINT:
int intValue = rs.getInt(colIndex);
if (!rs.wasNull()) {
value = "" + intValue;
}
break;
case Types.JAVA_OBJECT:
Object obj = rs.getObject(colIndex);
if (obj != null) {
value = String.valueOf(obj);
}
break;
case Types.DATE:
java.sql.Date date = rs.getDate(colIndex);
if (date != null) {
value = DATE_FORMATTER.format(date);
;
}
break;
case Types.TIME:
Time t = rs.getTime(colIndex);
if (t != null) {
value = t.toString();
}
break;
case Types.TIMESTAMP:
Timestamp tstamp = rs.getTimestamp(colIndex);
if (tstamp != null) {
value = TIMESTAMP_FORMATTER.format(tstamp);
}
break;
case Types.LONGVARCHAR:
case Types.VARCHAR:
case Types.CHAR:
value = rs.getString(colIndex);
break;
default:
value = "";
}
if (value == null) {
value = "";
}
return value;
}
@Override
public Object getDbValue(ResultSet resultSet) throws SQLException {
Date time = resultSet.getTime(fieldSQL);
return resultSet.wasNull() ? null : time;
}
/**
* Check the consistency of a ResultSet column that returns
* a TIMESTAMP value. Can be used for any column of type TIMESTAMP.
*
* @param rs ResultSet holding the column, positioned on a row
* @param column Column with the TIMESTAMP value.
* @return Returns the Time object obtained from the column.
* @throws SQLException
*/
private Timestamp checkTimestampValue(ResultSet rs, int column) throws SQLException
{
assertEquals(java.sql.Types.TIMESTAMP,
rs.getMetaData().getColumnType(column));
Timestamp tsv = rs.getTimestamp(column);
assertEquals(tsv == null, rs.wasNull());
Object ov = rs.getObject(column);
assertEquals(ov == null, rs.wasNull());
if (tsv == null) {
assertNull(ov);
return null;
}
assertTrue(ov instanceof java.sql.Timestamp);
assertEquals(tsv, ov);
Time tv = rs.getTime(column);
assertNotNull(tv);
assertFalse(rs.wasNull());
// Check the date portion is set to 1970/01/01
assertTime1970(tv);
// Check the TIME portion is set to the same as tv
assertTimeEqual(tv, tsv);
String sv = rs.getString(column);
assertNotNull(sv);
assertFalse(rs.wasNull());
// Assert the string converted back into a Time matches the Time returned.
assertEquals("ResultSet String converted to java.sql.Timestamp mismatch",
tsv, Timestamp.valueOf(sv));
return tsv;
}
@SuppressWarnings("unchecked")
@Override
public Object getTuple(ResultSet result)
{
Object obj;
try {
obj = pojoClass.newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
store.disconnect();
throw new RuntimeException(ex);
}
try {
for (int i = 0; i < fieldInfos.size(); i++) {
int type = columnDataTypes.get(i);
ActiveFieldInfo afi = columnFieldSetters.get(i);
switch (type) {
case Types.CHAR:
case Types.VARCHAR:
String strVal = result.getString(i + 1);
((PojoUtils.Setter<Object, String>)afi.setterOrGetter).set(obj, strVal);
break;
case Types.BOOLEAN:
boolean boolVal = result.getBoolean(i + 1);
((PojoUtils.SetterBoolean<Object>)afi.setterOrGetter).set(obj, boolVal);
break;
case Types.TINYINT:
byte byteVal = result.getByte(i + 1);
((PojoUtils.SetterByte<Object>)afi.setterOrGetter).set(obj, byteVal);
break;
case Types.SMALLINT:
short shortVal = result.getShort(i + 1);
((PojoUtils.SetterShort<Object>)afi.setterOrGetter).set(obj, shortVal);
break;
case Types.INTEGER:
int intVal = result.getInt(i + 1);
((PojoUtils.SetterInt<Object>)afi.setterOrGetter).set(obj, intVal);
break;
case Types.BIGINT:
long longVal = result.getLong(i + 1);
((PojoUtils.SetterLong<Object>)afi.setterOrGetter).set(obj, longVal);
break;
case Types.FLOAT:
float floatVal = result.getFloat(i + 1);
((PojoUtils.SetterFloat<Object>)afi.setterOrGetter).set(obj, floatVal);
break;
case Types.DOUBLE:
double doubleVal = result.getDouble(i + 1);
((PojoUtils.SetterDouble<Object>)afi.setterOrGetter).set(obj, doubleVal);
break;
case Types.DECIMAL:
BigDecimal bdVal = result.getBigDecimal(i + 1);
((PojoUtils.Setter<Object, BigDecimal>)afi.setterOrGetter).set(obj, bdVal);
break;
case Types.TIMESTAMP:
Timestamp tsVal = result.getTimestamp(i + 1);
((PojoUtils.Setter<Object, Timestamp>)afi.setterOrGetter).set(obj, tsVal);
break;
case Types.TIME:
Time timeVal = result.getTime(i + 1);
((PojoUtils.Setter<Object, Time>)afi.setterOrGetter).set(obj, timeVal);
break;
case Types.DATE:
Date dateVal = result.getDate(i + 1);
((PojoUtils.Setter<Object, Date>)afi.setterOrGetter).set(obj, dateVal);
break;
default:
handleUnknownDataType(type, obj, afi);
break;
}
}
return obj;
} catch (SQLException e) {
store.disconnect();
throw new RuntimeException("fetching metadata", e);
}
}