---
title: "ResultSetMetaData和DatabaseMetaData用法简介(转)"
author: "rcyl2003"
date: "2008-05-01T00:36:51Z"
tags: ["Access", "JDBC", "Oracle", "SQL", "设计模式"]
slug: "resultsetmetadata-databasemetadata"
draft: false
lastmod: "2008-05-01T00:36:51Z"
id: 205325
url: "https://rcyl2003.iteye.com/blog/205325"
---

内容概要：

1、ResultSetMetaData中的方法的介绍。

2、DatabaseMetaData中主要方法的介绍。

3、如何使用DatabaseMetaData类获得的信息进行反向设计表。

?

??? 在论坛中看到有好多人问关于，数据库中信息和表的信息的获得的问题，如：数据库中有多少表，表中的字段的含义等，为了解决这个问题我学习了相关的两个类ResultSetMetaData和DataBaseMetaData来解决相关问题。下面是我的一些学习的心得体会，希望能够给朋友们带来一点启示或者帮助。首先是ResultSetMetaData：

这个类完成了查询结果信息和结果中的列的各种信息。它包含的方法以及各个方法的作用，在下面这个演示程序中一一介绍。在这个演示程序中用到数据库是Access的，数据库中的表的名字是STUDENTINFO表，包含字段有

IDNO 文本型 长为8

NAME 文本型 长为8

SEX 文本型 长为6

AGE 数值型 长为8

BIRTHDT 文本型 长为8

程序中主要是操作这个表，选择的驱动是ODBC。程序如下：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)package javax.util.guxing;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import java.sql.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)public class DBAccess ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  public static java.sql.Connection conn = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  private String sqlStr = "";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  public DBAccess() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      conn = DriverManager.getConnection("jdbc:odbc:TestDB", "admin", "");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    }catch (ClassNotFoundException ex) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println(ex.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    }catch (SQLException sqlEx) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println(sqlEx.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)     
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  public ResultSet Search() ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    ResultSet rset = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    sqlStr = "SELECT * FROM STUDENTINF ";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    Statement smt = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      smt = conn.createStatement();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      rset = smt.executeQuery(sqlStr);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    catch (SQLException ex) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("Exception:" + ex.toString());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    return rset;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  public void getResultSetMetaData()...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    ResultSet rs = null;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    try ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      String\[] tp = ...{"TABLE"};  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      rs = this.Search();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      ResultSetMetaData rsmd = rs.getMetaData();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)      /\*\*//*  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)       获得ResultSetMeataData对象。所有方法的参数都是列的索引号，即第几列，从1开始  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("下面这些方法是ResultSetMetaData中方法");  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("获得1列所在的Catalog名字 : " + rsmd.getCatalogName(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("获得1列对应数据类型的类 " + rsmd.getColumnClassName(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("获得该ResultSet所有列的数目 " + rsmd.getColumnCount());  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列在数据库中类型的最大字符个数" + rsmd.getColumnDisplaySize(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println(" 1列的默认的列的标题" + rsmd.getColumnLabel(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列的模式 " + rsmd.GetSchemaName(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列的类型,返回SqlType中的编号 " + rsmd.getColumnType(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列在数据库中的类型，返回类型全名" + rsmd.getColumnTypeName(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列类型的精确度(类型的长度): " + rsmd.getPrecision(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列小数点后的位数 " + rsmd.getScale(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列对应的模式的名称（应该用于Oracle） " + rsmd.getSchemaName(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列对应的表名 " + rsmd.getTableName(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列是否自动递增" + rsmd.isAutoIncrement(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列在数据库中是否为货币型" + rsmd.isCurrency(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列是否为空" + rsmd.isNullable(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列是否为只读 " + rsmd.isReadOnly(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      System.out.println("1列能否出现在where中 " + rsmd.isSearchable(1));  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)    catch (SQLException ex) ...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      ex.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)    }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)  public static void main(String args\[])...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    DBAccess dbAccess = new DBAccess();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)    dbAccess.getResultSetMetaData();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)  }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

通过上面例子中的方法能够获得，ResultSet中的表的信息。ResultSetMetaData只是获得表的信息，使用DatabaseMetaData则是用来获得数据库的信息，下面介绍这个类的使用方法。

?

DatabaseMetaData对象提供的是关于数据库的各种信息，这些信息包括：

1、??? 数据库与用户，数据库标识符以及函数与存储过程。

2、??? 数据库限制。

3、??? 数据库支持不支持的功能。

4、??? 架构、编目、表、列和视图等。

通过调用DatabaseMetaData的各种方法，程序可以动态的了解一个数据库。由于这个类中的方法非常的多那么就介绍几个常用的方法来给大家参考。

DatabaseMetaData实例的获取方法是，通过连接来获得的

Connection conn = //创建的连接。

DatabaseMetaData dbmd = Conn.getMetaData();

创建了这个实例，就可以使用他的方法来获取数据库得信息。首先是数据库中用户标识符的信息的获得，主要使用如下的方法：

getDatabaseProductName()用以获得当前数据库是什么数据库。比如oracle，access等。返回的是字符串。

getDatabaseProductVersion()获得数据库的版本。返回的字符串。

getDriverVersion()获得驱动程序的版本。返回字符串。

supportsResultSetType(ResultSet.resultype)是判定是否支持这种结果集的类型。比如参数如果是Result.TYPE\_FORWARD\_ONLY,那就是判定是否支持，只能先前移动结果集的指针。返回值为boolean，true表示支持。

上面介绍的只是几个常用的方法，这个类中还有很多方法，可以到jdk的帮助文档中去查看类java.sql.DatabaseMetaData。

这个类中还有一个比较常用的方法就是获得表的信息。使用的方法是：

getTables（String catalog,String schema,String tableName,String\[] types），

这个方法带有四个参数，他们表示的含义如下：

String catalog——要获得表所在的编目。串“”””意味着没有任何编目，Null表示所有编目。

String schema——要获得表所在的模式。串“”””意味着没有任何模式，Null表示所有模式。该参数可以包含单字符的通配符（“\_”）,也可以包含多字符的通配符（“%”）。

String tableName——指出要返回表名与该参数匹配的那些表，该参数可以包含单字符的通配符（“\_”）,也可以包含多字符的通配符（“%”）。

String types——一个指出返回何种表的数组。可能的数组项是：”TABLE”，”VIEW”，

”SYSTEM TABLE”，”GLOBAL TEMPORARY”，”LOCAL TEMPORARY”，”ALIAS”，

“SYSNONYM”。

通过getTables（）方法返回一个表的信息的结果集。这个结果集包括字段有：TABLE\_CAT表所在的编目。TABLE\_SCHEM表所在的模式，TABLE\_NAME表的名称。TABLE\_TYPE标的类型。REMARKS一段解释性的备注。通过这些字段可以完成表的信息的获取。

还有两个方法一个是获得列getColumns(String catalog,String schama,String tablename,String columnPattern)一个是获得关键字的方法`getPrimaryKeys(String?catalog, String?schema, String?table)这两个方法中的参数的含义和上面的介绍的是相同的。凡是pattern的都是可以用通配符匹配的。getColums()返回的是结果集，这个结果集包括了列的所有信息，类型，名称，可否为空等。getPrimaryKey（）则是返回了某个表的关键字的结果集。`

`通过getTables（），getColumns（），getPrimaryKeys（）就可以完成表的反向设计了。主要步骤如下：`

`1、? 通过getTables()获得数据库中表的信息。`

2、? 对于每个表使用，getColumns(),getPrimaryKeys()获得相应的列名，类型，限制条件，关键字等。

3、? 通过1，2获得信息可以生成相应的建表的SQL语句。