Java -- mysql -- get all table names and table field names ., Mysqltable
Obtain the names of all tables in the database:
(Reference: 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));}
Principle: Column3IsTABLE_NAME
(See documentation of getTables ).
Obtain the names of all fields in a table:
(Reference: 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));}
Mysql obtains table fields and sorts them by field names.
SELECT COLUMN_NAME
FROM 'information _ scheme'. 'columns'
Where 'table _ scheme' = 'your SCHEMA name' and 'table _ name' = 'your TABLE name'
Order by COLUMN_NAME;
In the mysql database, how does one write an SQL statement in a java program to obtain all the table names in the specified database and the field names in the specified table in the specified database?
Specify the database in the url, such as String url = "jdbc: mysql: // localhost: 3306/test? UseUnicode = true & characterEncoding = gbk ";
Test indicates the database name.
In addition, you can add a database, such as DATA1.USER1, when writing a table in an SQL statement to prevent this problem.