使用JDBC检索给定表的所有索引 [英] Retrieve all Indexes for a given Table with JDBC
问题描述
我想编写一个SpringBatch Tasklet,它自动激活或取消激活给定数据库表的所有索引。代码需要独立于DBMS(需要SQL Server,Oracle和HSQLDB)。
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.
所以任何人都可以提供一些关于如何设置所有索引的提示表只有一个JDBC连接对象才能激活或不激活?
So could anyone give some hints on how to set all indexes of table to active or inactive with just a JDBC connection object?
推荐答案
你必须在主键之间做一个区别(使用 DatabaseMetaData.getPrimaryKeys()
要检索)和其他索引(通过 dbMetaData.getIndexInfo(null,null,tableName,true,false)
)。$
在你的循环中使用:
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 )
提取索引名称 -
rs.getBoolean(NON_UNIQUE)
提取唯一信息 -
rs.getShort(TYPE)
提取索引类型 -
rs.getInt(ORDINAL_POSITION)
提取序号位置
rs.getString("INDEX_NAME")
to extract index namers.getBoolean("NON_UNIQUE")
to extract unique informationrs.getShort("TYPE")
to extract index typers.getInt("ORDINAL_POSITION")
to extract ordinal position
使用 ORDINAL_POSITION
作为键中断(当前值为< =前一个)以检测索引更改。
读取官方DatabaseMetaData.getIndexInfo()doc
Use ORDINAL_POSITION
as key break (when current value is <= of previous one) to detect index change.
Read official DatabaseMetaData.getIndexInfo() doc
这篇关于使用JDBC检索给定表的所有索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!