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:

Jdbc_MetaData_Example.java
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; 
 
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
        connection = DriverManager.getConnection( 
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root", 
                "123456"); 
 
        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 :

Terminal
Database Name=  MySQL
Database version=  5.7.12-0ubuntu1
Database Driver version=  mysql-connector-java-5.1.6 ( Revision: ${svn.Revision} )
Sql keywords=  ACCESSIBLE,ANALYZE,ASENSITIVE,BEFORE,BIGINT..
numeric functions=  ABS,ACOS,ASIN,ATAN,ATAN2,BIT_COUNT,CEILING,COS,COT,DEGREES,EXP..
String functions=  ASCII,BIN,BIT_LENGTH,CHAR,CHARACTER_LENGTH..
Search String Escapre=  \
Supported storage procedures=  true
getMaxRowSize=2147483639
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 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment