by Dinesh Thakur Category: Servlet with MySql Database

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
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
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 ( ())
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.



ResultSet get Catalogs ()

Retrieves the catalogue names available in this database.

ResultSet getProcedureColumns (String

catalog, String schemaPattern, String

procedure NamePattern, String


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

ResultSet getProcedures (String

catalog, String schemaPattern, String


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.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles