我的需求:
就是用java.sql包中的类来实现,查询mysql数据库中的所有表的结构信息。
比如:我用程序连接到mysql数据库,然后查询数据库中的所有数据库中的表,也要查询出每个表中的字段名,类型,长度,这些信息,然后放到我的GUI界面中显示。我用JTree来显示这些数据。。
就相当于mysql管理工具一样,显示这些结构关系。能不能提供相关的信息供我参考一下。说明越详细越好。

解决方案 »

  1.   

    DatabaseMetaData metaData = connection.getMetaData();看DatabaseMetaData的APIDOC
      

  2.   

    http://exampledepot.com/taxonomy/term/213 这个网页有下面一些例子
    Getting the Maximum Table Name Length in a Database
    Getting the Name of a JDBC Type
    Listing All Non-SQL92 Keywords Used by a Database
    Listing Available SQL Types Used by a Database
    Listing the Numeric Functions Supported by a Database
    Listing the String Functions Supported by a Database
    Listing the System Functions Supported by a Database
    Listing the Time and Date Functions Supported by a Database
      

  3.   

    通过执行SQL语句。
    show tables; --显示所有表
    show columns from `TABLE_NAME`; --显示指定表的所有列信息。
      

  4.   

    执行SQL语句,然后显示在JTable中就可以
      

  5.   

    desc 表名
    返回的就是一个普通的结果集。显示一下就可以了。
      

  6.   

    Creating a DatabaseMetadata Object
    A DatabaseMetaData object is created with the Connection method
    getMetaData. Once created, it can be used to dynamically discover information
    about the underlying data source. CODE EXAMPLE 7-1 creates a DatabaseMetadata
    object and uses it to determine the maximum number of characters allowed for a
    table name.
    // con is a Connection object
    DatabaseMetaData dbmd = con.getMetadata();
    int maxLen = dbmd.getMaxTableNameLength();
    CODE EXAMPLE 7-1 Creating and using a DatabaseMetadata objectSQL Objects and Their Attributes
    Some DatabaseMetaData methods provide information about the SQL objects that
    populate a given data source. This group also includes methods to determine the
    attributes of those objects. Methods in this group return ResultSet objects in
    which each row describes a particular object. For example, the method getUDTs
    returns a ResultSet object in which there is a row for each UDT that has been
    defined in the data source. Examples of this category are:
    ■ getSchemas
    ■ getCatalogs
    ■ getTables
    ■ getPrimaryKeys
    ■ getProcedures
    ■ getProcedureColumns
    ■ getUDTs
    ■ getFunctions
    ■ getFunctionColumns// -- more information refer to JSR221 - JDBC™ 4.0 Specification