JDBC Tutorial

Metadata

Metadata:

Data about data is known as metadata. Metadata can be obtained at two levels, they are user database details and universal database details.

Obtaining user database details:

In order to obtain user database details we must follow the following procedure:

  1. Obtain an object of ResultSetMetaData by using the following method which is present in
  2. ResultSet.

    Result set metadata

    For example:

    ResultSetMetaData rsmd=rs.getMetaData ();
    
  3. In general every user database contains number of columns, name of the columns and type of columns. In order to obtain the above information we must use the following methods which are present in ResultSetMetaData interface.
  4. public int getColumnCount (); 
    public String getColumnName (); 
    public String getColumnLabel (); 
    public String getColumnType ();
    

Obtaining universal database details:

When we get a connection from the database we can come to know which database we are using. To obtain information about universal database we must use the following steps:

  1. Obtain an object of DatabaseMetaData by calling the following method which is present in Connection interface.
  2. Database MetaData

    For example:

    DatabaseMetaData dmd=con.getMetaData ();
    
  3. In general every universal database contains database name, database version, driver name, driver version, driver major version and driver minor version. To obtain these information, DatabaseMetaData interface contains the following methods:
  4. public String getDatabaseProductName (); 
    public String getDatabaseProductVersion (); 
    public String getDriverName ();
    public String getDriverVersion (); 
    public String getDriverMajorVersion (); 
    public String getDriverMinorVersion ();
    

Write a java program which illustrates the concept of DatabaseMetaData and ResultSetMetaData?

Answer:

import java.sql.*;

class MetaData {

    public static void main(String[] args) {

        try {
            DriverManager.registerDriver(new Sun.jdbc.odbc.JdbcOdbcDriver());
            System.out.println("DRIVERS LOADED...");
            Connection con = DriverManager.getConnection("jdbc : odbc : oradsn", "scott", "tiger");
            System.out.println("CONNECTION ESTABLISHED...");

// UNIVERSAL DATABASE DETAILS
            DatabaseMetaData dmd = con.getMetaData();
            System.out.println("DATABASE NAME : " + dmd.getDatabaseProductName());
            System.out.println("DATABASE VERSION : " + dmd.getDatabaseProductVersion());
            System.out.println("NAME OF THE DRIVER : " + dmd.getDriverName());
            System.out.println("VERSION OF THE DRIVER : " + dmd.getDriverVersion());
            System.out.println("MAJOR VERSION OF DRIVER : " + dmd.getDriverMajorVersion());
            System.out.println("MINOR VERSION OF DRIVER : " + dmd.getDriverMinorVersion());

// USER DATABASE DETAILS
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from dept");
            ResultSetMetaData rsmd = rs.getMetaData();
            System.out.println("NUMBER OF COLUMNS : " + rsmd.getColumnCount());
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {

                System.out.println("NAME OF THE COLUMN : " + rsmd.getColumnName(i));
                System.out.println("TYPE OF THE COLUMN : " + rsmd.getColumnType(i));
            }
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }// main
};// MetaData

Write a java program which points the data of a table along with its column names?

Answer:

import java.sql.*;

class Table {

    public static void main(String[] args) {

        try {
            DriverManager.registerDriver(new Sun.jdbc.odbc.JdbcOdbcDriver());
            System.out.println("DRIVERS LOADED...");
            Connection con = DriverManager.getConnection("jdbc:odbc:oradsn", "scott", "tiger");
            System.out.println("CONNECTION  ESTABLISHED...");
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from dept");
            ResultSetMetaData rsmd = rs.getMetaData();
            System.out.println("==================================================");

// PRINTING COLUMN NAME
            for (int i = 1; i <= rsmd.getColumnCount(); i++) {

                System.out.print(rsmd.getColumnName(i) + "	");
            }
            System.out.println("");
            System.out.println("==================================================");

// PRINTING THE DATA OF THE TABLE
            while (rs.next()) {
                for (int j = 1; j <= rsmd.getColumnCount(); j++) {
                    System.out.print(rs.getString(j) + "	");
                }
                System.out.println("");
            }
            con.close();
        } catch (SQLException sqle) {
            sqle.printStackTrace();

        }
    }// main
};// Table