JAVA中獲取欄位資訊的方法
(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();
}*注意在不使用遊標時應關閉。