JDBCメタデータ操作--DatabaseMetaインターフェース詳細
18717 ワード
著作権声明:本文は博主のオリジナル文章で、転載は元の出所を明記してください。ありがとうございます。https://blog.csdn.net/czw2010/article/details/18816599
1.はじめに
JDBC技術仕様では、Connection、Sttement、ResultSetの3つの開発過程でよく使われるインターフェースを提供しています。JDBC仕様は各インターフェースに対応するインターフェース記述オブジェクト、つまりxMetaDataシリーズ記述オブジェクトを提供しています。DatabaseMetaDataとResult SetMetaDataは2つのよく使われているデータベースのメタデータに関する情報を取得するインターフェースであり、ここではData baseMeta Dataインターフェースのみがメタデータを取得する方法を説明する。
この記事のアドレス:http://blog.csdn.net/chen_zw/articale/detail/18816599
2. DatabaseMetaDataインターフェースでよく使われる方法:
(1) ResultSet getTables(String catalog,String schemaPattern,String table NamePattern,String[]types); //テーブル情報の取得
(2) ResultSet get Primary Keys(String catalog、String schema、String table); //テーブルキーの情報を取得する
(3) ResultSet get IndexInfo(String catalog,String schema,String table,bootlean unique,bootlean apple); //テーブルインデックス情報の取得
(4) ResultSet get Columns(String catalog,String schemaPattern,String table NamePattern,String columnNamePattern);テーブル列情報の取得
3.デモの実装:
ノート: (1)JDBCメタデータの操作は、性能が消耗しますので、使用は極力避けてください。
(2)メタデータのREMARKを取得する前に、Remarks Reporting属性をtrueとする必要があります。そうでないと、取得したREMARK属性はすべてnullです。この属性を設定する方法は二つあります。上記のDemoを参照してください。
1.はじめに
JDBC技術仕様では、Connection、Sttement、ResultSetの3つの開発過程でよく使われるインターフェースを提供しています。JDBC仕様は各インターフェースに対応するインターフェース記述オブジェクト、つまりxMetaDataシリーズ記述オブジェクトを提供しています。DatabaseMetaDataとResult SetMetaDataは2つのよく使われているデータベースのメタデータに関する情報を取得するインターフェースであり、ここではData baseMeta Dataインターフェースのみがメタデータを取得する方法を説明する。
この記事のアドレス:http://blog.csdn.net/chen_zw/articale/detail/18816599
2. DatabaseMetaDataインターフェースでよく使われる方法:
(1) ResultSet getTables(String catalog,String schemaPattern,String table NamePattern,String[]types); //テーブル情報の取得
(2) ResultSet get Primary Keys(String catalog、String schema、String table); //テーブルキーの情報を取得する
(3) ResultSet get IndexInfo(String catalog,String schema,String table,bootlean unique,bootlean apple); //テーブルインデックス情報の取得
(4) ResultSet get Columns(String catalog,String schemaPattern,String table NamePattern,String columnNamePattern);テーブル列情報の取得
3.デモの実装:
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(); //
}
}
ノート: (1)JDBCメタデータの操作は、性能が消耗しますので、使用は極力避けてください。
(2)メタデータのREMARKを取得する前に、Remarks Reporting属性をtrueとする必要があります。そうでないと、取得したREMARK属性はすべてnullです。この属性を設定する方法は二つあります。上記のDemoを参照してください。