Java query all tables of Oracle database databasemetadata usage (detailed explanation)
I Get an instance of this object
II Method gettables usage
Prototype:
This method can return the result set resultset, which has 5 columns. If it exceeds the limit, an out of bounds exception will be reported
Function Description: get the table information of the specified parameters
Parameter Description: parameter: Catalog: directory name, which is generally empty Parameter: Schema: database name. For Oracle, user name parameter: tablename: table name parameter: Type: table type (table | view)
Note: parameter names must be capitalized during use. Or get something.
III Usage of method getcolumns
Function Description: get the column information of the specified table.
Prototype:
Parameter Description:
Parameter Catalog: category name parameter schema: user scheme name parameter tablename: database table name parameter columnname: column name
4、 Usage of method getprimarykeys
Function Description: get the primary key information of the specified table.
Prototype:
Parameter Description:
Parameter Catalog: category name parameter schema: user scheme name parameter tablename: database table name
Note: the table name must be specified, otherwise the return value will be nothing.
5、 Method Usage of gettypeinfo()
Function Description: get the data type information of the current database.
6、 Usage of method getexportedkeys
Function Description: get the foreign key information of the specified table.
Parameter Description: parameter Catalog: category name parameter schema: user scheme name parameter tablename: database table name
Next, take MySQL and Oracle as examples to explain these two parameters.
The organizational structure of Oracle and MySQL data is completely different. Intuitively, the attachment ways of tables and views are different. In Oracle, the user management mechanism is adopted. Tables and views are attached to a user. At this time, the user will become a "schema" of Oracle; In mysql, tables and views are directly attached to the database. In this way, get null from catalog in Oracle, and get uppercase user name list from schema. The catalog obtained in MySQL is a list of database names, and the schema is null. Readers can test through the following two methods provided by databasemetadata, and they return the resultset data type.
//Get category definition
rs=dbmd. getCatalogs();
//Get schema definition
rs=dbmd. getSchemas();
Based on the above analysis:
If the database is MySQL: then the first parameter catalog can be the name of the database. When the item is null, fill in null for the database name specified in the URL string and the second parameter schema;
If the database is origin: then the first parameter catalog is null, and fill in the uppercase user name, such as "Scott". If the item is null, the query range is all schema users. Return value analysis
The return value of method gettables is a result set type. For the information in the result set, go to jdk1 More than 20 items are reserved to describe the relevant information of the table, but not every data will return these more than 20 items We can often use the following four items:
TABLE_ Schema: for Oracle, it is an uppercase user name, and for MySQL, it is null.
TABLE_ Name: the name of the table.
TABLE_ Cat = null for Oracle, database name for MySQL.
TABLE_ Type = type of table, which is used for table and view according to an item in the fourth parameter types array.
The above is all about the usage (detailed explanation) of Java querying all tables of Oracle database databasemetadata brought by Xiaobian. I hope you can support programming tips~