Data about data is called metadata. That means data about column names, data type names of database software etc. JDBC supported Metadata programming areas follows:
1 Database Metadata
2 ResultSet Metadata
We’ll be covering the following topics in this tutorial:
Database Metadata
By this programmer can get limitations and capabilities of underlying Database software like database software name, versions, max chars in table, etc. Database- Metadata object means it the object of a java class that implements java.sql. Database Metadata (interface). Programmer call various methods on Database Metadata object to gather much information about underlying database software.
Example:
DatabaseMetaDatadbmd=con.getMetaData();
Use
DatabaseMetaData programming is useful while developing GUI tools for Database software like sqlyog, mysql front, etc.
JDBC Metadata Programming is in no way related to performing CURD operations on database. It is given to gather additional details about database software and its tables. If certain JDBC driver cannot retrieve certain information about database then it returns zero “0” or “null” value while working with DatabaseMetaData object.
Example application
//DBMetaData.java
import java.io.*;
import java.sql.*;
public class DBMetaData
{
public static void main (String args[ ]) throws SQLException,ClassNotFoundException
{
Class.forName(“Sun.jdbc.odbc.JdbcOdbcDrivet”) ;
Connection con=DriverManager.getConnection(“jdbc:odbc:oradsn”,”scott”,”tiger”);
DatabaseMetaData dbmd=con.getMetaData();
System.out.println(“database version:”+dbmd.getDatabaseProductVersion()+”\n”);
System.out.println(“database name:”+dbmd.getDatabaseProductName()+”\n”);
System.out.println(“sql keywords:” +dbmd.getSQLKeywords() +”\n”);
System.out.println(“numeric functions: “+dbmd.getNumericFunctions () +”\n”) ;
System.out.println(“String functions: “+dbmd.getStringFunctions ()+”\n” );
System.out.println (“System functions:” +dbmd.getSystemFunctions()+”\n”) ;
System.out.println(“Searc string Escape:” +dbmd.getSearchStringEscape ()+”\n”);
System.out.println (“getMaxRowSize: “+dbmd.getMaxRowSize()+”\n”);
System.out.println (“getMaxstatementLength:”+dbmd.getMaxStatementLength() +”\n”);
System.out.println(“get maximum tables in a select query:”+dbmd.getMaxTablesInSelect()+”\n”);
System.out.println(“get maximum length of table name:”+dbmd.getMaxTableNameLength()+”\n”);
System.out.println(“jdbcapi version is :”+dbmd.getJDBCMajorVersion()+”\n”);
}
}
ResultSetMetaData
It is used to gather more information about database table that is represented by JBDC ResultSet object. It is used to get no of columns, column name, column data types, etc., information from database table.
ResultSetMetaData object means it is the object of JBDC driver supplied Java class that implements java.sql.ResultSet.MetaData(interface). This object is very useful while generating reports based on database table data; like progress report, sales report, etc.
Method calling
ResultSet rs =st.executeQuery(“select * from emp”);
ResultSetMetaData rsmd=rs.getMetaData();
Here we have taken college table as follows:
//ResuleSetMetaDataTest
import java.sql.*;
import java.util.*;
public class ResultSetMetaDataTest
{
public static void main(String args[]) throws SQLException,ClassNotFoundException
{
//read inputs
Scanner sc = new Scanner(System.in);
System.out.println(“Enter table name”);
String tname=sc.next();
//create jdbc connection
Class.forName(“oracle.jdbc.driver.OracleDriver”) ;
Connection cn=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:oracl”,”scott”,”tiger”);
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery (“select * from “+tname);
ResultSetMetaData rsmd=rs.getMetaData();
int colcnt=rsmd.getColumnCount();
//System.out.println(“Columns present in the table are:\n”) ;
for(int i=1;i<=colcnt;i++)
{
System.out.print (rsmd.getColumnLabel(i)+”\t\t” ) ;
}
System.out.println(“\n”);
//Systeml.out.println(“datatypes of the columns are:\n”);
for(int i=1;i<=colcnt;++i)
{
System.out.print (rsmd.getColumnTypeName(i)+”\t”);
}
System.out.println(“\n”);
//System.out.println(“records of the table are:\n”);
while(rs.next())
{
System.out.println(rs.getString(1)+”\t\t”+rs.getString(2)+”\t\t”+rs.getString(3));
/*for (int i=1; i<=colcnt; ++i)
{
System.out.println(rs. getString(i) +”\ t “) ;
}*/
}
rs.close() ;
st.close() ;
cn.close();
}
}