java tutorial - Java DatabaseMetaData - java programming - learn java - java basics - java for beginners

  • Metadata is nothing but the data about data.
  • The DatabaseMetaData interface provides the ability to get information such as driver name, total number of tables, driver version and so on.
  • We can get the object of DatabaseMetaData by calling getMetaData() method of Connection interface.


DatabaseMetaData dbmd=conn.getMetaData();
click below button to copy the code. By - java tutorial - team

Methods of DatabaseMetaData interface

public String getDriverName()throws SQLExceptionit returns the name of the JDBC driver.
public String getDriverVersion()throws SQLExceptionit returns the version number of the JDBC driver.
public String getUserName()throws SQLExceptionit returns the username of the database.
public String getDatabaseProductName()throws SQLExceptionit returns the product name of the database.

Sample Code

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import com.javawithease.util.JDBCUtil;
 * This class is used to show the use of DataBaseMetaData.
 * @author javawithease
public class JDBCExample {
	public static void main(String args[]){
		Connection conn = null;
			//get connection
			conn = JDBCUtil.getConnection();
			//get DatabaseMetaData
			DatabaseMetaData dbmd = conn.getMetaData();  
			//Result set meta data
			System.out.println("Driver Name: " 
					+ dbmd.getDriverName());
			System.out.println("Driver Version: " 
					+ dbmd.getDriverVersion());
			System.out.println("DB UserName: " 
					+ dbmd.getUserName());
			System.out.println("DB Product Name: " 
				+ dbmd.getDatabaseProductName());
			System.out.println("DB Product Version: " 
				+ dbmd.getDatabaseProductVersion());
			//close connection
		}catch(Exception e){
click below button to copy the code. By - java tutorial - team

import java.sql.Connection;
import java.sql.DriverManager;
 * This is a utility class for JDBC connection.
 * @author jawithease
public class JDBCUtil {
	//JDBC and database properties.
	private static final String DB_DRIVER = 
	private static final String DB_URL = 
	private static final String DB_USERNAME = "system";
	private static final String DB_PASSWORD = "oracle";
	public static Connection getConnection(){
		Connection conn = null;
			//Register the JDBC driver
			//Open the connection
			conn = DriverManager.
			if(conn != null){
			   System.out.println("Successfully connected.");
			   System.out.println("Failed to connect.");
		}catch(Exception e){
		return conn;
click below button to copy the code. By - java tutorial - team


Successfully connected.
Driver Name: Oracle JDBC driver
Driver Version:
DB Product Name: Oracle
DB Product Version: 
Oracle Database 10g Express Edition 
Release - Production

Related Searches to Java DatabaseMetaData