Retrieve all Indexes for a given Table with JDBC

前端 未结 4 1340
臣服心动
臣服心动 2021-01-19 21:29

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

4条回答
  •  抹茶落季
    2021-01-19 21:40

    The other way of getting all indexes is using the information_schema statistics 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());}        
    }
    

提交回复
热议问题