Java -- mysql -- get all table names and table field names ., Mysqltable

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.