JDBC Database MetaData Example


 

JDBC Database MetaData Example

In this example you will learn JDBC DatabaseMetaData interface and how to use this interface in your application.

In this example you will learn JDBC DatabaseMetaData interface and how to use this interface in your application.

JDBC DatabaseMetaData Example

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.

An example given below demonstrate the use of DatabaseMetaData.

At first create table named student in MySql database and inset values into it as.

CREATE TABLE student (
RollNo int(9)  PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text
 );

Insert Value Into student table

INSERT INTO student VALUES(1, 'Ram', 'B.Tech', 'Delhi') ;

 

JDBCDatabaseMetaDataExample.java

package roseindia.net;

import java.sql.*;

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);
		}
	}
}
When you run this application it will display message as shown below:

Printing All Premitive DataTypes supported by this Database Applications

BIT
BOOL
TINYINT
TINYINT UNSIGNED
BIGINT
BIGINT UNSIGNED
LONG VARBINARY
MEDIUMBLOB
LONGBLOB
BLOB
TINYBLOB
VARBINARY
BINARY
LONG VARCHAR
MEDIUMTEXT
LONGTEXT
TEXT
TINYTEXT
CHAR
NUMERIC
DECIMAL
INTEGER
INTEGER UNSIGNED
INT
INT UNSIGNED
MEDIUMINT
MEDIUMINT UNSIGNED
SMALLINT
SMALLINT UNSIGNED
FLOAT
DOUBLE
DOUBLE PRECISION
REAL
VARCHAR
ENUM
SET
DATE
TIME
DATETIME
TIMESTAMP

Fetching Query.............
RollNo Name Course Address
1 vnay MCA Motihari
2 John BCA Patna
3 Vinay MCA Motihari
4 Ram BCA Patna

Download this example code

Ads