当初在JDBC还没研究透的时候,就偷懒去用了hibernate,现在回过头来做jdbc的开发,发现自己真的了解的太少了,惭愧惭愧。
public static List getAllTableName(Connection cnn) throws SQLException{
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;
}