how to get all the columns of a specific table uca

2019-09-04 18:05发布

问题:

This question already has an answer here:

  • Retrieve column names from java.sql.ResultSet 11 answers

I want to get all the column names for a specific table called Impedance2, in an Access database I have. I can only seem to get column metadata I think rather than the actual name:

This is the code I am using:

DatabaseMetaData md = conn.getMetaData();
                        ResultSet rst = md.getTables(null, null, "Impedance2", null);
                        ResultSetMetaData rsmd = rst.getMetaData();
                        System.out.println("Column names as reported by ResultSetMetaData:");
                        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                            System.out.println("Col"+rsmd.getColumnName(i));
                        }

This is the result I am getting:

ColTABLE_CAT
ColTABLE_SCHEM
ColTABLE_NAME
ColTABLE_TYPE
ColREMARKS
ColTYPE_CAT
ColTYPE_SCHEM
ColTYPE_NAME
ColSELF_REFERENCING_COL_NAME
ColREF_GENERATION
ColHSQLDB_TYPE
ColREAD_ONLY
ColCOMMIT_ACTION

回答1:

You're asking for a resultset of tables with their attributes. If you read the metadata of this resultset you'll get the metadata of metadata. Use getColumns to get the table column names and just read the resultset.