package com.database.manager;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* @author daoger
*
* 2009-9-24
*/
public class DatabaseMetaDateApplication
{
private DatabaseMetaData dbMetaData = null;
private Connection con = null;
private void getDatabaseMetaData()
{
try
{
if (dbMetaData == null)
{
Class.forName("oracle.jdbc.driver.OracleDriver");
String url = "jdbc:oracle:thin:@192.168.0.2:1521:×××";
String user = "×××";
String password = "×××";
con = DriverManager.getConnection(url, user, password);
dbMetaData = con.getMetaData();
}
} catch (ClassNotFoundException e)
{
// TODO: handle ClassNotFoundException
e.printStackTrace();
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
public void colseCon()
{
try
{
if (con != null)
{
con.close();
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得数据库的一些相关信息
*/
public void getDataBaseInformations()
{
try
{
System.out.println("URL:" + dbMetaData.getURL() + ";");
System.out.println("UserName:" + dbMetaData.getUserName() + ";");
System.out.println("isReadOnly:" + dbMetaData.isReadOnly() + ";");
System.out.println("DatabaseProductName:" + dbMetaData.getDatabaseProductName() + ";");
System.out.println("DatabaseProductVersion:" + dbMetaData.getDatabaseProductVersion() + ";");
System.out.println("DriverName:" + dbMetaData.getDriverName() + ";");
System.out.println("DriverVersion:" + dbMetaData.getDriverVersion());
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得该用户下面的所有表
*/
public void getAllTableList(String schemaName)
{
try
{
// table type. Typical types are "TABLE", "VIEW", "SYSTEM
// TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",
// "SYNONYM".
String[] types =
{ "TABLE" };
ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);
while (rs.next())
{
String tableName = rs.getString("TABLE_NAME");
// table type. Typical types are "TABLE", "VIEW", "SYSTEM
// TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",
// "SYNONYM".
String tableType = rs.getString("TABLE_TYPE");
// explanatory comment on the table
String remarks = rs.getString("REMARKS");
System.out.println(tableName + "-" + tableType + "-" + remarks);
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得该用户下面的所有视图
*/
public void getAllViewList(String schemaName)
{
try
{
String[] types =
{ "VIEW" };
ResultSet rs = dbMetaData.getTables(null, schemaName, "%", types);
while (rs.next())
{
String viewName = rs.getString("TABLE_NAME");
// table type. Typical types are "TABLE", "VIEW", "SYSTEM
// TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS",
// "SYNONYM".
String viewType = rs.getString("TABLE_TYPE");
// explanatory comment on the table
String remarks = rs.getString("REMARKS");
System.out.println(viewName + "-" + viewType + "-" + remarks);
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得数据库中所有方案名称
*/
public void getAllSchemas()
{
try
{
ResultSet rs = dbMetaData.getSchemas();
while (rs.next())
{
String tableSchem = rs.getString("TABLE_SCHEM");
System.out.println(tableSchem);
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得表或视图中的所有列信息
*/
public void getTableColumns(String schemaName, String tableName)
{
try
{
ResultSet rs = dbMetaData.getColumns(null, schemaName, tableName, "%");
while (rs.next())
{
// table catalog (may be null)
String tableCat = rs.getString("TABLE_CAT");
// table schema (may be null)
String tableSchemaName = rs.getString("TABLE_SCHEM");
// table name
String tableName_ = rs.getString("TABLE_NAME");
// column name
String columnName = rs.getString("COLUMN_NAME");
// SQL type from java.sql.Types
int dataType = rs.getInt("DATA_TYPE");
// Data source dependent type name, for a UDT the type name is
// fully qualified
String dataTypeName = rs.getString("TYPE_NAME");
// table schema (may be null)
int columnSize = rs.getInt("COLUMN_SIZE");
// the number of fractional digits. Null is returned for data
// types where DECIMAL_DIGITS is not applicable.
int decimalDigits = rs.getInt("DECIMAL_DIGITS");
// Radix (typically either 10 or 2)
int numPrecRadix = rs.getInt("NUM_PREC_RADIX");
// is NULL allowed.
int nullAble = rs.getInt("NULLABLE");
// comment describing column (may be null)
String remarks = rs.getString("REMARKS");
// default value for the column, which should be interpreted as
// a string when the value is enclosed in single quotes (may be
// null)
String columnDef = rs.getString("COLUMN_DEF");
//
int sqlDataType = rs.getInt("SQL_DATA_TYPE");
//
int sqlDatetimeSub = rs.getInt("SQL_DATETIME_SUB");
// for char types the maximum number of bytes in the column
int charOctetLength = rs.getInt("CHAR_OCTET_LENGTH");
// index of column in table (starting at 1)
int ordinalPosition = rs.getInt("ORDINAL_POSITION");
// ISO rules are used to determine the nullability for a column.
// YES --- if the parameter can include NULLs;
// NO --- if the parameter cannot include NULLs
// empty string --- if the nullability for the parameter is
// unknown
String isNullAble = rs.getString("IS_NULLABLE");
// Indicates whether this column is auto incremented
// YES --- if the column is auto incremented
// NO --- if the column is not auto incremented
// empty string --- if it cannot be determined whether the
// column is auto incremented parameter is unknown
String isAutoincrement = rs.getString("IS_AUTOINCREMENT");
System.out.println(tableCat + "-" + tableSchemaName + "-" + tableName_ + "-" + columnName + "-"
+ dataType + "-" + dataTypeName + "-" + columnSize + "-" + decimalDigits + "-" + numPrecRadix
+ "-" + nullAble + "-" + remarks + "-" + columnDef + "-" + sqlDataType + "-" + sqlDatetimeSub
+ charOctetLength + "-" + ordinalPosition + "-" + isNullAble + "-" + isAutoincrement + "-");
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得一个表的索引信息
*/
public void getIndexInfo(String schemaName, String tableName)
{
try
{
ResultSet rs = dbMetaData.getIndexInfo(null, schemaName, tableName, true, true);
while (rs.next())
{
// Can index values be non-unique. false when TYPE is
// tableIndexStatistic
boolean nonUnique = rs.getBoolean("NON_UNIQUE");
// index catalog (may be null); null when TYPE is
// tableIndexStatistic
String indexQualifier = rs.getString("INDEX_QUALIFIER");
// index name; null when TYPE is tableIndexStatistic
String indexName = rs.getString("INDEX_NAME");
// index type:
// tableIndexStatistic - this identifies table statistics that
// are returned in conjuction with a table's index descriptions
// tableIndexClustered - this is a clustered index
// tableIndexHashed - this is a hashed index
// tableIndexOther - this is some other style of index
short type = rs.getShort("TYPE");
// column sequence number within index; zero when TYPE is
// tableIndexStatistic
short ordinalPosition = rs.getShort("ORDINAL_POSITION");
// column name; null when TYPE is tableIndexStatistic
String columnName = rs.getString("COLUMN_NAME");
// column sort sequence, "A" => ascending, "D" => descending,
// may be null if sort sequence is not supported; null when TYPE
// is tableIndexStatistic
String ascOrDesc = rs.getString("ASC_OR_DESC");
// When TYPE is tableIndexStatistic, then this is the number of
// rows in the table; otherwise, it is the number of unique
// values in the index.
int cardinality = rs.getInt("CARDINALITY");
System.out.println(nonUnique + "-" + indexQualifier + "-" + indexName + "-" + type + "-"
+ ordinalPosition + "-" + columnName + "-" + ascOrDesc + "-" + cardinality);
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得一个表的主键信息
*/
public void getAllPrimaryKeys(String schemaName, String tableName)
{
try
{
ResultSet rs = dbMetaData.getPrimaryKeys(null, schemaName, tableName);
while (rs.next())
{
// column name
String columnName = rs.getString("COLUMN_NAME");
// sequence number within primary key( a value of 1 represents
// the first column of the primary key, a value of 2 would
// represent the second column within the primary key).
short keySeq = rs.getShort("KEY_SEQ");
// primary key name (may be null)
String pkName = rs.getString("PK_NAME");
System.out.println(columnName + "-" + keySeq + "-" + pkName);
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
/**
* 获得一个表的外键信息
*/
public void getAllExportedKeys(String schemaName, String tableName)
{
try
{
ResultSet rs = dbMetaData.getExportedKeys(null, schemaName, tableName);
while (rs.next())
{
// primary key table catalog (may be null)
String pkTableCat = rs.getString("PKTABLE_CAT");
// primary key table schema (may be null)
String pkTableSchem = rs.getString("PKTABLE_SCHEM");
// primary key table name
String pkTableName = rs.getString("PKTABLE_NAME");
// primary key column name
String pkColumnName = rs.getString("PKCOLUMN_NAME");
// foreign key table catalog (may be null) being exported (may
// be null)
String fkTableCat = rs.getString("FKTABLE_CAT");
// foreign key table schema (may be null) being exported (may be
// null)
String fkTableSchem = rs.getString("FKTABLE_SCHEM");
// foreign key table name being exported
String fkTableName = rs.getString("FKTABLE_NAME");
// foreign key column name being exported
String fkColumnName = rs.getString("FKCOLUMN_NAME");
// sequence number within foreign key( a value of 1 represents
// the first column of the foreign key, a value of 2 would
// represent the second column within the foreign key).
short keySeq = rs.getShort("KEY_SEQ");
// What happens to foreign key when primary is updated:
// importedNoAction - do not allow update of primary key if it
// has been imported
// importedKeyCascade - change imported key to agree with
// primary key update
// importedKeySetNull - change imported key to NULL if its
// primary key has been updated
// importedKeySetDefault - change imported key to default values
// if its primary key has been updated
// importedKeyRestrict - same as importedKeyNoAction (for ODBC
// 2.x compatibility)
short updateRule = rs.getShort("UPDATE_RULE");
// What happens to the foreign key when primary is deleted.
// importedKeyNoAction - do not allow delete of primary key if
// it has been imported
// importedKeyCascade - delete rows that import a deleted key
// importedKeySetNull - change imported key to NULL if its
// primary key has been deleted
// importedKeyRestrict - same as importedKeyNoAction (for ODBC
// 2.x compatibility)
// importedKeySetDefault - change imported key to default if its
// primary key has been deleted
short delRule = rs.getShort("DELETE_RULE");
// foreign key name (may be null)
String fkName = rs.getString("FK_NAME");
// primary key name (may be null)
String pkName = rs.getString("PK_NAME");
// can the evaluation of foreign key constraints be deferred
// until commit
// importedKeyInitiallyDeferred - see SQL92 for definition
// importedKeyInitiallyImmediate - see SQL92 for definition
// importedKeyNotDeferrable - see SQL92 for definition
short deferRability = rs.getShort("DEFERRABILITY");
System.out.println(pkTableCat + "-" + pkTableSchem + "-" + pkTableName + "-" + pkColumnName + "-"
+ fkTableCat + "-" + fkTableSchem + "-" + fkTableName + "-" + fkColumnName + "-" + keySeq + "-"
+ updateRule + "-" + delRule + "-" + fkName + "-" + pkName + "-" + deferRability);
}
} catch (SQLException e)
{
// TODO: handle SQLException
e.printStackTrace();
}
}
public DatabaseMetaDateApplication()
{
this.getDatabaseMetaData();
}
}
相关推荐
`DatabaseMetaData` 是Java数据库连接(JDBC)API的一部分,它提供了关于数据库模式、特性以及元数据的详细信息。本篇文章将深入探讨如何利用`DatabaseMetaData`生成数据库的DLL(在关系型数据库中,DLL通常指的是...
在Java编程中,当我们需要与Oracle数据库交互时,`java.sql.DatabaseMetaData`接口提供了一种方式来获取关于数据库的各种元数据信息。这篇文章将详细介绍如何利用`DatabaseMetaData`查询Oracle数据库的所有表。 ...
在数据库操作中,JDBC(Java Database Connectivity)提供了`DatabaseMetaData`接口,允许查询数据库的元信息,如表结构、列信息、索引、视图等。通过`Connection`对象的`getMetaData()`方法可以获得这些信息。 6....
2. **DatabaseMetaData接口** 3. **getTables方法** 4. **ResultSet处理** ### 实现原理与步骤 #### 1. JDBC简介 JDBC是Java中用于连接和操作关系型数据库的标准API。它提供了一套标准的方法来执行SQL语句,获取...
import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; public class DatabaseMetadataExample { public ...
Java实现数据库元数据编程经典代码Metadata Java Database Programming classic code
JDBC:java版本的ODBC JDBC连接数据库的步骤: 1.注册驱动(加载驱动): 注册的方式: 1.使用类加载器(使用反射的方式) Class.forName(driverName); 2.实例化Driver Driver driver = new oracle.jdbc...
项目分为核心查询模块“database-metadata-core”和数据展示模块“database-metadata-view”,其中核心模块采用纯JDBC方式,通过java.sql.DatabaseMetaData对象获取数据库元数据信息,不依赖任何外部jar包,适用于...
JDBC提供了java.sql.Connection、java.sql.DriverManager、java.sql.DatabaseMetaData等类,用于建立数据库连接和获取元数据。 1. **建立数据库连接**: 使用`DriverManager.getConnection()`方法,提供数据库URL...
DatabaseMetaData dbmd=con.getMetaData(); //获取DatabaseMetaData实例 System.out.println(dbmd.getDatabaseProductName()); //获取数据库名称 System.out.println(dbmd.getDatabaseProductVersion()); //...
首先,你需要导入JDBC相关的类,如`java.sql.Connection`,`java.sql.DriverManager`,`java.sql.DatabaseMetaData`等。例如: ```java import java.sql.Connection; import java.sql.DriverManager; import ...
首先,我们需要获取`DatabaseMetadata`,然后创建`OracleDataPump`对象。 ```java DatabaseMetaData metaData = conn.getMetaData(); OracleDataPump dp = new OracleDataPump(metaData); ``` 3. **定义导出参数**...
例如,使用`DatabaseMetaData`接口的`getTables()`方法,可以列出所有表的信息。记得处理可能的异常,如`SQLException`。 4. **遍历表数据**: 对每个表,程序需要执行`SELECT * FROM 表名`这样的查询来获取所有...
这可以通过`DatabaseMetaData`对象完成,可以查询表、列、索引等信息。例如,获取所有表的名称: ```java DatabaseMetaData metaData = connection.getMetaData(); ResultSet tables = metaData.getTables(null, ...
2. **查询元数据**:连接成功后,通过`DatabaseMetaData`接口获取数据库的表信息,包括表名、列名、数据类型等。这些信息是生成Java实体类的基础。 3. **解析字段信息**:根据查询到的元数据,解析每个字段,将其...
`DatabaseMetaData`接口提供了许多方法,用于获取关于数据库的结构、支持的数据类型、SQL特性等信息。例如,你可以用以下代码获取所有支持的SQL关键字: ```java DatabaseMetaData dbMeta = conn.getMetaData(); ...
此外,还可以通过`DatabaseMetaData.getColumns()`方法获取表的列信息,`getPrimaryKeys()`获取主键信息,`getExportedKeys()`获取外键信息等。 在实际项目中,你可能需要对这些结果进行处理,比如创建一个`...
8. **数据库元数据**:DatabaseMetaData接口提供了一系列方法,用于获取数据库的元信息,如数据库版本、表格信息、列信息等。 9. **JDBC连接池**:为了提高性能和资源利用率,通常使用连接池来管理数据库连接。例如...
连接成功后,可以使用`DatabaseMetaData`接口获取数据库的元数据,如版本信息、支持的SQL特性等。例如: ```java DatabaseMetaData dbmd = conn.getMetaData(); System.out.println("数据库名称:" + dbmd....
总结来说,使用JDBC查看数据库的详细信息主要涉及加载驱动、建立连接、获取`DatabaseMetaData`对象并调用其方法,以及正确处理结果集和关闭资源。这对于任何需要与数据库交互的Java应用都是至关重要的。通过阅读博文...