JDBC元数据操作(一)-- DatabaseMetaData接口详解
轉自:?https://blog.csdn.net/chen_zw/article/details/18816599?
?
1. 前言
????在JDBC技術規范中,提供了Connection,Statement,ResultSet這三個開發過程中經常用到的接口。針對與每個接口,JDBC規范提供了相應的接口描述對象,也就是xxxMetaData系列描述對象。DatabaseMetaData和ResultSetMetaData就是兩個常用的獲取數據庫元數據相關信息的接口,本文只講解DatabaseMetaData接口獲取元數據的方法。
本文地址:http://blog.csdn.net/chen_zw/article/details/18816599
2.?DatabaseMetaData接口常用的方法:
(1)?ResultSet getTables(String catalog,String schemaPattern,String tableNamePattern,String[] types); ??//獲取表信息
(2)?ResultSet getPrimaryKeys(String catalog,String schema,String table); ?//獲取表主鍵信息
(3)?ResultSet getIndexInfo(String catalog,String schema,String table,boolean unique,boolean approximate); ?//獲取表索引信息
(4)?ResultSet getColumns(String catalog,String schemaPattern,String tableNamePattern,String columnNamePattern); //獲取表列信息
3. Demo封裝演示:
package com.util;
?
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
?
import oracle.jdbc.driver.OracleConnection;
?
/**
?* @Description: JDBC操作元數據示例-- DatabaseMetaData接口
?* @CreateTime: 2014-1-19 下午9:46:44
?* @author: chenzw?
?* @version V1.0
?*/
public class JdbcUtil {
?? ?//獲得驅動 ?
? ? private static String DRIVER = "oracle.jdbc.driver.OracleDriver"; ?
? ? //獲得url ?
? ? private static String URL = "jdbc:oracle:thin:@localhost:test"; ?
? ? //獲得連接數據庫的用戶名 ?
? ? private static String USER = "root"; ?
? ? //獲得連接數據庫的密碼 ?
? ? private static String PASS = "root"; ?
?
? ? static { ?
? ? ? ? try { ??
? ? ? ? ? ? //初始化JDBC驅動并讓驅動加載到jvm中 ?
? ? ? ? ? ? Class.forName(DRIVER); ?
? ? ? ? } catch (ClassNotFoundException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? } ?
?? ?
? ? public static Connection getConnection(){ ?
? ? ? ? Connection conn = null; ?
? ? ? ? try { ??
? ? ? ? ? ? //連接數據庫 ?
? ? ? ? ?? ?
?? ? ? ? ? /*
?? ? ? ? ? ?* 設置可獲取REMARK備注信息
?? ? ? ? ? Properties props =new Properties();
?? ? ? ? ? props.put("remarksReporting","true");
?? ? ? ? ? props.put("user", USER);
?? ? ? ? ? props.put("password", PASS);
?? ? ? ? ? conn =DriverManager.getConnection(URL,props);*/
? ? ? ? ?? ?
? ? ? ? ? ? conn = DriverManager.getConnection(URL,USER,PASS); ?
? ? ? ? ? ? conn.setAutoCommit(true);
? ? ? ? } catch (SQLException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? ? ? return conn; ?
? ? } ?
?
?? ?//關閉連接
? ? public static void close(Object o){ ?
? ? ? ? if (o == null){ ?
? ? ? ? ? ? return; ?
? ? ? ? } ?
? ? ? ? if (o instanceof ResultSet){ ?
? ? ? ? ? ? try { ?
? ? ? ? ? ? ? ? ((ResultSet)o).close(); ?
? ? ? ? ? ? } catch (SQLException e) { ?
? ? ? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? ? ? } ?
? ? ? ? } else if(o instanceof Statement){ ?
? ? ? ? ? ? try { ?
? ? ? ? ? ? ? ? ((Statement)o).close(); ?
? ? ? ? ? ? } catch (SQLException e) { ?
? ? ? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? ? ? } ?
? ? ? ? } else if (o instanceof Connection){ ?
? ? ? ? ? ? Connection c = (Connection)o; ?
? ? ? ? ? ? try { ?
? ? ? ? ? ? ? ? if (!c.isClosed()){ ?
? ? ? ? ? ? ? ? ? ? c.close(); ?
? ? ? ? ? ? ? ? } ?
? ? ? ? ? ? } catch (SQLException e) { ?
? ? ? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? ? ? } ?
? ? ? ? } ? ?
? ? } ?
? ??
? ??
? ? public static void close(ResultSet rs, Statement stmt, ??
? ? ? ? ? ? Connection conn){ ?
? ? ?? ?close(rs); ?
? ? ?? ?close(stmt); ?
? ? ?? ?close(conn); ?
?? ?} ?
? ??
? ? public static void close(ResultSet rs, ??
? ? ? ? ? ? Connection conn){ ?
? ? ?? ?close(rs); ??
? ? ?? ?close(conn); ?
?? ?} ?
? ??
? ??
? ??
? ? /**
? ? ?* @Description: 獲取數據庫相關信息
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午5:09:12?
? ? ?* @throws
? ? ?*/
? ? public static void getDataBaseInfo() { ?
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
? ? ? ? try{ ?
? ? ? ? ?? ? DatabaseMetaData dbmd = conn.getMetaData();
? ? ? ? ?? ? System.out.println("數據庫已知的用戶: "+ dbmd.getUserName()); ? ?
? ? ? ? ? ? ?System.out.println("數據庫的系統函數的逗號分隔列表: "+ dbmd.getSystemFunctions()); ? ?
? ? ? ? ? ? ?System.out.println("數據庫的時間和日期函數的逗號分隔列表: "+ dbmd.getTimeDateFunctions()); ? ?
? ? ? ? ? ? ?System.out.println("數據庫的字符串函數的逗號分隔列表: "+ dbmd.getStringFunctions()); ? ?
? ? ? ? ? ? ?System.out.println("數據庫供應商用于 'schema' 的首選術語: "+ dbmd.getSchemaTerm()); ? ?
? ? ? ? ? ? ?System.out.println("數據庫URL: " + dbmd.getURL()); ? ?
? ? ? ? ? ? ?System.out.println("是否允許只讀:" + dbmd.isReadOnly()); ? ?
? ? ? ? ? ? ?System.out.println("數據庫的產品名稱:" + dbmd.getDatabaseProductName()); ? ?
? ? ? ? ? ? ?System.out.println("數據庫的版本:" + dbmd.getDatabaseProductVersion()); ? ?
? ? ? ? ? ? ?System.out.println("驅動程序的名稱:" + dbmd.getDriverName()); ? ?
? ? ? ? ? ? ?System.out.println("驅動程序的版本:" + dbmd.getDriverVersion()); ?
? ? ? ? ? ? ?
? ? ? ? ? ? ?System.out.println("數據庫中使用的表類型"); ? ?
? ? ? ? ? ? ?rs = dbmd.getTableTypes(); ? ?
? ? ? ? ? ? ?while (rs.next()) { ? ?
? ? ? ? ? ? ? ? ?System.out.println(rs.getString("TABLE_TYPE")); ? ?
? ? ? ? ? ? ?} ? ?
? ? ? ? }catch (SQLException e){ ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?}?
? ? }?
? ??
? ? /**
? ? ?* @Description:獲得數據庫中所有Schemas(對應于oracle中的Tablespace)
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午5:10:35?
? ? ?* @throws
? ? ?*/
? ? public static void getSchemasInfo(){ ?
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
? ? ? ? try{ ?
? ? ? ? ?? ?DatabaseMetaData dbmd = conn.getMetaData();
? ? ? ? ? ? rs = dbmd.getSchemas(); ??
? ? ? ? ? ? while (rs.next()){ ? ??
? ? ? ? ? ? ? ? String tableSchem = rs.getString("TABLE_SCHEM");?
? ? ? ? ? ? ? ? System.out.println(tableSchem); ? ??
? ? ? ? ? ? } ? ??
? ? ? ? } catch (SQLException e){ ?
? ? ? ? ? ? e.printStackTrace(); ? ??
? ? ? ? } finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?} ?
? ? } ?
? ??
? ? /**
? ? ?* @Description: 獲取數據庫中所有的表信息
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午5:08:28?
? ? ?* @throws
? ? ?*/
? ? public static void getTablesList() { ?
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
? ? ? ? try { ?
? ? ? ? ?? ?/**
?? ??? ??? ? * 設置連接屬性,使得可獲取到表的REMARK(備注)
?? ??? ??? ? */
?? ??? ??? ?((OracleConnection)conn).setRemarksReporting(true);?
? ? ? ? ?? ?DatabaseMetaData dbmd = conn.getMetaData();
? ? ? ? ? ? String[] types = { "TABLE" }; ?
? ? ? ? ? ? rs = dbmd.getTables(null, null, "%", types); ?
? ? ? ? ? ? while (rs.next()) { ?
? ? ? ? ? ? ? ? String tableName = rs.getString("TABLE_NAME"); ?//表名 ?
? ? ? ? ? ? ? ? String tableType = rs.getString("TABLE_TYPE"); ?//表類型 ?
? ? ? ? ? ? ? ? String remarks = rs.getString("REMARKS"); ? ? ? //表備注 ?
? ? ? ? ? ? ? ? System.out.println(tableName + " - " + tableType + " - " + remarks); ?
? ? ? ? ? ? } ?
? ? ? ? } catch (SQLException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?}?
? ? } ?
? ??
? ? /**
? ? ?* @Description: 獲取某表信息
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午3:26:30?
? ? ?* @throws
? ? ?*/
? ? public static void getTablesInfo(){
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
?? ??? ?try {
?? ??? ??? ?/**
?? ??? ??? ? * 設置連接屬性,使得可獲取到表的REMARK(備注)
?? ??? ??? ? */
?? ??? ??? ?((OracleConnection)conn).setRemarksReporting(true);?
?? ??? ??? ?DatabaseMetaData dbmd = conn.getMetaData();
?? ??? ??? ?/**
?? ??? ??? ? * 獲取給定類別中使用的表的描述。
?? ??? ??? ? * 方法原型:ResultSet getTables(String catalog,String schemaPattern,String tableNamePattern,String[] types);
?? ??? ??? ? * catalog - 表所在的類別名稱;""表示獲取沒有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對應于Tablespace);""表示獲取沒有模式的列,null標識獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * tableNamePattern - 表名稱;可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * types - 表類型數組; "TABLE"、"VIEW"、"SYSTEM TABLE"、"GLOBAL TEMPORARY"、"LOCAL TEMPORARY"、"ALIAS" 和 "SYNONYM";null表示包含所有的表類型;可包含單字符通配符("_"),或多字符通配符("%");?
?? ??? ??? ? */
?? ??? ??? ?rs = dbmd.getTables(null, null, "CUST_INTER_TF_SERVICE_REQ", new String[]{"TABLE","VIEW"});?
?
?
?? ??? ??? ?while(rs.next()){
?? ??? ??? ??? ? String tableCat = rs.getString("TABLE_CAT"); ?//表類別(可為null)?
?? ??? ??? ??? ? String tableSchemaName = rs.getString("TABLE_SCHEM");//表模式(可能為空),在oracle中獲取的是命名空間,其它數據庫未知 ? ??
?? ??? ??? ??? ? String tableName = rs.getString("TABLE_NAME"); ?//表名 ?
?? ? ? ? ? ? ? ? String tableType = rs.getString("TABLE_TYPE"); ?//表類型,典型的類型是 "TABLE"、"VIEW"、"SYSTEM TABLE"、"GLOBAL TEMPORARY"、"LOCAL TEMPORARY"、"ALIAS" 和 "SYNONYM"。
?? ? ? ? ? ? ? ? String remarks = rs.getString("REMARKS"); ? ? ? //表備注 ?
?? ? ? ? ? ? ? ??
?? ? ? ? ? ? ? ? System.out.println(tableCat + " - " + tableSchemaName + " - " +tableName + " - " + tableType + " - "?
?? ? ? ? ? ? ? ? ? ? ? ?+ remarks); ?
?? ??? ??? ?}
?? ??? ?} catch (Exception ex) {
?? ??? ??? ?ex.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?}
? ? }
?
? ? /**
? ? ?* @Description: 獲取表主鍵信息
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午5:12:53?
? ? ?* @throws
? ? ?*/
? ? public static void getPrimaryKeysInfo() { ?
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
? ? ? ? try{ ?
? ? ? ? ?? ?DatabaseMetaData dbmd = conn.getMetaData();
? ? ? ? ?? ?/**
? ? ? ? ?? ? * 獲取對給定表的主鍵列的描述
? ? ? ? ?? ? * 方法原型:ResultSet getPrimaryKeys(String catalog,String schema,String table);
? ? ? ? ?? ? * catalog - 表所在的類別名稱;""表示獲取沒有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對應于Tablespace);""表示獲取沒有模式的列,null標識獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * table - 表名稱;可包含單字符通配符("_"),或多字符通配符("%");
? ? ? ? ?? ? */
? ? ? ? ? ? rs = dbmd.getPrimaryKeys(null, null, "CUST_INTER_TF_SERVICE_REQ"); ?
? ? ? ? ? ??
? ? ? ? ? ? while (rs.next()){ ?
? ? ? ? ? ? ?? ?String tableCat = rs.getString("TABLE_CAT"); ?//表類別(可為null)?
?? ??? ??? ??? ?String tableSchemaName = rs.getString("TABLE_SCHEM");//表模式(可能為空),在oracle中獲取的是命名空間,其它數據庫未知 ? ??
?? ??? ??? ??? ?String tableName = rs.getString("TABLE_NAME"); ?//表名 ?
?? ??? ??? ??? ?String columnName = rs.getString("COLUMN_NAME");//列名 ?
? ? ? ? ? ? ? ? short keySeq = rs.getShort("KEY_SEQ");//序列號(主鍵內值1表示第一列的主鍵,值2代表主鍵內的第二列) ?
? ? ? ? ? ? ? ? String pkName = rs.getString("PK_NAME"); //主鍵名稱 ? ?
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? System.out.println(tableCat + " - " + tableSchemaName + " - " + tableName + " - " + columnName + " - "
? ? ? ? ? ? ? ? ? ? ? ?+ keySeq + " - " + pkName); ? ??
? ? ? ? ? ? } ?
? ? ? ? }catch (SQLException e){ ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? }finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?}
? ? } ?
? ??
? ? /**
? ? ?* @Description: 獲取表索引信息
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午5:12:04?
? ? ?* @throws
? ? ?*/
? ? public static void getIndexInfo() {?
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
? ? ? ? try{ ?
? ? ? ? ?? ?DatabaseMetaData dbmd = conn.getMetaData();
? ? ? ? ?? ?/**
? ? ? ? ?? ? * 獲取給定表的索引和統計信息的描述
? ? ? ? ?? ? * 方法原型:ResultSet getIndexInfo(String catalog,String schema,String table,boolean unique,boolean approximate)
? ? ? ? ?? ? * catalog - 表所在的類別名稱;""表示獲取沒有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對應于Tablespace);""表示獲取沒有模式的列,null標識獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * table - 表名稱;可包含單字符通配符("_"),或多字符通配符("%");
? ? ? ? ?? ? * unique - 該參數為 true時,僅返回唯一值的索引; 該參數為 false時,返回所有索引;
? ? ? ? ?? ? * approximate - 該參數為true時,允許結果是接近的數據值或這些數據值以外的值;該參數為 false時,要求結果是精確結果;
? ? ? ? ?? ? */
? ? ? ? ? ? rs = dbmd.getIndexInfo(null, null, "CUST_INTER_TF_SERVICE_REQ", false, true); ?
? ? ? ? ? ? while (rs.next()){ ?
? ? ? ? ? ? ?? ?String tableCat = rs.getString("TABLE_CAT"); ?//表類別(可為null)?
?? ??? ??? ??? ?String tableSchemaName = rs.getString("TABLE_SCHEM");//表模式(可能為空),在oracle中獲取的是命名空間,其它數據庫未知 ? ??
?? ??? ??? ??? ?String tableName = rs.getString("TABLE_NAME"); ?//表名 ?
? ? ? ? ? ? ? ? boolean nonUnique = rs.getBoolean("NON_UNIQUE");// 索引值是否可以不唯一,TYPE為 tableIndexStatistic時索引值為 false;
? ? ? ? ? ? ? ? String indexQualifier = rs.getString("INDEX_QUALIFIER");//索引類別(可能為空),TYPE為 tableIndexStatistic 時索引類別為 null;?
? ? ? ? ? ? ? ? String indexName = rs.getString("INDEX_NAME");//索引的名稱 ;TYPE為 tableIndexStatistic 時索引名稱為 null;
? ? ? ? ? ? ? ? /**
? ? ? ? ? ? ? ? ?* 索引類型:?
? ? ? ? ? ? ? ? ?* ?tableIndexStatistic - 此標識與表的索引描述一起返回的表統計信息?
? ? ? ? ? ? ? ? ?* ?tableIndexClustered - 此為集群索引?
? ? ? ? ? ? ? ? ?* ?tableIndexHashed - 此為散列索引?
? ? ? ? ? ? ? ? ?* ?tableIndexOther - 此為某種其他樣式的索引?
? ? ? ? ? ? ? ? ?*/
? ? ? ? ? ? ? ? short type = rs.getShort("TYPE");//索引類型;
? ? ? ? ? ? ? ? short ordinalPosition = rs.getShort("ORDINAL_POSITION");//在索引列順序號;TYPE為 tableIndexStatistic 時該序列號為零;
? ? ? ? ? ? ? ? String columnName = rs.getString("COLUMN_NAME");//列名;TYPE為 tableIndexStatistic時列名稱為 null;
? ? ? ? ? ? ? ? String ascOrDesc = rs.getString("ASC_OR_DESC");//列排序順序:升序還是降序[A:升序; B:降序];如果排序序列不受支持,可能為 null;TYPE為 tableIndexStatistic時排序序列為 null;
? ? ? ? ? ? ? ? int cardinality = rs.getInt("CARDINALITY"); ? //基數;TYPE為 tableIndexStatistic 時,它是表中的行數;否則,它是索引中唯一值的數量。 ??
? ? ? ? ? ? ? ? int pages = rs.getInt("PAGES"); //TYPE為 tableIndexStatisic時,它是用于表的頁數,否則它是用于當前索引的頁數。
? ? ? ? ? ? ? ? String filterCondition = rs.getString("FILTER_CONDITION"); //過濾器條件,如果有的話(可能為 null)。
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? System.out.println(tableCat + " - " + tableSchemaName + " - " + tableName + " - " + nonUnique + " - "?
? ? ? ? ? ? ? ? ? ? ? ?+ indexQualifier + " - " + indexName + " - " + type + " - " + ordinalPosition + " - " + columnName?
? ? ? ? ? ? ? ? ? ? ? ?+ " - " + ascOrDesc + " - " + cardinality + " - " + pages + " - " + filterCondition); ? ??
? ? ? ? ? ? } ? ??
? ? ? ? } catch (SQLException e){ ?
? ? ? ? ? ? e.printStackTrace(); ? ??
? ? ? ? } finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?} ?
? ? } ?
? ??
? ? ?
? ? /**
? ? ?* @Description: 獲取表中列值信息
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午2:55:56?
? ? ?* @throws
? ? ?*/
? ? public static void getColumnsInfo(){
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
?? ??? ?
?? ??? ?try{
?? ??? ??? ?/**
?? ??? ??? ? * 設置連接屬性,使得可獲取到列的REMARK(備注)
?? ??? ??? ? */
?? ??? ??? ?((OracleConnection)conn).setRemarksReporting(true);?
?? ??? ??? ?DatabaseMetaData dbmd = conn.getMetaData();
?? ??? ??? ?/**
?? ??? ??? ? * 獲取可在指定類別中使用的表列的描述。
?? ??? ??? ? * 方法原型:ResultSet getColumns(String catalog,String schemaPattern,String tableNamePattern,String columnNamePattern)
?? ??? ??? ? * catalog - 表所在的類別名稱;""表示獲取沒有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對應于Tablespace);""表示獲取沒有模式的列,null標識獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * tableNamePattern - 表名稱;可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * columnNamePattern - 列名稱; ""表示獲取列名為""的列(當然獲取不到);null表示獲取所有的列;可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? */
?? ??? ??? ?rs =dbmd.getColumns(null, null, "CUST_INTER_TF_SERVICE_REQ", null);
?? ??? ??? ?
?? ??? ??? ?while(rs.next()){
?? ??? ??? ??? ?String tableCat = rs.getString("TABLE_CAT"); ?//表類別(可能為空) ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? String tableSchemaName = rs.getString("TABLE_SCHEM"); ?//表模式(可能為空),在oracle中獲取的是命名空間,其它數據庫未知 ? ??
? ? ? ? ? ? ? ? String tableName_ = rs.getString("TABLE_NAME"); ?//表名 ?
? ? ? ? ? ? ? ? String columnName = rs.getString("COLUMN_NAME"); ?//列名 ?
? ? ? ? ? ? ? ? int dataType = rs.getInt("DATA_TYPE"); ? ? //對應的java.sql.Types的SQL類型(列類型ID) ? ??
? ? ? ? ? ? ? ? String dataTypeName = rs.getString("TYPE_NAME"); ?//java.sql.Types類型名稱(列類型名稱)
? ? ? ? ? ? ? ? int columnSize = rs.getInt("COLUMN_SIZE"); ?//列大小 ?
? ? ? ? ? ? ? ? int decimalDigits = rs.getInt("DECIMAL_DIGITS"); ?//小數位數?
? ? ? ? ? ? ? ? int numPrecRadix = rs.getInt("NUM_PREC_RADIX"); ?//基數(通常是10或2) --未知
? ? ? ? ? ? ? ? /**
? ? ? ? ? ? ? ? ?* ?0 (columnNoNulls) - 該列不允許為空
? ? ? ? ? ? ? ? ?* ?1 (columnNullable) - 該列允許為空
? ? ? ? ? ? ? ? ?* ?2 (columnNullableUnknown) - 不確定該列是否為空
? ? ? ? ? ? ? ? ?*/
? ? ? ? ? ? ? ? int nullAble = rs.getInt("NULLABLE"); ?//是否允許為null ?
? ? ? ? ? ? ? ? String remarks = rs.getString("REMARKS"); ?//列描述 ?
? ? ? ? ? ? ? ? String columnDef = rs.getString("COLUMN_DEF"); ?//默認值 ?
? ? ? ? ? ? ? ? int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH"); ? ?// 對于 char 類型,該長度是列中的最大字節數?
? ? ? ? ? ? ? ? int ordinalPosition = rs.getInt("ORDINAL_POSITION"); ? //表中列的索引(從1開始) ?
? ? ? ? ? ? ? ? /**?
? ? ? ? ? ? ? ? ?* ISO規則用來確定某一列的是否可為空(等同于NULLABLE的值:[ 0:'YES'; 1:'NO'; 2:''; ])
? ? ? ? ? ? ? ? ?* YES -- 該列可以有空值;?
? ? ? ? ? ? ? ? ?* NO -- 該列不能為空;
? ? ? ? ? ? ? ? ?* 空字符串--- 不知道該列是否可為空
? ? ? ? ? ? ? ? ?*/ ?
? ? ? ? ? ? ? ? String isNullAble = rs.getString("IS_NULLABLE"); ?
? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? /**?
? ? ? ? ? ? ? ? ?* 指示此列是否是自動遞增?
? ? ? ? ? ? ? ? ?* YES -- 該列是自動遞增的
? ? ? ? ? ? ? ? ?* NO -- 該列不是自動遞增
? ? ? ? ? ? ? ? ?* 空字串--- 不能確定該列是否自動遞增
? ? ? ? ? ? ? ? ?*/ ?
? ? ? ? ? ? ? ? //String isAutoincrement = rs.getString("IS_AUTOINCREMENT"); ? //該參數測試報錯 ? ?
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? System.out.println(tableCat + " - " + tableSchemaName + " - " + tableName_ + " - " + columnName +?
? ? ? ? ? ? ? ? ?? ??? ?" - " + dataType + " - " + dataTypeName + " - " + columnSize + " - " + decimalDigits + " - "?
? ? ? ? ? ? ? ? ?? ??? ?+ numPrecRadix + " - " + nullAble + " - " + remarks + " - " + columnDef + " - " + charOctetLength
? ? ? ? ? ? ? ? ?? ??? ?+ " - " + ordinalPosition + " - " + isNullAble );?
? ? ? ? ? ? ? ??
?? ??? ??? ?}
?? ??? ?}catch(SQLException ex){
?? ??? ??? ?ex.printStackTrace();
?? ??? ?}finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?}
? ? }
? ??
?? ?/**
?? ? * @Description: TODO
?? ? * @author: chenzw?
?? ? * @CreateTime: 2014-1-17 下午2:47:45
?? ? * @param args?
?? ? * @throws?
?? ? */
?? ?public static void main(String[] args) {
?? ??? ?getDataBaseInfo(); ?//獲取數據庫信息
?? ??? ?getSchemasInfo(); //獲取數據庫所有Schema
?? ??? ?getTablesList(); ?//獲取某用戶下所有的表
?? ??? ?getTablesInfo(); ?//獲取表信息
?? ??? ?getPrimaryKeysInfo(); //獲取表主鍵信息
?? ??? ?getIndexInfo(); ?//獲取表索引信息
?? ??? ?getColumnsInfo(); //獲取表中列值信息
?? ?}
}
Note: (1)JDBC元數據的操作是很消耗性能的,所以應盡量避免使用。
(2)在獲取元數據中的REMARK(即 備注)前,需要設置RemarksReporting屬性為true,否則獲取到的REMARK屬性都是null,設置該屬性有兩種方法,請參閱上面的Demo。
————————————————
版權聲明:本文為CSDN博主「夜之子」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/chen_zw/article/details/18816599
總結
以上是生活随笔為你收集整理的JDBC元数据操作(一)-- DatabaseMetaData接口详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初见端倪是什么意思 初见端倪的意思
- 下一篇: Dom4j完整教程详解