Java DatabaseMetaData getColumns() method with example

JDBCJava 8Object Oriented ProgrammingProgramming

This method retrieves the description of the columns of a table. 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.

  • columnNamePattern - A String parameter representing the name (or, name pattern) of the column.

This method returns a ResultSet object describing specified columns. 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.
COLUMN_NAME
String
Name of the column.
DATA_TYPE
Int
Data type of the column as integer.
TYPE_NAME
String
Name of the datatype of the column.
COLUMN_SIZE
int
Size of the column.
REMARKS
String
Comments on the column.
COLUMN_DEF
String
Default value of the column.
ORDINAL_POSITION
int
Index of the column in the table.
IS_AUTOINCREMENT
String
Returns yes if the column is auto- incremented, returns false if the column is not auto-incremented, returns an empty String (" ") if cannot be determined.
IS_GENERATEDCOLUMN
String
Returns yes if the column is a generated column, returns false if the column is not a generated column, returns an empty String (" ") if cannot be determined.


To get the description of required columns 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 required columns, by invoking the getColumns() method of the DatabaseMetaData interface.

Example

Let us create a database with name example_database and create a table in it with name sample_table using the CREATE statements as shown below −

Statement stmt = con.createStatement();
stmt.execute("CREATE DATABASE example_database");
stmt.execute("CREATE TABLE example_database.sample_table(Name VARCHAR(255), age INT, Location VARCHAR(255));");

Now, in to this table we will insert two records −

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

Following JDBC program establishes connection with MySQL database, retrieves the description of the specified column.

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DatabaseMetaData_getColumns {
   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 columns = metaData.getColumns(null, null, "sample_table", null);
      //Printing the column name and size
      while (columns.next()){
         System.out.print("Column name and size: "+columns.getString("COLUMN_NAME"));
         System.out.print("("+columns.getInt("COLUMN_SIZE")+")");
         System.out.println(" ");
         System.out.println("Ordinal position: "+columns.getInt("ORDINAL_POSITION"));
         System.out.println("Catalog: "+columns.getString("TABLE_CAT"));
         System.out.println("Data type (integer value): "+columns.getInt("DATA_TYPE"));
         System.out.println("Data type name: "+columns.getString("TYPE_NAME"));
         System.out.println(" ");
      }
   }
}

Output

Connection established......
Column name and size: Name(255)
Ordinal position: 1
Catalog: example_database
Data type (integer value): 12
Data type name: VARCHAR
Column name and size: age(10)
Ordinal position: 2
Catalog: example_database
Data type (integer value): 4
Data type name: INT
Column name and size: Location(255)
Ordinal position: 3
Catalog: example_database
Data type (integer value): 12
Data type name: VARCHAR
raja
Published on 09-May-2019 14:55:28
Advertisements