Java SQL Table getTables(Connection conn)

Here you can find the source of getTables(Connection conn)

Description

Get a list of tables in the database.

License

Apache License

Parameter

Parameter Description
conn database connection

Exception

Parameter Description
SQLException an exception

Return

a list of tables

Declaration

public static List<String> getTables(Connection conn) throws SQLException 

Method Source Code

//package com.java2s;
/**/*w ww  .java 2  s.co m*/
 * Copyright 2015 Confluent Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/

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

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
    /**
     * The default table types to include when listing tables if none are specified. Valid values
     * are those specified by the @{java.sql.DatabaseMetaData#getTables} method's TABLE_TYPE column.
     * The default only includes standard, user-defined tables.
     */
    public static final Set<String> DEFAULT_TABLE_TYPES = Collections
            .unmodifiableSet(new HashSet<String>(Arrays.asList("TABLE")));
    private static final int GET_TABLES_TYPE_COLUMN = 4;
    private static final int GET_TABLES_NAME_COLUMN = 3;

    /**
     * Get a list of tables in the database. This uses the default filters, which only include
     * user-defined tables.
     * @param conn database connection
     * @return a list of tables
     * @throws SQLException
     */
    public static List<String> getTables(Connection conn) throws SQLException {
        return getTables(conn, DEFAULT_TABLE_TYPES);
    }

    /**
     * Get a list of table names in the database.
     * @param conn database connection
     * @param types a set of table types that should be included in the results
     * @throws SQLException
     */
    public static List<String> getTables(Connection conn, Set<String> types) throws SQLException {
        DatabaseMetaData metadata = conn.getMetaData();
        ResultSet rs = metadata.getTables(null, null, "%", null);
        List<String> tableNames = new ArrayList<String>();
        while (rs.next()) {
            if (types.contains(rs.getString(GET_TABLES_TYPE_COLUMN))) {
                String colName = rs.getString(GET_TABLES_NAME_COLUMN);
                // SQLite JDBC driver does not correctly mark these as system tables
                if (metadata.getDatabaseProductName().equals("SQLite") && colName.startsWith("sqlite_")) {
                    continue;
                }

                tableNames.add(colName);
            }
        }
        return tableNames;
    }
}

Related

  1. getTableFields(Connection con, String tableName)
  2. getTableList(Connection connection)
  3. getTableName(String sql)
  4. getTableNames(Connection conn)
  5. getTableNames(Connection conn)
  6. getTables(Connection connection)
  7. getTables(Connection connection)
  8. getTables(Connection connection)
  9. getTablesFromDatabase(Connection conn)