JDBC元数据操作(一)-- DatabaseMetaData接口详解
轉(zhuǎn)自:?https://blog.csdn.net/chen_zw/article/details/18816599?
?
1. 前言
????在JDBC技術(shù)規(guī)范中,提供了Connection,Statement,ResultSet這三個(gè)開(kāi)發(fā)過(guò)程中經(jīng)常用到的接口。針對(duì)與每個(gè)接口,JDBC規(guī)范提供了相應(yīng)的接口描述對(duì)象,也就是xxxMetaData系列描述對(duì)象。DatabaseMetaData和ResultSetMetaData就是兩個(gè)常用的獲取數(shù)據(jù)庫(kù)元數(shù)據(jù)相關(guān)信息的接口,本文只講解DatabaseMetaData接口獲取元數(shù)據(jù)的方法。
本文地址: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操作元數(shù)據(jù)示例-- DatabaseMetaData接口
?* @CreateTime: 2014-1-19 下午9:46:44
?* @author: chenzw?
?* @version V1.0
?*/
public class JdbcUtil {
?? ?//獲得驅(qū)動(dòng) ?
? ? private static String DRIVER = "oracle.jdbc.driver.OracleDriver"; ?
? ? //獲得url ?
? ? private static String URL = "jdbc:oracle:thin:@localhost:test"; ?
? ? //獲得連接數(shù)據(jù)庫(kù)的用戶名 ?
? ? private static String USER = "root"; ?
? ? //獲得連接數(shù)據(jù)庫(kù)的密碼 ?
? ? private static String PASS = "root"; ?
?
? ? static { ?
? ? ? ? try { ??
? ? ? ? ? ? //初始化JDBC驅(qū)動(dòng)并讓驅(qū)動(dòng)加載到j(luò)vm中 ?
? ? ? ? ? ? Class.forName(DRIVER); ?
? ? ? ? } catch (ClassNotFoundException e) { ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } ?
? ? } ?
?? ?
? ? public static Connection getConnection(){ ?
? ? ? ? Connection conn = null; ?
? ? ? ? try { ??
? ? ? ? ? ? //連接數(shù)據(jù)庫(kù) ?
? ? ? ? ?? ?
?? ? ? ? ? /*
?? ? ? ? ? ?* 設(shè)置可獲取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; ?
? ? } ?
?
?? ?//關(guān)閉連接
? ? 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: 獲取數(shù)據(jù)庫(kù)相關(guān)信息
? ? ?* @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("數(shù)據(jù)庫(kù)已知的用戶: "+ dbmd.getUserName()); ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)的系統(tǒng)函數(shù)的逗號(hào)分隔列表: "+ dbmd.getSystemFunctions()); ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)的時(shí)間和日期函數(shù)的逗號(hào)分隔列表: "+ dbmd.getTimeDateFunctions()); ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)的字符串函數(shù)的逗號(hào)分隔列表: "+ dbmd.getStringFunctions()); ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)供應(yīng)商用于 'schema' 的首選術(shù)語(yǔ): "+ dbmd.getSchemaTerm()); ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)URL: " + dbmd.getURL()); ? ?
? ? ? ? ? ? ?System.out.println("是否允許只讀:" + dbmd.isReadOnly()); ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)的產(chǎn)品名稱:" + dbmd.getDatabaseProductName()); ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)的版本:" + dbmd.getDatabaseProductVersion()); ? ?
? ? ? ? ? ? ?System.out.println("驅(qū)動(dòng)程序的名稱:" + dbmd.getDriverName()); ? ?
? ? ? ? ? ? ?System.out.println("驅(qū)動(dòng)程序的版本:" + dbmd.getDriverVersion()); ?
? ? ? ? ? ? ?
? ? ? ? ? ? ?System.out.println("數(shù)據(jù)庫(kù)中使用的表類型"); ? ?
? ? ? ? ? ? ?rs = dbmd.getTableTypes(); ? ?
? ? ? ? ? ? ?while (rs.next()) { ? ?
? ? ? ? ? ? ? ? ?System.out.println(rs.getString("TABLE_TYPE")); ? ?
? ? ? ? ? ? ?} ? ?
? ? ? ? }catch (SQLException e){ ?
? ? ? ? ? ? e.printStackTrace(); ?
? ? ? ? } finally{
?? ??? ??? ?JdbcUtil.close(rs,conn);
?? ??? ?}?
? ? }?
? ??
? ? /**
? ? ?* @Description:獲得數(shù)據(jù)庫(kù)中所有Schemas(對(duì)應(yīng)于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: 獲取數(shù)據(jù)庫(kù)中所有的表信息
? ? ?* @author: chenzw?
? ? ?* @CreateTime: 2014-1-27 下午5:08:28?
? ? ?* @throws
? ? ?*/
? ? public static void getTablesList() { ?
? ? ?? ?Connection conn = ?getConnection();
?? ??? ?ResultSet rs = null;
? ? ? ? try { ?
? ? ? ? ?? ?/**
?? ??? ??? ? * 設(shè)置連接屬性,使得可獲取到表的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 {
?? ??? ??? ?/**
?? ??? ??? ? * 設(shè)置連接屬性,使得可獲取到表的REMARK(備注)
?? ??? ??? ? */
?? ??? ??? ?((OracleConnection)conn).setRemarksReporting(true);?
?? ??? ??? ?DatabaseMetaData dbmd = conn.getMetaData();
?? ??? ??? ?/**
?? ??? ??? ? * 獲取給定類別中使用的表的描述。
?? ??? ??? ? * 方法原型:ResultSet getTables(String catalog,String schemaPattern,String tableNamePattern,String[] types);
?? ??? ??? ? * catalog - 表所在的類別名稱;""表示獲取沒(méi)有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對(duì)應(yīng)于Tablespace);""表示獲取沒(méi)有模式的列,null標(biāo)識(shí)獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * tableNamePattern - 表名稱;可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * types - 表類型數(shù)組; "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中獲取的是命名空間,其它數(shù)據(jù)庫(kù)未知 ? ??
?? ??? ??? ??? ? 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();
? ? ? ? ?? ?/**
? ? ? ? ?? ? * 獲取對(duì)給定表的主鍵列的描述
? ? ? ? ?? ? * 方法原型:ResultSet getPrimaryKeys(String catalog,String schema,String table);
? ? ? ? ?? ? * catalog - 表所在的類別名稱;""表示獲取沒(méi)有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對(duì)應(yīng)于Tablespace);""表示獲取沒(méi)有模式的列,null標(biāo)識(shí)獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * 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中獲取的是命名空間,其它數(shù)據(jù)庫(kù)未知 ? ??
?? ??? ??? ??? ?String tableName = rs.getString("TABLE_NAME"); ?//表名 ?
?? ??? ??? ??? ?String columnName = rs.getString("COLUMN_NAME");//列名 ?
? ? ? ? ? ? ? ? short keySeq = rs.getShort("KEY_SEQ");//序列號(hào)(主鍵內(nèi)值1表示第一列的主鍵,值2代表主鍵內(nèi)的第二列) ?
? ? ? ? ? ? ? ? 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();
? ? ? ? ?? ?/**
? ? ? ? ?? ? * 獲取給定表的索引和統(tǒng)計(jì)信息的描述
? ? ? ? ?? ? * 方法原型:ResultSet getIndexInfo(String catalog,String schema,String table,boolean unique,boolean approximate)
? ? ? ? ?? ? * catalog - 表所在的類別名稱;""表示獲取沒(méi)有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對(duì)應(yīng)于Tablespace);""表示獲取沒(méi)有模式的列,null標(biāo)識(shí)獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * table - 表名稱;可包含單字符通配符("_"),或多字符通配符("%");
? ? ? ? ?? ? * unique - 該參數(shù)為 true時(shí),僅返回唯一值的索引; 該參數(shù)為 false時(shí),返回所有索引;
? ? ? ? ?? ? * approximate - 該參數(shù)為true時(shí),允許結(jié)果是接近的數(shù)據(jù)值或這些數(shù)據(jù)值以外的值;該參數(shù)為 false時(shí),要求結(jié)果是精確結(jié)果;
? ? ? ? ?? ? */
? ? ? ? ? ? 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中獲取的是命名空間,其它數(shù)據(jù)庫(kù)未知 ? ??
?? ??? ??? ??? ?String tableName = rs.getString("TABLE_NAME"); ?//表名 ?
? ? ? ? ? ? ? ? boolean nonUnique = rs.getBoolean("NON_UNIQUE");// 索引值是否可以不唯一,TYPE為 tableIndexStatistic時(shí)索引值為 false;
? ? ? ? ? ? ? ? String indexQualifier = rs.getString("INDEX_QUALIFIER");//索引類別(可能為空),TYPE為 tableIndexStatistic 時(shí)索引類別為 null;?
? ? ? ? ? ? ? ? String indexName = rs.getString("INDEX_NAME");//索引的名稱 ;TYPE為 tableIndexStatistic 時(shí)索引名稱為 null;
? ? ? ? ? ? ? ? /**
? ? ? ? ? ? ? ? ?* 索引類型:?
? ? ? ? ? ? ? ? ?* ?tableIndexStatistic - 此標(biāo)識(shí)與表的索引描述一起返回的表統(tǒng)計(jì)信息?
? ? ? ? ? ? ? ? ?* ?tableIndexClustered - 此為集群索引?
? ? ? ? ? ? ? ? ?* ?tableIndexHashed - 此為散列索引?
? ? ? ? ? ? ? ? ?* ?tableIndexOther - 此為某種其他樣式的索引?
? ? ? ? ? ? ? ? ?*/
? ? ? ? ? ? ? ? short type = rs.getShort("TYPE");//索引類型;
? ? ? ? ? ? ? ? short ordinalPosition = rs.getShort("ORDINAL_POSITION");//在索引列順序號(hào);TYPE為 tableIndexStatistic 時(shí)該序列號(hào)為零;
? ? ? ? ? ? ? ? String columnName = rs.getString("COLUMN_NAME");//列名;TYPE為 tableIndexStatistic時(shí)列名稱為 null;
? ? ? ? ? ? ? ? String ascOrDesc = rs.getString("ASC_OR_DESC");//列排序順序:升序還是降序[A:升序; B:降序];如果排序序列不受支持,可能為 null;TYPE為 tableIndexStatistic時(shí)排序序列為 null;
? ? ? ? ? ? ? ? int cardinality = rs.getInt("CARDINALITY"); ? //基數(shù);TYPE為 tableIndexStatistic 時(shí),它是表中的行數(shù);否則,它是索引中唯一值的數(shù)量。 ??
? ? ? ? ? ? ? ? int pages = rs.getInt("PAGES"); //TYPE為 tableIndexStatisic時(shí),它是用于表的頁(yè)數(shù),否則它是用于當(dāng)前索引的頁(yè)數(shù)。
? ? ? ? ? ? ? ? String filterCondition = rs.getString("FILTER_CONDITION"); //過(guò)濾器條件,如果有的話(可能為 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{
?? ??? ??? ?/**
?? ??? ??? ? * 設(shè)置連接屬性,使得可獲取到列的REMARK(備注)
?? ??? ??? ? */
?? ??? ??? ?((OracleConnection)conn).setRemarksReporting(true);?
?? ??? ??? ?DatabaseMetaData dbmd = conn.getMetaData();
?? ??? ??? ?/**
?? ??? ??? ? * 獲取可在指定類別中使用的表列的描述。
?? ??? ??? ? * 方法原型:ResultSet getColumns(String catalog,String schemaPattern,String tableNamePattern,String columnNamePattern)
?? ??? ??? ? * catalog - 表所在的類別名稱;""表示獲取沒(méi)有類別的列,null表示獲取所有類別的列。
?? ??? ??? ? * schema - 表所在的模式名稱(oracle中對(duì)應(yīng)于Tablespace);""表示獲取沒(méi)有模式的列,null標(biāo)識(shí)獲取所有模式的列; 可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * tableNamePattern - 表名稱;可包含單字符通配符("_"),或多字符通配符("%");
?? ??? ??? ? * columnNamePattern - 列名稱; ""表示獲取列名為""的列(當(dāng)然獲取不到);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中獲取的是命名空間,其它數(shù)據(jù)庫(kù)未知 ? ??
? ? ? ? ? ? ? ? String tableName_ = rs.getString("TABLE_NAME"); ?//表名 ?
? ? ? ? ? ? ? ? String columnName = rs.getString("COLUMN_NAME"); ?//列名 ?
? ? ? ? ? ? ? ? int dataType = rs.getInt("DATA_TYPE"); ? ? //對(duì)應(yīng)的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"); ?//小數(shù)位數(shù)?
? ? ? ? ? ? ? ? int numPrecRadix = rs.getInt("NUM_PREC_RADIX"); ?//基數(shù)(通常是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"); ?//默認(rèn)值 ?
? ? ? ? ? ? ? ? int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH"); ? ?// 對(duì)于 char 類型,該長(zhǎng)度是列中的最大字節(jié)數(shù)?
? ? ? ? ? ? ? ? int ordinalPosition = rs.getInt("ORDINAL_POSITION"); ? //表中列的索引(從1開(kāi)始) ?
? ? ? ? ? ? ? ? /**?
? ? ? ? ? ? ? ? ?* ISO規(guī)則用來(lái)確定某一列的是否可為空(等同于NULLABLE的值:[ 0:'YES'; 1:'NO'; 2:''; ])
? ? ? ? ? ? ? ? ?* YES -- 該列可以有空值;?
? ? ? ? ? ? ? ? ?* NO -- 該列不能為空;
? ? ? ? ? ? ? ? ?* 空字符串--- 不知道該列是否可為空
? ? ? ? ? ? ? ? ?*/ ?
? ? ? ? ? ? ? ? String isNullAble = rs.getString("IS_NULLABLE"); ?
? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? /**?
? ? ? ? ? ? ? ? ?* 指示此列是否是自動(dòng)遞增?
? ? ? ? ? ? ? ? ?* YES -- 該列是自動(dòng)遞增的
? ? ? ? ? ? ? ? ?* NO -- 該列不是自動(dòng)遞增
? ? ? ? ? ? ? ? ?* 空字串--- 不能確定該列是否自動(dòng)遞增
? ? ? ? ? ? ? ? ?*/ ?
? ? ? ? ? ? ? ? //String isAutoincrement = rs.getString("IS_AUTOINCREMENT"); ? //該參數(shù)測(cè)試報(bào)錯(cuò) ? ?
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? 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(); ?//獲取數(shù)據(jù)庫(kù)信息
?? ??? ?getSchemasInfo(); //獲取數(shù)據(jù)庫(kù)所有Schema
?? ??? ?getTablesList(); ?//獲取某用戶下所有的表
?? ??? ?getTablesInfo(); ?//獲取表信息
?? ??? ?getPrimaryKeysInfo(); //獲取表主鍵信息
?? ??? ?getIndexInfo(); ?//獲取表索引信息
?? ??? ?getColumnsInfo(); //獲取表中列值信息
?? ?}
}
Note: (1)JDBC元數(shù)據(jù)的操作是很消耗性能的,所以應(yīng)盡量避免使用。
(2)在獲取元數(shù)據(jù)中的REMARK(即 備注)前,需要設(shè)置RemarksReporting屬性為true,否則獲取到的REMARK屬性都是null,設(shè)置該屬性有兩種方法,請(qǐng)參閱上面的Demo。
————————————————
版權(quán)聲明:本文為CSDN博主「夜之子」的原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/chen_zw/article/details/18816599
總結(jié)
以上是生活随笔為你收集整理的JDBC元数据操作(一)-- DatabaseMetaData接口详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 初见端倪是什么意思 初见端倪的意思
- 下一篇: Dom4j完整教程详解