Get table structure in Java

Source: Internet
Author: User

JDBC. Assume there is a con

Databasemetadata dbmd = con. getmetadata ();

Rs = dbmd. getcolumns (con. getcatalog (), schema, tablename, null );

Rs. getstring (data_type) Java. SQL. types SQL type

The size of the RS. getstring (column_size) column. For the char or date type, the column size is the maximum number of characters. For the numeric and decimal types, the column size is the precision.

The number of digits in the decimal part of Rs. getstring (decimal_digits ).

In JDBC, metadata is used to obtain information about a specific table. You can query tables in the database, fields in the table, and attributes of fields. In metadata, this information is stored in the resultset through a series of getxxx functions, and then returned to the user. There are also a lot of instructions on metadata on the Internet. Here I just want to record how to use JDBC and obtain information about data tables from the perspective of my own learning.

First of all, there is a http://hometown.aol.com/kgb1001001/Articles/JDBCMetadata/JDBC_Metadata.htm named understanding JDBC metadataArticle.

The following figure shows how to obtain table information under my JDBC.Code. I use MySQL 5.0 as the testing platform.

1. The code for connecting JDBC to MySQL is very standard and simple.

Class. forname ("com. MySQL. JDBC. Driver"). newinstance ();

Connection conn = drivermanager

. Getconnection ("JDBC: mysql: // localhost/test? User = root & Password = 123456 ");

2. The following describes how to obtain table information.

M_dbmetadata = m_connection.getmetadata ();

Resultset tableret = m_dbmetadata.gettables (null, "%", m_tablename, new string [] {"table "});

"%" Indicates the meaning of *, that is, all meanings. M_tablename is the name of the data table to be obtained. To obtain the names of all tables, you can use "%" as the parameter.

3. Extract the table name.

While (tableret. Next) system. Out. println (tableret. getstring ("table_name "));

You can use getstring ("table_name") to obtain the table name.

It can be seen from this that JDBC stores all the results returned through the gettables interface in a memory structure similar to a table, the table_name field is the name of each table.

4. Extract the names and types of fields in the table.

String columnname;

String columntype;

Resultset colret = m_dbmetadata.getcolumns (null, "%", m_tablename, "% ");

While (colret. Next ()){

Columnname = colret. getstring ("column_name ");

Columntype = colret. getstring ("type_name ");

Int datasize = colret. getint ("column_size ");

Int digits = colret. getint ("decimal_digits ");

Int nullable = colret. getint ("nullable ");

System. Out. println (columnname + "" + columntype + "" + datasize + "" + digits + "" +

Nullable );

}

In JDBC, The getcolumns interface is used to query fields. Like gettables, "%" indicates all arbitrary fields, while m_tablename indicates the name of the data table.

Getcolumns returns all fields in a similar memory table, while column_name is the name of the field, and type_name is the data type, such as "int", "int unsigned", etc, column_size returns an integer, that is, the length of a field. For example, if the INT (8) field is defined, 8 is returned. If nullable is returned, 1 indicates null, 0 indicates not null.

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.