SqlResultSetReader { public static Table read(ResultSet resultSet) throws SQLException { ResultSetMetaData metaData = resultSet.getMetaData(); Table table = Table.create(); for (int i = 1; i <= metaData.getColumnCount(); i++) { ColumnType type = getColumnType(metaData.getColumnType(i), metaData.getScale(i), metaData.getPrecision(i)); Preconditions.checkState( type != null, "No column type found for %s as specified for column %s", metaData.getColumnType(i), metaData.getColumnName(i)); Column<?> newColumn = type.create(metaData.getColumnLabel(i)); table.addColumns(newColumn); } while (resultSet.next()) { for (int i = 1; i <= metaData.getColumnCount(); i++) { Column<?> column = table.column(i - 1); if (column instanceof ShortColumn) { appendToColumn(column, resultSet, resultSet.getShort(i)); } else if (column instanceof IntColumn) { appendToColumn(column, resultSet, resultSet.getInt(i)); } else if (column instanceof LongColumn) { appendToColumn(column, resultSet, resultSet.getLong(i)); } else if (column instanceof FloatColumn) { appendToColumn(column, resultSet, resultSet.getFloat(i)); } else if (column instanceof DoubleColumn) { appendToColumn(column, resultSet, resultSet.getDouble(i)); } else if (column instanceof BooleanColumn) { appendToColumn(column, resultSet, resultSet.getBoolean(i)); } else { column.appendObj(resultSet.getObject(i)); } } } return table; }  static void mapJdbcTypeToColumnType(Integer jdbc, ColumnType columnType); static Table read(ResultSet resultSet);  }
@Test public void testSqlResultSetReader() throws Exception { final String DB_URL = "jdbc:h2:mem:CoffeeDB"; Connection conn = DriverManager.getConnection(DB_URL); TestDb.dropTables(conn); TestDb.buildCoffeeTable(conn); TestDb.buildCustomerTable(conn); TestDb.buildUnpaidOrderTable(conn); TestDb.buildNumbersTable(conn); TestDb.buildNullValuesTable(conn); try (Statement stmt = conn.createStatement()) { String sql; sql = "SELECT * FROM coffee"; try (ResultSet rs = stmt.executeQuery(sql)) { Table coffee = SqlResultSetReader.read(rs); assertEquals(4, coffee.columnCount()); assertEquals(18, coffee.rowCount()); } sql = "SELECT * FROM Customer"; try (ResultSet rs = stmt.executeQuery(sql)) { Table customer = SqlResultSetReader.read(rs); assertEquals(7, customer.columnCount()); assertEquals(3, customer.rowCount()); } sql = "SELECT * FROM UnpaidOrder"; try (ResultSet rs = stmt.executeQuery(sql)) { Table unpaidInvoice = SqlResultSetReader.read(rs); assertEquals(5, unpaidInvoice.columnCount()); assertEquals(0, unpaidInvoice.rowCount()); } sql = "SELECT * FROM Numbers"; try (ResultSet rs = stmt.executeQuery(sql)) { Table numbers = SqlResultSetReader.read(rs); assertEquals(13, numbers.columnCount()); assertEquals(3, numbers.rowCount()); assertTrue(numbers.column("Description").type() instanceof StringColumnType); assertTrue(numbers.column("NumInt").type() instanceof IntColumnType); assertTrue(numbers.column("NumInt6_0").type() instanceof IntColumnType); assertTrue(numbers.column("NumLong").type() instanceof LongColumnType); assertTrue(numbers.column("NumShort").type() instanceof ShortColumnType); assertTrue(numbers.column("NumNumber").type() instanceof DoubleColumnType); assertTrue(numbers.column("NumBigInt").type() instanceof DoubleColumnType); assertTrue(numbers.column("NumBigDec").type() instanceof DoubleColumnType); assertTrue(numbers.column("NumFloat7_1").type() instanceof FloatColumnType); assertTrue(numbers.column("NumFloat7_7").type() instanceof FloatColumnType); assertTrue(numbers.column("NumDouble7_8").type() instanceof DoubleColumnType); assertTrue(numbers.column("NumDouble7_16").type() instanceof DoubleColumnType); } sql = "SELECT * FROM NullValues"; try (ResultSet rs = stmt.executeQuery(sql)) { Table nullValues = SqlResultSetReader.read(rs); assertEquals(8, nullValues.columnCount()); assertEquals(3, nullValues.rowCount()); assertEquals(2, nullValues.column("StringValue").removeMissing().size()); assertEquals(1, nullValues.column("DoubleValue").removeMissing().size()); assertEquals(2, nullValues.column("IntegerValue").removeMissing().size()); assertEquals(1, nullValues.column("ShortValue").removeMissing().size()); assertEquals(1, nullValues.column("LongValue").removeMissing().size()); assertEquals(1, nullValues.column("FloatValue").removeMissing().size()); assertEquals(1, nullValues.column("BooleanValue").removeMissing().size()); } } }