Listing All Tables - getTables()

This section describes how to get a list of all tables in the current database on the SQL Server through the DatabaseMetaData object.

If you want to list all tables in the current database on the SQL server, you can use the DatabaseMetaData method: getTables(). It returns all tables that meets the input criteria specified. Input parameters are:

The output is captured in a ResultSet object with the following fields:

The following sample program displays all tables in the AdventureWorksLT database:

/* ListTables.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
public class ListTables {
  public static void main(String [] args) {
    Connection con = null;
    try {

      con = DriverManager.getConnection(
          "jdbc:sqlserver://localhost\\SQLEXPRESS;"
        + "user=herong;password=T0pSecret;"
        + "database=AdventureWorks2019");

      DatabaseMetaData meta = con.getMetaData();
      ResultSet res = meta.getTables(null, null, null,
         new String[] {"TABLE"});
      System.out.println("List of tables: ");
      while (res.next()) {
         System.out.println(
            "   "+res.getString("TABLE_CAT")
           + ", "+res.getString("TABLE_SCHEM")
           + ", "+res.getString("TABLE_NAME")
           + ", "+res.getString("TABLE_TYPE")
           + ", "+res.getString("REMARKS"));
      }
      res.close();

      con.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

If you run this program, you will get:

herong> java -cp .;mssql-jdbc-9.4.1.jre16.jar ListTables.java

List of tables:
   ..., dbo, AWBuildVersion,...
   ..., dbo, DatabaseLog,...
   ..., dbo, ErrorLog,...
   ..., HumanResources, Department,...
   ..., HumanResources, Employee,...
   ..., HumanResources, EmployeeDepartmentHistory,...
   ..., HumanResources, EmployeePayHistory,...
   ..., HumanResources, JobCandidate,...
   ..., HumanResources, Shift,...
   ..., Person, Address,...
   ..., Person, AddressType,...
   ..., Person, BusinessEntity,...
   ..., Person, BusinessEntityAddress,...
   ..., Person, BusinessEntityContact,...
   ..., Person, ContactType,...
   ..., Person, CountryRegion,...
   ..., Person, EmailAddress,...
   ..., Person, Password,...
   ..., Person, Person,...
   ..., Person, PersonPhone,...
   ..., Person, PhoneNumberType,...
   ..., Person, StateProvince,...
   ..., Production, BillOfMaterials,...
   ..., Production, Culture,...
   ..., Production, Document,...
   ..., Production, Illustration,...
   ..., Production, Location,...
   ..., Production, Product,...
   ..., Production, ProductCategory,...
   ..., Production, ProductCostHistory,...
   ..., Production, ProductDescription,...
   ..., Production, ProductDocument,...
   ..., Production, ProductInventory,...
   ..., Production, ProductListPriceHistory,...
   ..., Production, ProductModel,...
   ..., Production, ProductModelIllustration,...
   ..., Production, ProductModelProductDescriptionCulture,...
   ..., Production, ProductPhoto,...
   ..., Production, ProductProductPhoto,...
   ..., Production, ProductReview,...
   ..., Production, ProductSubcategory,...
   ..., Production, ScrapReason,...
   ..., Production, TransactionHistory,...
   ..., Production, TransactionHistoryArchive,...
   ..., Production, UnitMeasure,...
   ..., Production, WorkOrder,...
   ..., Production, WorkOrderRouting,...
   ..., Purchasing, ProductVendor,...
   ..., Purchasing, PurchaseOrderDetail,...
   ..., Purchasing, PurchaseOrderHeader,...
   ..., Purchasing, ShipMethod,...
   ..., Purchasing, Vendor,...
   ..., Sales, CountryRegionCurrency,...
   ..., Sales, CreditCard,...
   ..., Sales, Currency,...
   ..., Sales, CurrencyRate,...
   ..., Sales, Customer,...
   ..., Sales, PersonCreditCard,...
   ..., Sales, SalesOrderDetail,...
   ..., Sales, SalesOrderHeader,...
   ..., Sales, SalesOrderHeaderSalesReason,...
   ..., Sales, SalesPerson,...
   ..., Sales, SalesPersonQuotaHistory,...
   ..., Sales, SalesReason,...
   ..., Sales, SalesTaxRate,...
   ..., Sales, SalesTerritory,...
   ..., Sales, SalesTerritoryHistory,...
   ..., Sales, ShoppingCartItem,...
   ..., Sales, SpecialOffer,...
   ..., Sales, SpecialOfferProduct,...
   ..., Sales, Store,...
   ..., sys, trace_xe_action_map,...
   ..., sys, trace_xe_event_map,...

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 Microsoft SQL Server Express Edition

 Microsoft JDBC Driver for SQL Server

 Microsoft JDBC Driver - Query Statements and Result Sets

Microsoft JDBC Driver - DatabaseMetaData Object

 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()

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 Using Connection Pool with JDBC

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Archived Tutorials

 References

 Full Version in PDF/EPUB