Java DatabaseMetaData getTables() method with example



This method retrieves the description of the tables available in the specified database/catalog. It accepts 4 parameters −

  • catalog   − A string parameter representing the name (or, name pattern) of the catalog (database in general) in which the table (that contains the columns of which you need to retrieve the description about) exists. pass "" to get the description of the columns in tables with no catalog and, pass null if you don't want to use catalog and thus narrow the search.

  • schemaPattern  − A String parameter representing the name (or, name pattern) of the schema of the table, pass "" if in case of columns in tables with no schema and, pass null if you don't want to use schema.

  • tableNamePattern  − A String parameter representing the name (or, name pattern) of the table.

  • types − A String parameter representing the name (or, name pattern) of the column.

This method returns a ResultSet object describing the tables in the specified catalog. This object holds values for the following details (as column names) −

Column name
Data type
Description
TABLE_CAT
String
Catalog of the table.
TABLE_SCHEM
String
Catalog of the schema.
TABLE_NAME
String
Name of the table.
TABLE_TYPE
String
Type of the table. (table, view, system table, global table, alias, synonym etc..)
REMARKS
String
Comments on the column.
TYPE_SCHEM
String
Schema of the table.
TYPE_NAME
String
Name of the type.
SELF_REFERENCING_COL_NAME
String
Name of the designated column of the table.
REF_GENERATION
String
Either SYSTEM or, USER or, DERIVED.


To get the description of required tables in the database −

  • Make sure your database is up and running.

  • Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.

  • Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, as String variables.

  • Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.

  • Finally get ResultSet object holding the description of the tables, by invoking the getTables() method of the DatabaseMetaData interface .

Example

Let us create a database with name sample_database and, created a table sample_table in it using CREATE statements as shown below −

CREATE DATABASE example_database;
CREATE TABLE example_database.sample_table(Name VARCHAR(255), age INT, Location VARCHAR(255));

Now, we will insert 2 records in the sample_table table using INSERT statements −

insert INTO example_database.sample_table values('Kasyap', 29, 'Vishakhapatnam');
INSERT INTO example_database.sample_table values('Krishna', 30, 'Hyderabad');

Following JDBC program establishes connection with MySQL database and, retrieves the description of the table named sample_table created above.

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetaData_getTables {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql://localhost/example_database";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Retrieving the columns in the database
      ResultSet tables = metaData.getTables(null, null, "sample_table", null);
      //Printing the column name and size
      while (tables.next()) {
         System.out.println("Table name: "+tables.getString("Table_NAME"));
         System.out.println("Table type: "+tables.getString("TABLE_TYPE"));
         System.out.println("Table schema: "+tables.getString("TABLE_SCHEM"));
         System.out.println("Table catalog: "+tables.getString("TABLE_CAT"));
         System.out.println(" ");
      }
   }
}

Output

Connection established......
Table name: sample_table
Table type: TABLE
Table schema: null
Table catalog: example_database
Updated on: 2019-07-30T22:30:26+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements