Retrieve mysql table comment using DatabaseMetaData

浪子不回头ぞ 提交于 2019-12-07 22:03:45

问题


So I'm using Vaadin Java web framework for a project which requires the ability to edit the table. Vaadin provides a way to get Connection object from SimpleJDBCConnectionPool (Here's the API)

From the Connection I can get DatabaseMetaData object. And I have the following code:

private List<String> getTableNames(DatabaseMetaData md) throws SQLException {
        ArrayList<String> tables = new ArrayList<String>();
        ResultSet rs = md.getTables(null, null, "", null);
        while (rs.next()) {
            tables.add(rs.getString("TABLE_NAME")); //Column 3 is for table name
            Logger.getLogger(CodeContainingClass.class.getName()).
                    info("Comment: " + rs.getString("REMARKS")); //Column 5 is for remarks
        }
        return tables;
}

It retrieves the Table name correctly, but unfortunately the REMARKS returns null. (Here's the API). I'm not sure what I'm doing wrong.

I verified that the table has a comment using the following query:

SHOW TABLE STATUS WHERE Name='tablename';

Any help will be greatly appreciated. Thank you very much.


回答1:


See this MySQL Connector/J bug (specifically the comment at 27 Jun 2012 11:26 and 28 Jun 2012 11:18). You need to specify connection property useInformationSchema=true to get this functionality.

See also the Connector/J Connection properties




回答2:


In MySQL the comment associated with each table is available from the information_schema.

Try this query:

 SELECT TABLE_NAME, TABLE_COMMENT
   FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_SCHEMA = 'YourDatabaseName'

Be sure to substitute the data base name you're actually using.



来源:https://stackoverflow.com/questions/14146230/retrieve-mysql-table-comment-using-databasemetadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!