JDBC-ODBC - Listing Tables with meta.GetTables()

This section describes how to get a list of tables in flat text file database connected by an ODBC driver.

From the previous tutorial, we know that the database represented by the ODBC driver based on flat text files is read-only. So I need to create some text files in the mapped directory. Each text file will be presented as a table by the ODBC driver.

To create 2 tables in the database represented by the DSN, HY_FLAT, I added two empty files in C:\local\ODBC_Flat_DB:

04/01/2007  06:31 PM                 0 Address.txt
04/01/2007  06:31 PM                 0 User.txt

The program below uses the getTables() method of the DatabaseMetaData to list all existing tables:

/* OdbcFlatListTables.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
import java.sql.*;
public class OdbcFlatListTables {
  public static void main(String [] args) {
    Connection con = null;
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:HY_FLAT");

      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) {
      System.err.println("Exception: "+e.getMessage());
    }
  }
}

The output looks good:

herong> progra~1\java\jdk1.7.0_45\bin\javac
   OdbcFlatListTables.java

herong> progra~1\java\jdk1.7.0_45\bin\java OdbcFlatListTables

List of tables:
   C:\LOCAL\ODBC_FLAT_DB, null, Address.txt, TABLE, null
   C:\LOCAL\ODBC_FLAT_DB, null, User.txt, TABLE, null

So Microsoft ODBC Text Driver manages flat text files as a database with the following rules:

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

 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 - Creating DSN for Flat Test File

 JDBC-ODBC - Connecting to Flat Text Files

 JDBC-ODBC - Getting Flat File Driver Info

 JDBC-ODBC - CREATE TABLE in Flat Text Files

JDBC-ODBC - Listing Tables with meta.GetTables()

 JDBC-ODBC - Tab Delimited Flat File Data

 JDBC-ODBC - ODBC Configuration for Flat Files

 JDBC-ODBC - Executing Queries on Flat Files

 JDBC-ODBC - Missing Flat Data Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Archived Tutorials

 References

 Full Version in PDF/EPUB