ColumnMetaDataLoader { public static Collection<ColumnMetaData> load(final Connection connection, final String tableNamePattern, final String databaseType) throws SQLException { Collection<ColumnMetaData> result = new LinkedList<>(); Collection<String> primaryKeys = loadPrimaryKeys(connection, tableNamePattern, databaseType); List<String> columnNames = new ArrayList<>(); List<Integer> columnTypes = new ArrayList<>(); List<String> columnTypeNames = new ArrayList<>(); List<Boolean> isPrimaryKeys = new ArrayList<>(); List<Boolean> isCaseSensitives = new ArrayList<>(); try (ResultSet resultSet = connection.getMetaData().getColumns(connection.getCatalog(), JdbcUtil.getSchema(connection, databaseType), tableNamePattern, "%")) { while (resultSet.next()) { String tableName = resultSet.getString(TABLE_NAME); if (Objects.equals(tableNamePattern, tableName)) { String columnName = resultSet.getString(COLUMN_NAME); columnTypes.add(resultSet.getInt(DATA_TYPE)); columnTypeNames.add(resultSet.getString(TYPE_NAME)); isPrimaryKeys.add(primaryKeys.contains(columnName)); columnNames.add(columnName); } } } try (ResultSet resultSet = connection.createStatement().executeQuery(generateEmptyResultSQL(tableNamePattern, databaseType))) { for (String each : columnNames) { isCaseSensitives.add(resultSet.getMetaData().isCaseSensitive(resultSet.findColumn(each))); } } for (int i = 0; i < columnNames.size(); i++) { result.add(new ColumnMetaData(columnNames.get(i), columnTypes.get(i), columnTypeNames.get(i), isPrimaryKeys.get(i), false, isCaseSensitives.get(i))); } return result; }  static Collection<ColumnMetaData> load(final Connection connection, final String tableNamePattern, final String databaseType);  }
@Test public void assertLoad() throws SQLException { Collection<ColumnMetaData> actual = ColumnMetaDataLoader.load(connection, TEST_TABLE, ""); assertThat(actual.size(), is(2)); Iterator<ColumnMetaData> columnMetaDataIterator = actual.iterator(); assertColumnMetaData(columnMetaDataIterator.next(), "pk_col", Types.INTEGER, "INT", true, true); assertColumnMetaData(columnMetaDataIterator.next(), "col", Types.VARCHAR, "VARCHAR", false, false); }