Java ResultSetMetaData Interface
The metadata means data about data i.e. we can get further information from the data.
If you have to get metadata of a table like total number of column, column name, column type etc. , ResultSetMetaData interface is useful because it provides methods to get metadata from the ResultSet object.
Commonly used methods of ResultSetMetaData interface
Method | Description |
---|---|
public int getColumnCount()throws SQLException | it returns the total number of columns in the ResultSet object. |
public String getColumnName(int index)throws SQLException | it returns the column name of the specified column index. |
public String getColumnTypeName(int index)throws SQLException | it returns the column type name for the specified index. |
public String getTableName(int index)throws SQLException | it returns the table name for the specified column index. |
How to get the object of ResultSetMetaData:
The getMetaData() method of ResultSet interface returns the object of ResultSetMetaData. Syntax: |
Example of ResultSetMetaData interface :
Output:Total columns: 2
Column Name of 1st column: ID
Column Type Name of 1st column: NUMBER
Java DatabaseMetaData interface
DatabaseMetaData interface provides methods to get meta data of a database such as database product name, database product version, driver name, name of total number of tables, name of total number of views etc.
Commonly used methods of DatabaseMetaData interface
- public String getDriverName()throws SQLException: it returns the name of the JDBC driver.
- public String getDriverVersion()throws SQLException: it returns the version number of the JDBC driver.
- public String getUserName()throws SQLException: it returns the username of the database.
- public String getDatabaseProductName()throws SQLException: it returns the product name of the database.
- public String getDatabaseProductVersion()throws SQLException: it returns the product version of the database.
- public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)throws SQLException: it returns the description of the tables of the specified catalog. The table type can be TABLE, VIEW, ALIAS, SYSTEM TABLE, SYNONYM etc.
How to get the object of DatabaseMetaData:
The getMetaData() method of Connection interface returns the object of DatabaseMetaData. Syntax:
Simple Example of DatabaseMetaData interface :
Output:Driver Name: Oracle JDBC Driver
Driver Version: 10.2.0.1.0XE
Database Product Name: Oracle
Database Product Version: Oracle Database 10g Express Edition
Release 10.2.0.1.0 -Production
Comments
Post a Comment