JDBC Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 2.11

Getting Database Server and Driver Info

This section describes how to get database server and JDBC driver information through the DatabaseMetaData object.

If you want to know the database server name and version, or the JDBC driver name and version, you can get them through the DatabaseMetaData object as shown in the following sample program:

/**
 * DatabaseInfo.java
 * Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
 */
import java.sql.*;
public class DatabaseInfo {
  public static void main(String [] args) {
    Connection con = null;
    try {

// Load Microsoft JDBC Driver 1.0
      Class.forName(
        "com.microsoft.sqlserver.jdbc.SQLServerDriver");

// Obtaining a connection to SQL Server
      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost:1269;"
        + "user=sa;password=HerongYang");

// Database and driver info
      DatabaseMetaData meta = con.getMetaData();
      System.out.println("Server name: " 
        + meta.getDatabaseProductName());
      System.out.println("Server version: "
        + meta.getDatabaseProductVersion());
      System.out.println("Driver name: "
        + meta.getDriverName());
      System.out.println("Driver version: "
        + meta.getDriverVersion());
      System.out.println("JDBC major version: "
        + meta.getJDBCMajorVersion());
      System.out.println("JDBC minor version: "
        + meta.getJDBCMinorVersion());

      con.close();
    } catch (java.lang.ClassNotFoundException e) {
      System.err.println("ClassNotFoundException: "
        +e.getMessage());
    } catch (SQLException e) {
      System.err.println("SQLException: "
        +e.getMessage());
    }
  }
}

If you run this program you will get:

Server name: Microsoft SQL Server
Server version: 9.00.1399
Driver name: Microsoft SQL Server 2005 JDBC Driver
Driver version: 1.0.809.102
JDBC major version: 3
JDBC minor version: 0

JDBC version information tells us that Microsoft JDBC Driver 1.0 supports only JDBC 3.0 API.

Sections in This Chapter

Commonly Used DatabaseMetaData Methods

Getting Database Server and Driver Info

Listing All Databases - getCatalogs()

Listing All Schemas - getSchemas()

Listing All Tables - getTables()

Listing All Culumns - getColumns()

Listing All Stored Procedures - getProcedures()

Dr. Herong Yang, updated in 2007
Getting Database Server and Driver Info