• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Servlet » Database » Accessing Metadata in Java Servlet
Next →
← Prev

Accessing Metadata in Java Servlet

By Dinesh Thakur

To make operations on the database we should know the data formats of the database. When using a ResultSet it is possible to access metadata for the data returned from the query. To obtain an object which encapsulates this metadata, use the method getMetaData () from the ResultSet class. This method returns an object of the ResultSetMetaData class and allows us to determine the number and type of columns.

The following is an example of how to use ResultSetMetaData:

String sql= "select * from Emp_info";
ResultSet rs = stmt.executeQuery (sql);
ResultSetMetaData metaData = rs.getMetaData ();
int column Count = metaData.getColumnCount ();
for (int count=1; count <= columnCount; count++)
{
     String columnType = metaData.getColumnTypeName (count);
     String column Name = metaData.getColumnName (count);
     System.out.println ("Column name:" + columnName + "Type:" + columnType);
}
The output of this appears as follows:
Column name: emp no Type: varchar
Column name: emp name Type: varchar
Column name: designation Type: varchar
Column name: date_of_joining Type: date
Column name: salary_in_rupees Type: float

Methods in the class ResultSetMetadata

 

Table lists the methods in the class ResultSetMetadata, with a short description.

                               Table Methods in the class ResultSetMetadata.

Method Description
public int getColumnCount () throws
SQLException;
Returns the number of columns in the given ResultSet object.
public boolean isSearchable (int column) throws SQLException;Returns whether the specified column can use the where clause.
public String getColumnLabel (int column) throws SQLException;Returns a string, which is a column label. Column label is the alias name given to the table field when
retrieving.
public String getColumnName (int column)
throws SQLException;
Returns the string, which specifies the column name.
public String getTableName (int column) throws SQLException;Returns the table name of the designated column.
public int getColumnType (int column) throws SQLException;Returns the designated column type in the forms of integer, which specified in Types class.
public String getColumnTypeName (int column) throws SQLException;Returns string, which specifies the data type of the column.

The class DatabaseMetadata

Like ResultSetMetaData we can get metadata for databases. The DatabaseMetaData class is used for obtaining database metadata. Like ResultsetMetaData, DataBaseMetaData also contains several methods which describe the database. Here is an example which uses DatabaseMetaData to retrieve a list of tables from the database:

DatabaseMetaData dbmd = dbCon.getMetaData ();
ResultSet rs = dbmd.getTables (null, null, "*", null);
while (rs.next ())
System.out.println (rs.getString (1) +" "+ rs.getString (3));

The above code displays the catalogue name and table name. The output is the following:

Catalogue                                                 Table-Name

/home/matt/classes/Exercises           PSYCHICVAMPIRES

/home/matt/classes/Exercises           BUSINESS CONTACTS                                               

/home/matt/classes/Exercises           SOCIAL CONTACTS

                                                         

The getTables () method of the class DataBaseMetaData returns the object of ResultSet with ten columns (which describe the table).

There are many database metadata methods used in the programming Some of the commonly used database metadata methods are listed in Table.

                 Table List of some methods in the class DataBaseMetaData.

Method

Description

ResultSet get Catalogs ()

Retrieves the catalogue names available in this database.

ResultSet getProcedureColumns (String

catalog, String schemaPattern, String

procedure NamePattern, String

columnNamePattern)

Retrieves a description of the given catalogue’s stored procedure parameter and result columns.

ResultSet getProcedures (String

catalog, String schemaPattern, String

procedureNamePattern)

Retrieves a description of the stored procedures available in the given catalogue procedures available in the given catalogue.

ResultSet getSchemas ()

Retrieves the schema names available in this database.

ResultSet getUDTs (String catalog, String

schemaPattern,String typeNamePattern,

int [] types)

Retrieves a descriptionof the user-defined types(UDTs) defined in a particular schema.

boolean supportsBatchUpdates ()

Retrieves whether this database supports batch updates.

You’ll also like:

  1. Accessing Array Elements in Java with Example
  2. METADATA in JDBC
  3. What is Metadata OR Data Dictionary?
  4. How to Accessing members of a class in C++
  5. Accessing Matrix Elements by Using Array Name as a Pointer
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW