Java  DatabaseMetaData getColumns() method with example

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.
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.
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.
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.
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) −
To get the description of required columns in the database −
Make sure your database is up and running.
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.
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 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.
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.
Finally get ResultSet object holding the description of the required columns, by invoking the getColumns() method of the DatabaseMetaData interface.
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(" ");
      }
   }
}
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