Java——mysql——获取所有table名和table字段名

获取database所有table名:

(参考:http://stackoverflow.com/questions/2780284/how-to-get-all-table-names-from-a-database )

DatabaseMetaData md = conn.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
    System.out.println(rs.getString(3));
}

 原理:Column 3 is the TABLE_NAME (see documentation of getTables).


 

获取某table的所有字段名:  

 (参考:http://java.dzone.com/snippets/listing-schematablecolumn )

DatabaseMetaData meta = conn.getMetaData();
ResultSet resultSet = meta.getColumns(databaseName, null, tableName, "%");
while (resultSet.next()) {
    System.out.println(esultSet.getString(4));
}

你可能感兴趣的:(Java)