JDBC Get Metadata

The Tutorial illustrate a program that helps you to understand JDBC Get Metadata.

JDBC Get Metadata

JDBC Get Metadata

     

JDBC Get Metadata is the collective data structure, data type and properties of a table. In this Tutorial we want to explain the database table person. The Get Metadata provides you the information about the column name and its data type of each column.

Understand with Example

In this Tutorial we want to describe a code that helps you in understanding a JDBC Get Metadata. The code  include a class Jdbc Get Metadata, Inside the class we have a list of method that return you the information of Metadata are as follow -

Before implementation of class, we import a package name java.sql,enables network interface between front end application and backend database. 

 

 

 

   Methods   Description
   DriverManager.getConnection ( ) Return you an object of connection, that helps in built a connection between url-database.
   createStatement ( ) Return you a SQL object, An object of connection class is used to execute and send queries in the database.
   executeQuery ( )  This method returns  the record set from a table. The record set is assigned to a result set. The sql select statement is used to retrieve the records from a table in the database.
   getMetaData ( ) This method return the number, type and properties of the result set object
   get column count ( ) This method return you the number of column in the Table Person

.Finally the println print the name of the table, column name, column size and data type in the output. In case there is an exception in the try block, the catch  block check the exception and handle it. JdbcGetMetadata.java

import java.sql.*;



public class JdbcGetMetadata {



  public static void main(String args[]) {

  Connection con = null;

  Statement st = null;

  ResultSet rs = null;



  String url = "jdbc:mysql://localhost:3306/";

  String db = "komal";

  String driver = "com.mysql.jdbc.Driver";

  String user = "root";

  String pass = "root";



  try {

  Class.forName(driver);

  con = DriverManager.getConnection(url + db, user, pass);

  st = con.createStatement();



  String sql = "select * from person";

  rs = st.executeQuery(sql);

  ResultSetMetaData metaData = rs.getMetaData();



  int rowCount = metaData.getColumnCount();



  System.out.println("Table Name : " + metaData.getTableName(2));

  System.out.println("Field  \tsize\tDataType");



  for (int i = 0; i < rowCount; i++) {

  System.out.print(metaData.getColumnName(i + 1) + "  \t");

  System.out.print(metaData.getColumnDisplaySize(i + 1) + "\t");

  System.out.println(metaData.getColumnTypeName(i + 1));

  }

  catch (Exception e) {

  System.out.println(e);

  }

  }

}
 

Output

Table Name : person
Field  	size	DataType
id  	2	VARCHAR
cname  	50	VARCHAR
dob  	10	DATE

Download code