//set up a connection
Connection connection = ...//set up a DatabaseMetaData
DatabaseMetaData dbData = connection.getMetaData();//get ALL tables from this database
TreeSet tableNames = new TreeSet();
ResultSet tableSet = null;String[] types = {"TABLE", "VIEW", "ALIAS", "SYNONYM"};
String userName = dbData.supportsSchemasInTableDefinitions() ? dbData.getUserName() : null;
tableSet = dbData.getTables(null, userName, null, types);
//return table name setwhile (tableSet.next()) {
   try {
        String tableName = tableSet.getString("TABLE_NAME");        tableName = (tableName == null) ? null : tableName.toUpperCase();
        String tableType = tableSet.getString("TABLE_TYPE");        tableType = (tableType == null) ? null : tableType.toUpperCase();
        //only allow certain table types
        if (tableType != null && !"TABLE".equals(tableType) && !"VIEW".equals(tableType) && !"ALIAS".equals(tableType) && !"SYNONYM".equals(tableType))
            continue;
        
        tableNames.add(tableName);
    } catch (SQLException sqle) {
      //...
    }
}

解决方案 »

  1.   

    the code support many jdbc driver: oracle , mysql , hsql , mssql ...
      

  2.   

    DatabaseMetaData md = connection.getMetaData();
    ResultSet rs = md.getTable(null, null, null, new String [] {"TABLE"});
    这个结果集包含了数据库关于所有库表的信息。表名是第三个域,

    while (rs.next())
    {
        tableName[i] = rs.getString(3);
        i++;
    }
    可以得到所有的表名,这是《核心技术卷II》里关于获取表名的方法,应该是跨数据库的。
      

  3.   

    public Enumeration getTableName(){
        Vector vector = new Vector();
        try{
          m_sta = m_con.createStatement();
          m_rs = m_sta.executeQuery("select * from tab where tabtype ='TABLE'");
          while(m_rs.next()){
            vector.addElement("(表)"+m_rs.getString(1));
          }
            tname = vector.elements();
        }
        catch(Exception e){
          e.printStackTrace();
        }
        return tname;
      }
      

  4.   

    首先要感谢各位,但我的问题是“如何从特定的ResultSet”对象中取得表名,而不是取得所有表名!如果要取得所有表,那我完全可以利用一行sql语句从系统表中取得,没有必要这么麻烦。我的意思是://某个类方法,返回一个ResultSet对象public ResultSet getRs()
    {
          try{
              rs = stmt.executeQuery("SELECT * FROM sm_employee");
          }catch(SQLException se){
       System.out.println("Execute Query failed!");
             se.printStackTrace();}
          return rs;
    }当程序调用此方法时,将获得一个ResultSet对象,如何对这个特定的ResultSet对象操作,从而获得表名 sm_employee?当然,此sql语句很简单,若是多表查询时,通过引入列的索引值也应该能够获得特定的表名。
      

  5.   

    ResultSetMetaData rsmd = rs.getMetaData();
    String tableName = rsmd.getTableName(int column);
    这样就可以了!
     
      

  6.   

    column是你结果集中的列的序号从“1”开始。
      

  7.   

    qu_shihong(孤峰):
    我想请问,你做过试验吗?如果做过,使用的是哪一种驱动,可否发一份给我?
      

  8.   

    Zazu:
      qu_shihong's solution is right , if your jdbc driver support jdbc api 2.0 , it works fine.
      

  9.   

    wys1978(Quake Wang) :support jdbc api 2.0 就 works fine 了?你做过考证吗?说实话,我手头有五、六种Oracle的驱动,多数都支持jdbc2.0 Release ,仅仅有一种实现了 getTableName(int column)方法,但它是嵌入到一种Database tool 中的,比较难于使用!所以我仍然在寻求其他的解决途径。不吝赐教!
      

  10.   

    Zazu:
      First , I'm sorry to give a error message without any experiments.
      Second , a bad news: It said that in JDBC Tutorial this method is not widely supported by DBMSs and many DB returns "". And it doesn't work with Oracle thin driver ( I tested ).
      I can not help anymore , I think you need to find another solution.
      

  11.   

    wys1978(Quake Wang) :That's all right.Thank you anyway!