In this tutorial, we are going to learn about DatabaseMetaData in JDBC with example. In the previous tutorial, we have discussed about what is ResultSetMetaData with example. DatabaseMetaData is some thing similar like ResultSetMetaData.

DtabaseMetaData in JDBC :

Here we listed some of the important points about DatabaseMetaData in JDBC.

  • DatabaseMetaData is an interface, which is coming from java.sql package.
  • We can use the DatabaseMetaData, when we want to know about database capabilities.
  • Typically the DatabaseMetaData helps while creating the database tools.
  • We can get the capabilities of a database by calling the getMetaData() on connection object.
java.sql.DatabaseMetaData dbMetaData = connection.getMetaData();

Here is the simple example for DatabaseMetaData in JDBC.

DatabaseMetaData in JDBC Example:
package com.onlinetutorialspoint.jdbc; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 
import java.sql.Statement; 
import java.util.Scanner; 
import com.mysql.jdbc.DatabaseMetaData; 
public class Jdbc_MetaData_Example { 
    public static void main(String[] args) throws Exception { 
        Connection connection = null; 
        connection = DriverManager.getConnection( 
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", 
        java.sql.DatabaseMetaData dbMetaData = connection.getMetaData(); 
        System.out.println("Database Name=  " 
                + dbMetaData.getDatabaseProductName()); 
        System.out.println("Database version=  " 
                + dbMetaData.getDatabaseProductVersion()); 
        System.out.println("Database Driver version=  " 
                + dbMetaData.getDriverVersion()); 
        System.out.println("Sql keywords = " + dbMetaData.getSQLKeywords()); 
        System.out.println("numeric functions=  " 
                + dbMetaData.getNumericFunctions()); 
        System.out.println("String functions=  " 
                + dbMetaData.getStringFunctions()); 
        System.out.println("Search String Escapre=  " 
                + dbMetaData.getSearchStringEscape()); 
        System.out.println("Supported storage procedures=  " 
                + dbMetaData.supportsStoredProcedures()); 
        System.out.println("getMaxRowSize=" + dbMetaData.getMaxRowSize()); 
        System.out.println("getMaxStatement length=  " 
                + dbMetaData.getMaxStatementLength()); 
        System.out.println("get max tables in a select query=  " 
                + dbMetaData.getMaxTablesInSelect()); 
        System.out.println("get Max Length of Table Name=  " 
                + dbMetaData.getMaxTableNameLength()); 
        System.out.println("jdbc api version is=  " 
                + dbMetaData.getJDBCMinorVersion()); 

Output :

Database Name=  MySQL
Database version=  5.7.12-0ubuntu1
Database Driver version=  mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
Search String Escapre=  \
Supported storage procedures=  true
getMaxStatement length=  65531
get max tables in a select query=  256
get Max Length of Table Name=  64
jdbc api version is=  0

On the above output, we can find the information about database.

Happy Learning 🙂