JDBC Get Metadata

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

Ads

Tutorials   
JDBC Video Tutorial: Create, Read, Update and Delete (CRUD) Operation examples JDBC Video tutorial - How to read Data from MySQL Database? JDBC Video tutorial - How to delete Data in a MySQL Database? Solving java.lang.ClassNotFoundException: com.mysql.jdbc.Driver exception JDBC Video tutorial - How to update Data in a MySQL Database? JDBC Video Tutorial- How to insert data into MySQL database table? JDBC Video Tutorial: How to download JDBC Driver for MySQL? JDBC Tutorial - Writing first JDBC example and running in Eclipse Java Cross Join MySQL Database Connectivity Example In Java Change Column Name in MySQL Java Mysql Connection Example JDBC ODBC Connection In Java ResultSet In Java Prepared Statement Example JdbcRowSet interface Java JdbcRowSet Example First Step towards JDBC! JDBC Training, Learn JDBC yourself Jdbc Get Column Names JDBC Example with MySQL JDBC Prepared Statement Update JDBC Prepared Statement Insert JDBC Prepared Statement Example JDBC Prepared statement Close JDBC Prepared Statement Addbatch JDBC Nested Resultset JDBC-Odbc Connection JDBC-Odbc Connectivity JDBC Insert Preparedstatement JDBC Insert Null JDBC Get Metadata JDBC Insert Record Jdbc Insert Statement JDBC Meta Data Get tables Jdbc Mysql Connection String Jdbc Mysql Connection Url JDBC Next Jdbc Get Connection JDBC Get Int JDBC Exception JDBC Get Row Count JDBC Functions JDBC Fetch JDBC Execute Update Example JDBC Execute Statement JDBC Execute Query JDBC Drive For Mysql JDBC Connectivity Code In Java JDBC connection timeout
Ads

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

Advertisements

Ads
Share on Google+Share on Google+

JDBC Get Metadata

Posted on: November 18, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: JDBC Get Metadata  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads