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.

Syntax:

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

Methods of DatabaseMetaData interface

MethodDescription
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

JDBCTest.java

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;
 
		try{			
			//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
			conn.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}	
}
click below button to copy the code. By - java tutorial - team

JDBCUtil.java

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 = 
		           "oracle.jdbc.driver.OracleDriver";
	private static final String DB_URL = 
		        "jdbc:oracle:thin:@localhost:1521:XE";
	private static final String DB_USERNAME = "system";
	private static final String DB_PASSWORD = "oracle";
 
	public static Connection getConnection(){
		Connection conn = null;
		try{
			//Register the JDBC driver
			Class.forName(DB_DRIVER);
 
			//Open the connection
			conn = DriverManager.
			getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
 
			if(conn != null){
			   System.out.println("Successfully connected.");
			}else{
			   System.out.println("Failed to connect.");
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return conn;
	}	
}
click below button to copy the code. By - java tutorial - team

Output

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

Related Searches to Java DatabaseMetaData