JDBC Resultsetmetadata Interface

Google 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 Advertisements

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

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

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

Cloud computing is the on demand availability of computer system resources, especially data storage and computing power, without direct active management by the user. Cloud Computing Tutorial

College Projects Related to Java, AWT, C Projects for College, C++ Projects for College, Android Projects. Download Java C C++ Projects

Download Projects

Adsense Advertisements


Buy This ads Space 8076671483

Buy This Ad Space @$120 per Month, Ad Size 300X600 Contact on: hitesh.xc@gmail.com or 8076671483 Try this Keyword C++ Programs