DatabaseMetaData get database information. __ Database

Source: Internet
Author: User
Tags table name

Import java.sql.Connection;

Import Java.sql.DriverManager;

Import java.sql.Statement;

Import Java.sql.DatabaseMetaData;

public class DataConn {

Public DataConn () {

}

public static void Main (string[] args) {

Try

{

Load Driver

The following code loads the JDBD-ODBC driver

Class.forName ("Oracle.jdbc.driver.OracleDriver");

Establish a connection

Connect to the DBMS with the appropriate driver, and look at the following code [modify the database information for your connection]:

String url= "Jdbc:oracle:thin:@192.168.4.45:1521:oemrep";

String user = "UMS";

String password = "Rongji";

Create a connection with a URL

Connection con=drivermanager.getconnection (Url,user,password);

Getting information about the database

DatabaseMetaData dbmetadata = Con.getmetadata ();

Returns a String class object that represents the URL of the database

System.out.println ("URL:" +dbmetadata.geturl () + ";");

Returns the user name that connects to the current database management system.

System.out.println ("UserName:" +dbmetadata.getusername () + ";");

Returns a Boolean value indicating whether the database allows only read operations.

System.out.println ("IsReadOnly:" +dbmetadata.isreadonly () + ";");

Returns the product name of the database.

System.out.println ("Databaseproductname:" +dbmetadata.getdatabaseproductname () + ";");

Returns the version number of the database.

System.out.println ("Databaseproductversion:" +dbmetadata.getdatabaseproductversion () + ";");

Returns the name of the driver driver.

System.out.println ("DriverName:" +dbmetadata.getdrivername () + ";");

Returns the version number of the driver.

System.out.println ("DriverVersion:" +dbmetadata.getdriverversion ());

Close connection

Con.close ();

}

catch (Exception e)

{

Output exception information

System.err.println ("SQLException:" +e.getmessage ());

E.printstacktrace ();

}

}

}

==================================================================

Through the example above, we can see that the implementation of the object of the DatabaseMetaData class, the following statement

<%

DatabaseMetaData Datameta=con.getmetadata ();

%>

The DatabaseMetaData class provides a number of methods for obtaining information about a data source, which enables you to have a very detailed understanding of the information in your database. As we have shown in the previous example, [other methods refer the reader to the DatabaseMetaData class in the JDK API]:

GetURL ()

Returns a String class object that represents the URL of the database.

GetUserName ()

Returns the user name that connects to the current database management system.

IsReadOnly ()

Returns a Boolean value indicating whether the database allows only read operations.

Getdatabaseproductname ()

Returns the product name of the database.

Getdatabaseproductversion ()

Returns the version number of the database.

Getdrivername ()

Returns the name of the driver driver.

Getdriverversion ()

Returns the version number of the driver.

================

first, get database table information Gettables

Gettables (String catalog,  String Schemapattern,   string Tablenamepattern, string[] types)  throws SQLException
Retrieves a description of the table that can be used in a given category. Returns only table descriptions that match the category, schema, table name, and type criteria. They are sorted according to Table_type, Table_schem, and TABLE_NAME.

Each table description has the following columns: table_cat string => table category (nullable) Table_schem String => table mode (can be NULL) table_name string => table name table_type string => table type. Typical types are "table", "VIEW", "SYSTEM table", "GLOBAL temporary", "local temporary", "ALIAS", and "synonym". Remarks Explanatory note for String => table type_cat string => type (nullable) Type_schem string => type pattern ( Nullable) type_name string => type name (nullable) Self_referencing_col_name string => The name of the specified "identifier" column with the Type table (nullable) ref_generation String => Specifies how the value is created in Self_referencing_col_name. These values are "SYSTEM", "USER", and "DERIVED". (may be null)

Note: Some databases may not return information for all tables. parameter: Catalog-category name, because it is stored in the database, it must match the category name. The parameter is "" to retrieve a description without a category, or null to indicate that the category name should not be used to narrow the search scope Schemapattern-pattern name pattern, because it is stored in the database, so it must match the schema name. The parameter is "" To retrieve the description that has no schema, or null to indicate that the schema name should not be used to narrow the search scope tablenamepattern-table name pattern, because it is stored in the database, so it must match the table name types-a list of table types to include, NULL to represent Return all Types return: ResultSet-Each row is a table description thrown: SQLException-If a database access error occurs

ii. Getting column information from the table GetColumns

ResultSet getcolumns(string catalog, String Schemapattern, String Tablenamepattern, String Columnnamepat Tern) throws SQLException

Retrieves a description of the table column that can be used in the specified category.

Returns only the description of the column that matches the category, pattern, table, and column name criteria. They are sorted according to Table_schem, TABLE_NAME, and ordinal_position.

Each column description has the following columns:Table_catString => Table category (nullable)Table_schemString => Table pattern (nullable)table_nameString => Table Namecolumn_nameString => Column Namedata_typeint => SQL type from Java.sql.Typestype_nameString => the type name on which the data source depends, and for the UDT, the type name is fully qualifiedcolumn_sizeint => the size of the column. For char or date types, the size of the column is the maximum number of characters, and for numeric and decimal types, the size of the column is precision.buffer_lengthNot being used.decimal_digitsThe number of digits of the int => decimal partNum_prec_radixint => cardinality (usually 10 or 2)NULLABLEint => whether NULL is allowed. Columnnonulls-Null value columnnullable-may not be allowed-explicitly allow null value columnnullableunknown-do not know whether to use nullremarksString => A comment that describes the column (nullable)Column_defString => Default value (Nullable)Sql_data_typeint => not usedsql_datetime_subint => not usedChar_octet_lengthint => for char type, this length is the maximum number of bytes in the columnordinal_positionThe index of the columns in the Int => table (starting from 1)is_nullableString => "No" indicates that a column is explicitly not allowed to use a null value, and "YES" indicates that a column might be allowed to use a null value. An empty string means that no one knows whether to allow null values.Scope_catlogString => the category of the table, which is the scope of the referencing property (null if Data_type is not REF)Scope_schemaString => the schema of the table, which is the scope of the referencing property (null if Data_type is not REF)scope_tableString => the table name, which is the scope of the referencing property (null if Data_type is not REF)Source_data_typeShort => The source type of the SQL type that is a different type or user generated ref type, from Java.sql.Types (if data_type is not DISTINCT or user-generated ref, NULL)Parameters:Catalog-category name, because it is stored in the database, it must match the category name. The parameter is "" to retrieve a description without a category, or null to indicate that the category name should not be used to narrow the search scope Schemapattern-pattern name pattern, because it is stored in the database, so it must match the schema name. The parameter is "" To retrieve the description that has no schema, or null to indicate that the schema name should not be used to narrow the search scope tablenamepattern-table name pattern, because it is stored in the database, so it must match the table name columnnamepattern-column name pattern, Because it is stored in a database, it must match the column namereturn:ResultSet-Each row is a column descriptionThrown:SQLException-If a database access error occursSee also:Getsearchstringescape ()

third, the table primary key information Getprimarykeys

Getprimarykeys (String catalog,  String schema,  string table)    throws SQLException

Retrieves a description of the primary key column for a given table. They are sorted according to column_name.

Each primary key column description has the following columns: table_cat string => table category (nullable) Table_schem String => table mode (can be NULL) table_name string => table name column_name string => column name key_seq short => serial number in primary key Pk_name String => The name of the primary key (nullable) parameter: Catalog-category name, because save stored in the database, so it must match the category name. The parameter is "" to retrieve a description without a category, or null to indicate that the category name should not be used to narrow the search scope schema-schema name because it is stored in the database, so it must match the schema name. The parameter is "" to retrieve the description without the schema, or null to indicate that the schema name should not be used to narrow the search Scope table-table name, because it is stored in the database, so it must match the table name return: ResultSet-Each row is a master The key column description throws: SQLException-If a database access error occurs

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.