ResultSet tabs = dbMetaData.getTables(null, null, null,types/*只要表就好了*/); /*记录集的结构如下: TABLE_CAT String => table catalog (may be null) TABLE_SCHEM String => table schema (may be null) TABLE_NAME String => table name TABLE_TYPE String => table type. REMARKS String => explanatory comment on the table TYPE_CAT String => the types catalog (may be null) TYPE_SCHEM String => the types schema (may be null) TYPE_NAME String => type name (may be null) SELF_REFERENCING_COL_NAME String => name of the designated "identifier" column of a typed table (may be null) REF_GENERATION String => specifies how values in SELF_REFERENCING_COL_NAME are created. Values are "SYSTEM", "USER", "DERIVED". (may be null) */ while(tabs.next()){ //只要表名这一列 tables.add(tabs.getObject("TABLE_NAME"));
} System.out.println(tables); return tables;
}
如果是 sqlserver2000 用一条sql语句就行了 select name from sysobjects where xtype in('S','U') //取得所有表包括用户表和系统表然后在 java中通过jdbc即可读出
要看你用的是什么数据库了 在db2中使用select tabname from syscat.keycoluse可以看到一个库中的所有的表名
fog628(无名) 的方法更通用一些
For Mysql: // Create a Statement object and use it to select each table Statement stmt = connMysql.createStatement(); ResultSet tablesResults = stmt.executeQuery("show tables"); //for sql server:select name from sysobjects where xtype in('S','U') //for db2 :select tabname from syscat.keycoluse while (tablesResults.next()) { String tableName = tablesResults.getString(1); System.out.println("\n"+tableName+":"); //executeQuery(tableName); //importResults(tableName); }
List tables = new ArrayList();
DatabaseMetaData dbMetaData = cnn.getMetaData();
//可为:"TABLE", "VIEW", "SYSTEM TABLE",
//"GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM"
String[] types = {"TABLE"};
ResultSet tabs = dbMetaData.getTables(null, null, null,types/*只要表就好了*/);
/*记录集的结构如下:
TABLE_CAT String => table catalog (may be null)
TABLE_SCHEM String => table schema (may be null)
TABLE_NAME String => table name
TABLE_TYPE String => table type.
REMARKS String => explanatory comment on the table
TYPE_CAT String => the types catalog (may be null)
TYPE_SCHEM String => the types schema (may be null)
TYPE_NAME String => type name (may be null)
SELF_REFERENCING_COL_NAME String => name of the designated "identifier" column of a typed table (may be null)
REF_GENERATION String => specifies how values in SELF_REFERENCING_COL_NAME are created. Values are "SYSTEM", "USER", "DERIVED". (may be null)
*/
while(tabs.next()){
//只要表名这一列
tables.add(tabs.getObject("TABLE_NAME"));
}
System.out.println(tables);
return tables;
}
在db2中使用select tabname from syscat.keycoluse可以看到一个库中的所有的表名
// Create a Statement object and use it to select each table
Statement stmt = connMysql.createStatement();
ResultSet tablesResults = stmt.executeQuery("show tables");
//for sql server:select name from sysobjects where xtype in('S','U')
//for db2 :select tabname from syscat.keycoluse
while (tablesResults.next())
{
String tableName = tablesResults.getString(1);
System.out.println("\n"+tableName+":");
//executeQuery(tableName);
//importResults(tableName);
}