JDBC Resultsetmetadata Interface

Advertisements

Prev Tutorial Next Tutorial

Resultsetmetadata Interface in JDBC

The metadata means data about data, in case of database we get metadata of a table like total number of column, column name, column type etc.

While executing a select operation on a database if the table structure is already known for the programmer, then a programmer of JDBC can read the data from ResutlSet object directly. If the table structure is unknown then a JDBC programmer has to take the help of ResultSetMetadata.

A ResultSetMetaData reference stores the metadata of the data selected into a ResutlSet object.

How to get the object of ResultSetMetaData ?

To obtain a object of ResultSetMetaData, we need to call getMetaData() method of ResutlSet object..

Syntax

ResultSetMetaData rsmd=rs.getMetaData();

Methods of ResultSetMetaData

The following are the methods called on ResultSetMetaData reference.

methodDiscription
1getColumnCount()To find the number of columns in a ResultSet
2getColumnName()To find the column name of a column index.
3getColumnTypeName()To find data type of a column.
4getColumnDisplaySize()To find size of a column.

Example of ResultSetMetaData

import java.sql.*;
class ResmataDataTest
{
public static void main(String[] args)throws Exception 
{
Class.forName("oracle.jdbc.OracleDriver");
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@John-pc:1521:xe","system","system");
System.out.println("driver is loaded");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
ResultSetMetaData rsmd=rs.getMetaData();
//find the no of columns
int count=rsmd.getColumnCount();
for(int i=1;i <=count;i++)
{
System.out.println("column no :"+i);
System.out.println("column name :"+rsmd.getColumnName(i));
System.out.println("column type :"+rsmd.getColumnTypeName(i));
System.out.println("column size :"+rsmd.getColumnDisplaySize(i));
System.out.println("-----------");
}
rs.close();
stmt.close();
con.close();
}
}

Prev Tutorial Next Tutorial

Google Advertisment

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

For Projects 9999595223

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements