Java SQL Table Column tableContainsIndex(Connection conn, String table, String column, String index)

Here you can find the source of tableContainsIndex(Connection conn, String table, String column, String index)

Description

Returns true if the index on the specified column exists for the specified table, false if it does not.

License

Open Source License

Declaration

public static boolean tableContainsIndex(Connection conn, String table, String column, String index)
        throws SQLException 

Method Source Code

//package com.java2s;

import java.sql.Connection;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    /**//  w  ww  .j a v  a2s .  c  om
     * Returns true if the index on the specified column exists for the specified table, false if
     * it does not. Optionally you can specifiy a non null index name, and the table will be
     * checked to see if it contains that specifically named index. <em>Note:</em> the names are
     * case sensitive.
     */
    public static boolean tableContainsIndex(Connection conn, String table, String column, String index)
            throws SQLException {
        boolean matched = false;
        ResultSet rs = conn.getMetaData().getIndexInfo("", "", table, false, true);
        while (rs.next()) {
            String tname = rs.getString("TABLE_NAME");
            String cname = rs.getString("COLUMN_NAME");
            String iname = rs.getString("INDEX_NAME");
            if (index == null) {
                if (tname.equals(table) && cname.equals(column)) {
                    matched = true;
                }
            } else if (index.equals(iname)) {
                matched = true;
            }
        }
        return matched;
    }
}

Related

  1. resultSetToOneColumnAsList(ResultSet rs)
  2. resultSetToStringFormat(ResultSet rs, String separator, String quote, boolean column)
  3. resultSetValue(ResultSet resultSet, Class parameterClass, Object object, String columnLabel)
  4. retrieveCoulmns(String tableName, String columnArray[], Statement statement, long offset, long numberOfRecords)
  5. tableContainsColumn(Connection conn, String table, String column)
  6. toColumnNameList(final ResultSet rs)
  7. toFlag(ResultSetMetaData metaData, int column)
  8. toFormatList(ResultSet results, List column)
  9. toMap(ResultSet rs, List wantedColumnNames)