Java SQL Table Column getIndexColumns(Connection connection, String schema, String table, String indexName)

Here you can find the source of getIndexColumns(Connection connection, String schema, String table, String indexName)

Description

Returns list of column names for the specified index

License

Open Source License

Parameter

Parameter Description
indexName name of the index

Return

list of index columns; can return empty list; never NULL

Declaration

public static List<String> getIndexColumns(Connection connection, String schema, String table, String indexName)
        throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.sql.*;
import java.util.*;

public class Main {
    /**/*www  .  j a v  a 2 s  . c o  m*/
     * Returns list of column names for the specified index
     *
     * @param indexName name of the index
     * @return list of index columns; can return empty list; never NULL
     */
    public static List<String> getIndexColumns(Connection connection, String schema, String table, String indexName)
            throws SQLException {
        final Map<Integer, String> columns = new TreeMap<>();

        ResultSet rs = null;
        try {
            rs = connection.getMetaData().getIndexInfo(null, schema, table, true, false);
            while (rs.next()) {
                final String name = rs.getString("INDEX_NAME");
                if (name != null && name.equals(indexName)) {
                    columns.put(rs.getInt("ORDINAL_POSITION"), rs.getString("COLUMN_NAME"));
                }
            }
        } finally {
            closeQuietly(rs);
        }
        return new ArrayList<>(columns.values());
    }

    /**
     * Closes provided {@link ResultSet} without throwing exception
     *
     * @param rs {@link ResultSet} to close
     */
    public static void closeQuietly(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (Exception e) {
                //ignore
            }
        }
    }

    /**
     * Closes provided {@link Statement} without throwing exception
     *
     * @param statement {@link Statement} to close
     */
    public static void closeQuietly(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
                //ignore
            }
        }
    }

    /**
     * Closes provided {@link Connection} without throwing exception
     *
     * @param connection {@link Connection} to close
     */
    public static void closeQuietly(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                //ignore
            }
        }
    }
}

Related

  1. getColumnTypes(ResultSet rs)
  2. getColumnValue(ResultSet rs, ResultSetMetaData rsmd, int colIndex)
  3. getColumnValue(String column, String defaultValue, Map columnIndexMap, ResultSet res)
  4. getColumnValueFromResultSet(int columnIndex, int argType, ResultSet rs)
  5. getColunmNames(ResultSetMetaData rsmd)
  6. getIndexName(Connection conn, String table, String column)
  7. getRange(Connection C, String table, String column)
  8. hasColumn(final ResultSet row, final String columnName)
  9. hasColumn(ResultSet rs, String columnName)