一、JDBC之DatabaseMetaData之获取表信息以及列信息 二、Select类型之获取表中列信息

              一、JDBC之DatabaseMetaData之获取表信息以及列信息

    (1) DatabaseMetaData实例的获取

        Connection conn = DriverManager.getConnection("DataBase");
        DatabaseMetaData dbmd = Conn.getMetaData();

   (2) 获得当前数据库中表的信息

        dbmd.getTables(String catalog,String schema,String tableName,String[] types),

       该方法带有四个参数,它们表示的含义如下:
        String catalog:要获得表所在的编目。Null表示所有编目。

        String schema:要获得表所在的模式。Null表示所有模式。

        String tableName:指出要返回表名与该参数匹配的那些表,

        String types:一个指出返回何种表的数组。

        可能的数组项是:"TABLE"、"VIEW"、"SYSTEM TABLE", "GLOBAL TEMPORARY","LOCAL  TEMPORARY","ALIAS","SYSNONYM"。

        通过getTables()方法返回的结果集中的每个表都有下面是10字段的描述信息。

        1.TABLE_CAT        (String)   => 表所在的编目(可能为空)  

        2.TABLE_SCHEM (String)   => 表所在的模式(可能为空) 

        3.TABLE_NAME    (String)   => 表的名称

        4.TABLE_TYPE     (String)    => 表的类型。

                典型的有 "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL  TEMPORARY", "ALIAS", "SYNONYM". 

        5.REMARKS          (String)       => 解释性的备注

        6.TYPE_CAT          (String)      =>编目类型(may be null) 

        7.TYPE_SCHEM   (String)      => 模式类型(may be null) 

        8.TYPE_NAME      (String)      => 类型名称(may be null) 

        9.SELF_REFERENCING_COL_NAME    (String) => name of the designated "identifier" column of a typed table (may be null) 

       10.REF_GENERATION   (String)    => specifies how values in SELF_REFERENCING_COL_NAME are created.

                   它的值有:"SYSTEM"、"USER"、"DERIVED",也可能为空。

  (3) 获得某个表的列信息

        dbmd.getColumns(String catalog,String schama,String tablename,String columnPattern)

        通过getColumns()方法返回的结果集中的每一列都有下面是23个字段段的描述信息。而且在结果集中直接使用下面字段前面的序号即可获取字段值。

        1.TABLE_CAT String => table catalog (may be null)

        2.TABLE_SCHEM String => table schema (may be null)

        3.TABLE_NAME String => table name (表名称)

        4.COLUMN_NAME String => column name(列名)

        5.DATA_TYPE int => SQL type from java.sql.Types(列的数据类型)

        6.TYPE_NAME String => (数据库中的列对应的类型。如DATE,STRING等)

        7.COLUMN_SIZE int => column size.

        8.BUFFER_LENGTH is not used.

        9.DECIMAL_DIGITS int => the number of fractional digits. Null is returned for data types where DECIMAL_DIGITS is not applicable.

       10.NUM_PREC_RADIX int => Radix (typically either 10 or 2)

       11.NULLABLE int => is NULL allowed.

       12.REMARKS String => comment describing column (may be null)

       13.COLUMN_DEF String => default value for the column, (may be null)

       14.SQL_DATA_TYPE int => unused

       15.SQL_DATETIME_SUB int => unused

       16.CHAR_OCTET_LENGTH int => for char types the maximum number of bytes in the column

       17.ORDINAL_POSITION int => index of column in table (starting at 1)

       18.IS_NULLABLE String => ISO rules are used to determine the nullability for a column.

       19.SCOPE_CATLOG String => catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)

        20.SCOPE_SCHEMA String => schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)

        21.SCOPE_TABLE String => table name that this the scope of a reference attribure (null if the DATA_TYPE isn't REF)

        22.SOURCE_DATA_TYPE short => source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types

       23.IS_AUTOINCREMENT String => Indicates whether this column is auto incremented

        二、Select类型之获取表中列信息

String sql = "select * from " + "TABLE_NAME";//假设此时有许多表
PreparedStatement stmt = null;
try {
stmt = connect.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
ResultSetMetaData data = rs.getMetaData();
while (rs.next()) {
for (int i = 1; i <= data.getColumnCount(); i++) {
String columnTypeName = data.getColumnTypeName(i);
String columnName = data.getColumnName(i);
}
}
}
} catch (Exception e) {
} finally {
stmt.close();
}*注意在不使用游标时应关闭。


猜你喜欢

转载自blog.csdn.net/qq_38340127/article/details/77934218