Question: I want to write a SpringBatch Tasklet, that automatically activates or de-activates all indexes for a given database table. The code needs to work independantly of the DBMS (SQL Server, Oracle and HSQLDB are required).

This is what I have tried so far:

```
DatabaseMetaData dbMetaData = connection.getMetaData();
ResultSet rs = dbMetaData.getIndexInfo(null, null, tableName, true, false);
while (rs.next()) {
    // work with ResultSet
}

```

However, I do not get the names of the Indexes or any useful information.

So could anyone give some hints on how to set all indexes of table to active or inactive with just a JDBC connection object?

Answer: 
You have to make a difference between primary keys (using `DatabaseMetaData.getPrimaryKeys()` to retrieve) and other indexes (via `dbMetaData.getIndexInfo(null, null, tableName, true, false)`).  

In your loop use:

* `rs.getString("INDEX_NAME")` to extract index name
* `rs.getBoolean("NON_UNIQUE")` to extract unique information
* `rs.getShort("TYPE")` to extract index type
* `rs.getInt("ORDINAL_POSITION")` to extract ordinal position

Use `ORDINAL_POSITION` as key break (when current value is <= of previous one) to detect index change.  

Read [official DatabaseMetaData.getIndexInfo() doc](http://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#getIndexInfo%28%29)
The other way of getting all indexes is using the [information\_schema statistics](http://dev.mysql.com/doc/refman/5.7/en/statistics-table.html) table: 

```
public void GetIndexesOfThisTable(final String tableName) {
    try {
        PreparedStatement ps = conn.prepareStatement("SELECT DISTINCT INDEX_NAME FROM information_schema.statistics WHERE table_name = ?");
        ps.setString(1, tableName);
        ResultSet rs = ps.executeQuery();

        while(rs.next()){
            System.out.println(rs.getString(1));
        }
        rs.close();
        ps.close();//important to close to prevent resource leaks
    }
    catch (Exception ex) {System.out.println(ex.getMessage());}        
}

```