Friday 13 January 2012

Database Metadata

    Metadata is data about data.
    Database metadata is information about a database.
    Database metadata provides information about the structure of a database and its tables,     views, and   stored procedures.

JDBC provides four interfaces that deal with database metadata
  1. java.sql.DatabaseMetaData: about the database as a whole: table names, table indexes, database product name and version, and actions the database supports.
  2. java.sql.ResultSetMetaData: about the types and properties of the columns in a ResultSet object.
  3. java.sql.ParameterMetaData: about the types and properties of the parameters in a PreparedStatement object.
  4. javax.sql.RowSetMetaData: about the columns in a RowSet object.
JDBC DatabaseMetaData is an interface of java.sql.*; package. DatabaseMetaData is generally implemented by the Database application vendors to know the capability of Database Management System in combination with the driver based JDBC technology. This interface is the tool for the user who need to discover how to deal with underlying database. Some meta data methods returns the string in form of ResultSet object. Then we retrieve data from this object with getInt(), and getString() methods. Some metadata objects takes argument also.

Example :


public class JDBCDatabaseMetaDataExample {
        public static void main(String args[]) {
                try {
                        // Loading database driver
                        Class.forName("com.mysql.jdbc.Driver");

                        // Connecting to the database
                        Connection conn = DriverManager.getConnection(
                                        "jdbc:mysql://localhost:3306/student", "root", "root");

                        DatabaseMetaData metaData = conn.getMetaData();
                        ResultSet rs = metaData.getTypeInfo();
                        System.out.println("Printing All 
                        Premitive DataTypes supported by this Database Applications\n");
                        while (rs.next()) {
                                System.out.println(rs.getString(1));
                        }
                        rs.close();
                        Statement stmt = conn.createStatement();
                        ResultSet resultSet = stmt.executeQuery("SELECT * FROM student");
                        ResultSetMetaData md = resultSet.getMetaData();
                        System.out.println("\n Fetching Query.............");
                        for (int i = 1; i <= md.getColumnCount(); i++)
                                System.out.print(md.getColumnLabel(i) + " ");
                        System.out.println();
                        while (resultSet.next()) {
                                for (int i = 1; i <= md.getColumnCount(); i++)
                                        System.out.print(resultSet.getString(i) + " ");
                                System.out.println();
                        }
                        resultSet.close();
                        stmt.close();
                        conn.close();
                } catch (SQLException e) {
                        System.out.println(e.toString());
                } catch (Exception e) {
                        System.out.println(e);
                }
        }
}







No comments:

Post a Comment