- 浏览: 531583 次
- 性别:
- 来自: 山东济南
文章分类
最新评论
-
dragon_8844:
非常不错,nice
java.util.concurrent 多线程框架 -
wusendong:
很好的文章!受益匪浅,谢谢!
java.util.concurrent 多线程框架 -
SINCE1978:
你也关注并发啊
java.util.concurrent 多线程框架 -
lku1314:
这个不错 刚刚找到这个组建 以前孤陋寡闻了 像lz学习!标 ...
quartz 在WEB中应用小结 -
lliiqiang:
人们对于目标需要的需求明确的去做,对于目标以外的因素是随机的执 ...
flex和后端的数据交互(一)--XML和HTTPService
最近要做一个数据字典的工具,看了一下DatabaseMetaData的使用,做个备忘示例!
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(); } }
相关推荐
### Java判断数据库表是否存在 在Java开发中,经常会遇到需要检查特定表是否存在于数据库中的情况。这通常是数据库操作流程中的一个重要环节,...同时,掌握这些基础知识对于深入理解Java数据库编程也是非常有帮助的。
通过本文的介绍,我们可以看到利用Java JDBC API可以很方便地获取MySQL数据库的表结构、主键、外键信息。这对于开发人员来说非常有用,特别是在进行数据库迁移、生成数据库文档或创建数据库管理工具时。希望本文能对...
在Java编程中,获取数据库的基本信息是常见的任务,这包括连接状态、数据库版本、表信息、用户权限等。本文将详细讲解如何使用Java JDBC(Java Database Connectivity)来获取这些信息。 首先,要与数据库建立连接...
在Java编程中,数据库操作是不可或缺的一部分,而`DatabaseMetaData`接口则是Java SQL API提供的一种机制,用于获取关于数据库的元数据信息。元数据是指关于数据的数据,例如表的结构、列的信息、索引、视图、存储...
在Java编程中,获取数据库对SQL支持的信息是开发数据库应用时常见的需求,这涉及到JDBC(Java Database Connectivity)API的使用。JDBC是Java中用于与各种类型数据库交互的一组接口和类,它允许程序员使用标准的Java...
连接成功后,可以使用`DatabaseMetaData`接口获取数据库的元数据,如版本信息、支持的SQL特性等。例如: ```java DatabaseMetaData dbmd = conn.getMetaData(); System.out.println("数据库名称:" + dbmd....
- DatabaseMetaData获取数据库的元数据,如表信息、列信息等。 总的来说,Java与数据库的连接主要依赖于JDBC API,通过它我们可以方便地进行数据库的读写操作。了解JDBC的工作原理和最佳实践,有助于提高开发效率...
10. **数据库元数据(Metadata)**:通过DatabaseMetaData对象,可以获取数据库的结构信息,如表名、列名等。源码可能展示了如何获取和使用元数据。 这些实例涵盖了从基础的JDBC操作到更复杂的数据库交互技术,对于...
7. **数据库元数据**:通过`DatabaseMetaData`获取数据库的结构和属性信息。 《Java数据库接口JDBC入门基础讲座》可能是针对初学者的教程,着重于介绍JDBC的基础概念和基本操作,包括如何设置数据库连接参数、执行...
JDBC是Java标准API,它提供了一套接口和类,用于连接和操作数据库。在Java代码中,我们需要添加MySQL的JDBC驱动依赖,例如`mysql-connector-java`,这样就可以使用`Connection`对象连接到MySQL服务器。 以下是一段...
在Java中,我们通常会使用JDBC(Java Database Connectivity)API来连接和操作数据库。JDBC提供了一组接口和类,使得开发者能够编写与特定数据库无关的代码,实现数据库的增删查改操作。 1. **数据库连接**:使用...
总结来说,使用JDBC查看数据库的详细信息主要涉及加载驱动、建立连接、获取`DatabaseMetaData`对象并调用其方法,以及正确处理结果集和关闭资源。这对于任何需要与数据库交互的Java应用都是至关重要的。通过阅读博文...
《Java数据库编程实例》这本书是Java开发者学习数据库编程的重要参考资料,其随书代码涵盖了前十章的实例,旨在帮助读者深入理解和实践Java与数据库的交互。本文将详细解析这些知识点,以便于你更好地掌握Java数据库...
### Kettle API - Java调用示例详解 #### 一、引言 Kettle是一款开源的数据集成工具,广泛应用于ETL(Extract-Transform-Load)过程中。它支持多种数据库平台,能够灵活处理复杂的数据转换任务。本文将详细介绍如何...
10.3.1 在JTree中显示DatabaseMetaData 10.4 检索有关RDBMS功能性的信息 10.5 使用ResultSetMetaData 10.6 小结 第3部分 三层Web站点与JDBC 第11章 构建会员Web站点 11.1 设计一个多层系统 11.2 功能性...
10.3.1 在JTree中显示DatabaseMetaData 10.4 检索有关RDBMS功能性的信息 10.5 使用ResultSetMetaData 10.6 小结 第3部分 三层Web站点与JDBC 第11章 构建会员Web站点 11.1 设计一个多层系统 11.2 功能性...
9. **数据库元数据**:`java.sql.DatabaseMetaData`接口提供了获取数据库元信息的方法,如数据库版本、表信息、列信息等。 10. **数据库连接池**:在实际应用中,通常会使用连接池(如C3P0、HikariCP、Druid等)...
7. **DatabaseMetaData对象**:提供关于数据库的元数据信息,例如数据库版本、支持的数据类型等。 8. **Transaction管理**:JDBC支持事务处理,可以使用`Connection`对象的`setAutoCommit()`和`commit()`/`rollback...
在Java编程中,我们可以使用JDBC(Java Database Connectivity)的`DatabaseMetaData`接口来获取数据库的元数据。这个接口提供了多种方法来获取关于数据库的各种信息。例如,在给出的代码示例中,`JDBCDataMeta`类...
- DatabaseMetaData:提供关于数据库的信息,如表、列、视图等。 8. **案例实践** 本教材会提供实际的数据库操作示例,包括用户注册、登录、商品查询等功能,通过这些案例深入理解JDBC API的使用。 通过学习和...