JDBC Statement – DatabaseMetaData Example

Java

Using the DatabaseMetaData interface you can obtain meta data about the database you are connecting to. You can see database specific information like JDBC driver name and version, database product name and product version as well as information about the tables, columns, views and stored procedures that the underlying database contains.

Common Methods used with DatabaseMetaData

Method NameDescription
getDriverName()Retrieves the name of this JDBC driver
getDriverVersion()Retrieves the version number of this JDBC driver as a String
getDatabaseProductName()Retrieves the name of this database product
getDatabaseProductVersion()Retrieves the version number of this database product
getProcedures(String catalog, String schemaPattern, String procedureNamePattern)Retrieves a description of the stored procedures available in the given catalog
getSchemas(String catalog, String schemaPattern)Retrieves the schema names available in this database
getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types)Retrieves a description of the tables available in the given catalog

Using the DatabaseMetaData object

DatabaseMetaData metadata = connection.getMetaData();
System.out.println("Showing DatabaseMetaData...");
System.out.println("JDBC Driver Name: " + metadata.getDriverName());
System.out.println("JDBC Driver Version: " + metadata.getDriverVersion());
System.out.println("Database Product Name: " + metadata.getDatabaseProductName());
System.out.println("Database Product Version: "+ metadata.getDatabaseProductVersion());

Full Program Listing

package com.avaldes.tutorials;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCDatabaseMetaDataExample {

  public static void main(String[] args)  throws SQLException {
    String database_url   = "jdbc:sqlserver://localhost:1433;databaseName=tutorial";
    String username     = "webuser";
    String password     = "webuser123";
    Connection connection   = null;
    
    try {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e) {
      System.out.println("ERROR: Unable to load SQLServer JDBC Driver");
      e.printStackTrace();
      return;
    }
   
    try {
      connection = DriverManager.getConnection(database_url, username, password);
    } catch (SQLException e) {
      System.out.println("ERROR:  Unable to establish a connection with the database!");
      e.printStackTrace();
      return;
    }
   
    try {
      if (connection != null) {
        DatabaseMetaData metadata = connection.getMetaData();
        System.out.println("Showing DatabaseMetaData...");
        System.out.println("JDBC Driver Name: " + metadata.getDriverName());
        System.out.println("JDBC Driver Version: " + metadata.getDriverVersion());
        System.out.println("Database Product Name: " + metadata.getDatabaseProductName());
        System.out.println("Database Product Version: "+ metadata.getDatabaseProductVersion());
      } else {
        System.out.println("ERROR: Unable to make a database connection!");
      }
    } catch (SQLException e) {
      e.printStackTrace();
      return;
    } finally {
        if (connection != null) connection.close();
    }
  }
}

Output

Showing DatabaseMetaData...
JDBC Driver Name : Microsoft JDBC Driver 4.0 for SQL Server
JDBC Driver Version : 4.0.2206.100
Database Product Name: Microsoft SQL Server
Database Product Version: 12.00.2000

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *