rcyl2003 发表于 2013-1-29 23:35:29

ResultSetMetaData和DatabaseMetaData用法简介(转)

内容概要:
1、ResultSetMetaData中的方法的介绍。
2、DatabaseMetaData中主要方法的介绍。
3、如何使用DatabaseMetaData类获得的信息进行反向设计表。
<span style="font-family: SimSun;" />?
??? 在论坛中看到有好多人问关于,数据库中信息和表的信息的获得的问题,如:数据库中有多少表,表中的字段的含义等,为了解决这个问题我学习了相关的两个类ResultSetMetaData和DataBaseMetaData来解决相关问题。下面是我的一些学习的心得体会,希望能够给朋友们带来一点启示或者帮助。首先是ResultSetMetaData:<span style="" />
这个类完成了查询结果信息和结果中的列的各种信息。它包含的方法以及各个方法的作用,在下面这个演示程序中一一介绍。在这个演示程序中用到数据库是Access的,数据库中的表的名字是STUDENTINFO表,包含字段有<span style="" />
IDNO 文本型 长为8
NAME 文本型 长为8
SEX 文本型 长为6
AGE 数值型 长为8
BIRTHDT 文本型 长为8
程序中主要是操作这个表,选择的驱动是ODBC。程序如下:

<div style="padding: 4px 5.4pt; width: 95%;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpackage javax.util.guxing;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifimport java.sql.*;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gifpublic 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.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif  public DBAccess() ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif    try ...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://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.gifhttp://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.gifhttp://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.gifhttp://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}
页: [1]
查看完整版本: ResultSetMetaData和DatabaseMetaData用法简介(转)