JDBC元資料操作-- DatabaseMetaData介面詳解
阿新 • • 發佈:2018-12-17
- 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(); //獲取表中列值資訊
- }
- }