獲取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)); }
SELECT COLUMN_NAME
FROM `information_schema`.`COLUMNS`
where `TABLE_SCHEMA`='你的架構名' and `TABLE_NAME`='你的表名'
order by COLUMN_NAME;
在url裡指定數據庫,比如String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=gbk";
其中test就是數據庫名
另外為了以防萬一,可以在SQL語句中寫表時加上數據庫,比如 DATA1.USER1