Listing All Table Names in a Database - Java JDBC

Java examples for JDBC:Table

Description

Listing All Table Names in a Database

Demo Code

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

public class Main {

  public void m() {
    try {/*from   w w  w  .j  a  v a 2 s  .c  o m*/
      Connection connection = null;
      // Gets the database metadata
      DatabaseMetaData dbmd = connection.getMetaData();

      // Specify the type of object; in this case we want tables
      String[] types = { "TABLE" };
      ResultSet resultSet = dbmd.getTables(null, null, "%", types);

      // Get the table names
      while (resultSet.next()) {
        // Get the table name
        String tableName = resultSet.getString(3);
        String tableCatalog = resultSet.getString(1);
        String tableSchema = resultSet.getString(2);
      }
    } catch (SQLException e) {
    }
  }
}

Related Tutorials