當前位置: 首頁>>代碼示例>>Java>>正文


Java Struct.getAttributes方法代碼示例

本文整理匯總了Java中java.sql.Struct.getAttributes方法的典型用法代碼示例。如果您正苦於以下問題:Java Struct.getAttributes方法的具體用法?Java Struct.getAttributes怎麽用?Java Struct.getAttributes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.sql.Struct的用法示例。


在下文中一共展示了Struct.getAttributes方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getValue

import java.sql.Struct; //導入方法依賴的package包/類
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);
  }
}
 
開發者ID:apache,項目名稱:calcite-avatica,代碼行數:61,代碼來源:JdbcResultSet.java

示例2: structAccessor

import java.sql.Struct; //導入方法依賴的package包/類
@Test public void structAccessor() throws Exception {
  // Define the struct type we're creating
  ColumnMetaData intMetaData = MetaImpl.columnMetaData("MY_INT", 1, int.class, false);
  ColumnMetaData stringMetaData = MetaImpl.columnMetaData("MY_STRING", 2, String.class, true);
  StructType structType = ColumnMetaData.struct(Arrays.asList(intMetaData, stringMetaData));
  // Create some structs
  Struct struct1 = new StructImpl(Arrays.<Object>asList(1, "one"));
  Struct struct2 = new StructImpl(Arrays.<Object>asList(2, "two"));
  Struct struct3 = new StructImpl(Arrays.<Object>asList(3));
  Struct struct4 = new StructImpl(Arrays.<Object>asList(4, "four", "ignored"));
  ColumnMetaData structMetaData = MetaImpl.columnMetaData("MY_STRUCT", 1, structType, false);
  List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(struct1),
      Collections.<Object>singletonList(struct2), Collections.<Object>singletonList(struct3),
      Collections.<Object>singletonList(struct4));
  // Create four rows, each with one (struct) column
  try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
    List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(structMetaData),
        Unsafe.localCalendar(), null);
    assertEquals(1, accessors.size());
    Accessor accessor = accessors.get(0);

    assertTrue(cursor.next());
    Struct s = accessor.getObject(Struct.class);
    Object[] structData = s.getAttributes();
    assertEquals(2, structData.length);
    assertEquals(1, structData[0]);
    assertEquals("one", structData[1]);

    assertTrue(cursor.next());
    s = accessor.getObject(Struct.class);
    structData = s.getAttributes();
    assertEquals(2, structData.length);
    assertEquals(2, structData[0]);
    assertEquals("two", structData[1]);

    assertTrue(cursor.next());
    s = accessor.getObject(Struct.class);
    structData = s.getAttributes();
    assertEquals(1, structData.length);
    assertEquals(3, structData[0]);

    assertTrue(cursor.next());
    s = accessor.getObject(Struct.class);
    structData = s.getAttributes();
    assertEquals(3, structData.length);
    assertEquals(4, structData[0]);
    assertEquals("four", structData[1]);
    // We didn't provide metadata, but we still expect to see it.
    assertEquals("ignored", structData[2]);
  }
}
 
開發者ID:apache,項目名稱:calcite-avatica,代碼行數:52,代碼來源:StructImplTest.java


注:本文中的java.sql.Struct.getAttributes方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。