本文整理汇总了Java中java.sql.Array.getResultSet方法的典型用法代码示例。如果您正苦于以下问题:Java Array.getResultSet方法的具体用法?Java Array.getResultSet怎么用?Java Array.getResultSet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.sql.Array
的用法示例。
在下文中一共展示了Array.getResultSet方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractUsingResultSet
import java.sql.Array; //导入方法依赖的package包/类
/**
* Converts an Array into a List using {@link Array#getResultSet()}. This implementation is
* recursive and can parse multi-dimensional arrays.
*/
static List<?> extractUsingResultSet(Array array, Calendar calendar) throws SQLException {
ResultSet arrayValues = array.getResultSet();
TreeMap<Integer, Object> map = new TreeMap<>();
while (arrayValues.next()) {
// column 1 is the index in the array, column 2 is the value.
// Recurse on `getValue` to unwrap nested types correctly.
// `j` is zero-indexed and incremented for us, thus we have `1` being used twice.
map.put(arrayValues.getInt(1), getValue(arrayValues, array.getBaseType(), 1, calendar));
}
// If the result set is not in the same order as the actual Array, TreeMap fixes that.
// Need to make a concrete list to ensure Jackson serialization.
return new ArrayList<>(map.values());
}
示例2: resultSetFromArray
import java.sql.Array; //导入方法依赖的package包/类
@Test public void resultSetFromArray() throws Exception {
// Define the struct type we're creating
ScalarType intType = ColumnMetaData.scalar(Types.INTEGER, "INTEGER", Rep.INTEGER);
ArrayType arrayType = ColumnMetaData.array(intType, "INTEGER", Rep.INTEGER);
ColumnMetaData arrayMetaData = MetaImpl.columnMetaData("MY_ARRAY", 1, arrayType, false);
ArrayImpl.Factory factory = new ArrayFactoryImpl(Unsafe.localCalendar().getTimeZone());
// Create some arrays from the structs
Array array1 = factory.createArray(intType, Arrays.<Object>asList(1, 2));
Array array2 = factory.createArray(intType, Arrays.<Object>asList(3));
Array array3 = factory.createArray(intType, Arrays.<Object>asList(4, 5, 6));
List<List<Object>> rows = Arrays.asList(Collections.<Object>singletonList(array1),
Collections.<Object>singletonList(array2), Collections.<Object>singletonList(array3));
// Create two rows, each with one (array) column
try (Cursor cursor = new ListIteratorCursor(rows.iterator())) {
List<Accessor> accessors = cursor.createAccessors(Collections.singletonList(arrayMetaData),
Unsafe.localCalendar(), factory);
assertEquals(1, accessors.size());
Accessor accessor = accessors.get(0);
assertTrue(cursor.next());
Array actualArray = accessor.getArray();
// An Array's result set has one row per array element.
// Each row has two columns. Column 1 is the array offset (1-based), Column 2 is the value.
ResultSet actualArrayResultSet = actualArray.getResultSet();
assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
assertTrue(actualArrayResultSet.next());
// Order is Avatica implementation specific
assertEquals(1, actualArrayResultSet.getInt(1));
assertEquals(1, actualArrayResultSet.getInt(2));
assertTrue(actualArrayResultSet.next());
assertEquals(2, actualArrayResultSet.getInt(1));
assertEquals(2, actualArrayResultSet.getInt(2));
assertFalse(actualArrayResultSet.next());
assertTrue(cursor.next());
actualArray = accessor.getArray();
actualArrayResultSet = actualArray.getResultSet();
assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
assertTrue(actualArrayResultSet.next());
assertEquals(1, actualArrayResultSet.getInt(1));
assertEquals(3, actualArrayResultSet.getInt(2));
assertFalse(actualArrayResultSet.next());
assertTrue(cursor.next());
actualArray = accessor.getArray();
actualArrayResultSet = actualArray.getResultSet();
assertEquals(2, actualArrayResultSet.getMetaData().getColumnCount());
assertTrue(actualArrayResultSet.next());
assertEquals(1, actualArrayResultSet.getInt(1));
assertEquals(4, actualArrayResultSet.getInt(2));
assertTrue(actualArrayResultSet.next());
assertEquals(2, actualArrayResultSet.getInt(1));
assertEquals(5, actualArrayResultSet.getInt(2));
assertTrue(actualArrayResultSet.next());
assertEquals(3, actualArrayResultSet.getInt(1));
assertEquals(6, actualArrayResultSet.getInt(2));
assertFalse(actualArrayResultSet.next());
assertFalse(cursor.next());
}
}